Before we dive in all the steps needed on how to create a formula, that you will use locally (ergo, not uploading it to homebrew public list), let’s briefly explain what is tap
and formula
in Homebrew.
Formula:
A formula is a package definition written in Ruby. A formula provides instructions and metadata for Homebrew to install a piece of software.
So basically a formula is a Ruby file, needed to install and run whatever you want in your machine. It links to a repo path, (yours). Doing a brew create xxxx.tar.gz
creates the formula (Ruby file), the tar.gz
is the zipped link of the project with the code you want to install in your machine (your repo); this gets clear in the steps bellow.
Tap:
A Git repository of Formulae and/or commands
This is how homebrew makes the reference or connection of your github repo referred in your formula (the ruby file) and saves it in a retrievable path:
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/YOUR-RUBY-FORMULA-HERE
Ok, so let’s begin!
Steps to reproduce:
1.- create a github repo where you are going to place the script or file you will like to run in brew for example: unsign-xcode
2.- add the file where your script is to the repo (don’t add file extension) and also grantpermission to it: chmod +x
and push the commit. I’ve created a file called unsign-xcode
for this example.
3.- add a tag version to the file: v1.0.0
(for example) and push the tag
4.- go to releases in your repo page in GitHub, select tags and in the section tar.gz
right click and select “Copy link address”
5.- now, create the formula (repeat this step everytime you update the version from the files in this repo), this step will create the Ruby file mentioned before:
brew create (paste the link copied before)
For example:
brew create https://github.com/phynet/unsign-xcode/archive/v1.0.0.tar.gz
6.- next, it’s turn to create a new repo, this time for the homebrew formula created in step 5. In this case will be called: homebrew-unsign-xcode
7.- clone it empty and try to move the ruby file created in step 5 to this new repo. The formula file is located in:
/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/unsign-xcode.rb
8.- open the file and edit it removing the line where test
text is placed and leave only the code with install
, there’s where you’re going to add bin.install
with the name of the file:
def install
bin.install "unsign_xcode"
end
9.- add the file to git, commit and push
Note: from here, this steps are going to be similar to any homebrew process to install code
10.- once pushed go to terminal and tap it using a reference to the repo from step 1
brew tap phynet/unsign-xcode
11.- now try to install using brew command
brew install unsign-xcode
11.1.- if a message like this one appears:
Error: unsign-xcode 1.0.3 is already installed
To upgrade to 1.0.4, run `brew upgrade unsign-xcode`
run the command brew update and it will automatically update to the tag version for the repo updated before.
12.- To use it type the command:
unsign-xcode
And there you have it 😀
BTW: this is the repo for my unsign-xcode command. https://github.com/phynet/unsign-xcode.