Upload a CLI Written in Swift to Homebrew
Writing a command tool with Swift
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.
localdic
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.
Tool
The best web for finding Swift packages you want.
Bootstrapping
Using SPM to quickly create an executable package.
1 | mkdir localdic |
There are two generated files, Package.swift
is the SPM configuration file, main.swift
is the application entrypoint.
1 | . |
1 | // Package.swift |
1 | // main.swift |
As you can see, SPM created a HelloWorld example for us. Using swift run
to build and run the package.
1 | swift run |
Now we add dependencies swift-argument-parse
and RainBow
(help output colorful string) to the Package.swift file.
1 | import PackageDescription |
Want to use the swift-argument-parser to create a CLI, we need to renamed the file main.swift
to LocalDic.swift
. In the LocalDic.swift
, we defined a struct LocalDic
conforming to the protocol ParsableCommand
and mark it with @main
. The @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.
To see the full version code, check out localdic
on github. Detail about using ArgumentParser, you can see the official document
Create a Makefile for your CLI
Finished coding, you need to build it as a executable file.
1 | 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 Makefile
.
1 | # Define variables. |
In the Makefile, I defined four commands. Other users can directly invoke make install
to build and install the CLI.
Your own homebrew formulae and taps
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.
1 | // localdic.rb |
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.
1 | . |
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.
1 | 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.
References
Homebrew Document
A tool explaining shell commands for you
Swift Program Distribution with Homebrew
Build a Command-line Tool