Building a Custom Homebrew Formula: A Case on tfblueprintgen

Written by krishnaduttpanchagnula | Published 2024/04/08
Tech Story Tags: software-development | macos | package-management | terraform | apple-macbook | package-manager | opensource | custom-homebrew

TLDRHomebrew has become defacto way to get and install the open source apps in the apple ecosystem. While homebrew has a vast repository of the appplications that it supports, it is sometimes required to create and publish our own packages to be installed be consumed by other people. In this brief blog, we are going to discuss how we can create the custom brew package for an app.via the TL;DR App

Homebrew has become a de facto way to get and install open-source apps in the Apple ecosystem. While Homebrew has a vast repository of the applications that it supports, it is sometimes required to create and publish our own packages to be installed and consumed by other people. In this brief blog, we are going to discuss how we can create the custom brew package for an app named tfblueprintgen, how we can test it, and how we can install it as a homebrew package. The blog will be divided into the following sections:

  • Understanding tfblueprintgen
  • Setting up the development environment
  • Creating and Testing the Homebrew formula
  • Testing it locally
  • Pushing it upstream and installing it from the upstream repos.

Understanding tfblueprintgen

Tfblueprintgen is an open-source command-line tool developed using the Charmbracelet CLI assets. It generates a modular file structure with code for your Terraform projects, speeding up the development process. By automating the creation of boilerplate files and directory structures, Tfblueprintgen streamlines setting new Terraform projects. To learn more about the project, refer to this.

Setting up the development environment

Once you have your application up in the GitHub environment, package your application for release by pushing it.

Once the release is complete, we can see our application is available in compressed format such as .tar.gz or .zip.

First, let’s setup our dev environment:

  • Set HOMEBREW_NO_INSTALL_FROM_API=1 in your shell environment,

  • Run brew tap homebrew/core and wait for the clone to complete. If the clone fails, run the following commands before running the brew tap homebrew/core again.

git config --global core.compression 0
git clone --depth 1 https://github.com/Homebrew/homebrew-core.git
git fetch --depth=2147483647
git pull --all

Once this is done, we are good to create the homebrew formula

Creating and Testing the Homebrew formula

To create the boilerplate homebrew formula, run “brew create <url of .tar.gz>” In my case it is

brew create https://github.com/krishnaduttPanchagnula/tfblueprintgen/archive/refs/tags/0.3.tar.gz

Running the above command opens a file to edit in vim. This formula file contains the following:

  • desc provides a brief description of the package.
  • homepage is left blank in this case.
  • url specifies the download URL for the package source code.
  • sha256 is the SHA-256 checksum of the package, which Home-brew uses to verify the integrity of the downloaded zip.
  • license declares the software license for the package.
  • depends_on specifies the dependencies that the current formula depends on.
  • install contains the instructions for building and installing the package.
  • test defines a test to ensure that the package was installed correctly by checking the version output.

Make changes to the install and test function so it reflects the installation and testing for your application. In my case, I have made changes as follows:

class Tfblueprintgen < Formula
  desc "This contains the formula for installing tfblueprintgen. tfblueprintgen cli utility developed using charmbracelet CLI assets, which generates the Modular file structure with the code for your Terraform code to speed up the development."
  homepage ""
  url "https://github.com/krishnaduttPanchagnula/tfblueprintgen/archive/refs/tags/0.3.tar.gz"
  sha256 "0ef05a67fa416691c849bd61d312bfd2e2194aadb14d9ac39ea2716ce6a834a6"
  license "MIT"

  depends_on "go" => :build

  def install
      puts `ls`
      # system ("cd tfblueprintgen-0.2")
       system ("go build -o tfblueprintgen main.go  ")

      bin.install "tfblueprintgen"
  end

  test do
    system "#{bin}/tfblueprintgen  --version"
    expected_version = "Tfblueprintgen version: 0.3"
    actual_version = shell_output("#{bin}/tfblueprintgen --version").strip
    assert_match expected_version, actual_version
  end
end

Once the formula is defined, install the formula using the following command.

brew install tfblueprintgen.rb

This command installs the package source, builds it according to the formula instructions ( defined in the install function), and installs the resulting binary. To test the binary installed, run “brew test <binaryname>.” In my case, the command will be

brew test tfblueprintgen

If the tests go well, you should see the test process running without any errors.

Pushing it to upstream and installing it from the upstream repo

Once the formula has been written and tested, it is time to publish it. Create a repository with the prefix homebrew like “homebrew-<your_repo_name>,” which in my case is “homebrew-tfblueprintgen.” Clone this repo locally and move your formula to that folder, and push it to Git Hub.

To install your tap locally from the formula stored in GitHub

  • Run brew tap <your_github_username>/homebrew-<your repo given name>

  • Then run brew install <your package name>

In my case, this is

brew tap krishnaduttPanchagnula/homebrew-tfblueprintgen 

brew install tfblueprintgen

Voila, your package can now be installed via homebrew.


Written by krishnaduttpanchagnula | Cloud DevOps engineer, who builds secure and scalable solutions.Explorer of Cultures. History buff. Coffee Connoisseur
Published by HackerNoon on 2024/04/08