I read a blog about the spell checking in Xcode. Spell-checking is very useful for me but it has some problems when learning or forgetting words in Xcode. Actually, the spelling checking in Xcode bases on the local dictionary of Mac system. Unfortunately, Mac system doesn’t provide a convenience way for us to manage the local dictionary. The only way to modify the local dictionary is to edit the ~/Library/Spelling/LocalDictionary file, then you must restart the AppleSpelling service with Activity Monitor app by killing the service. The AppleSpell service will restart automatically after being killed, applying modifications to all editors.
A CLI for managing the local dictionary on Mac.
So I want to make a CLI tool to list, learn and forget words in the local dictionary. I have some Ruby experience but I want to write it with Swift this time for measuring efficiency of developing scripts with Swift.
Swift team has already provided a package
swift-argument-parser to help us customize CLIs easily and efficiently.
The best web for finding Swift packages you want.
Using SPM to quickly create an executable package.
There are two generated files,
Package.swift is the SPM configuration file,
main.swift is the application entrypoint.
As you can see, SPM created a HelloWorld example for us. Using
swift run to build and run the package.
Now we add dependencies
RainBow (help output colorful string) to the Package.swift file.
Want to use the swift-argument-parser to create a CLI, we need to renamed the file
LocalDic.swift. In the
LocalDic.swift, we defined a struct
LocalDicconforming to the protocol
ParsableCommand and mark it with
@main indicates the struct is the program entrypoint.
A main.file or another file defined a type marked with @main can be as the entrypoint of executable, but you can not do these simultaneously.
Finished coding, you need to build it as a executable file.
swift build -c release
After the build finished, you can find a executable file
localdic at the path
.build/release/localdic. For easily using, you can copy it to the folder
/usr/local/bin and then you can directly print
localdic in you terminal to invoke the command.
A better way to build or manage our application is to create a
Makefile, a tool that defines a graph of rules for creating targets. A quick tutorial to learn
# Define variables.
In the Makefile, I defined four commands. Other users can directly invoke
make install to build and install the CLI.
The best way to share your applications to other Mac users is uploading them to Homebrew. Homebrew is a package manager for macOS, you can use it to install CLI or GUI applications. Of course, every developer can upload his applications to homebrew for sharing them with the world.
The precondition of uploading our applications to homebrew is a
formulae file that is a ruby-based description file.
The formula file configures basic information of your program, description, download url, version and installation way.
But uploading our formulae to the
homebrew/core repository has strict requirements, the homebrew provides another way
"tap", a personal repository to collect your own formulae.
To create a formula repository, you should initialize a git repository containing a folder
Formula and copy the formula file
localdic.rb into the repository.
Uploading you repository to github with name
homebrew-formulae, then you can invoke the command
brew tap <github-name>/homebrew-formulae to link your remote formula repository to local.
brew tap ssbun/formulae
Installed your own tap repository, you can directly invoke the
brew install localdic to install the formula into your local environment.