Last active
April 11, 2025 18:33
-
-
Save kwilczynski/8a462f7f98e702d5efa30afea84d5c4e to your computer and use it in GitHub Desktop.
Example Homebrew formula for a Go command-line (CLI) binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Binary < Formula | |
desc "" | |
homepage "" | |
head do | |
url "", branch: "main" | |
depends_on "[email protected]" => :build if build.head? | |
end | |
bottle :unneeded | |
option "without-completion", "Do not install shell completion" | |
depends_on OS.linux? ? :linux : :macos | |
def install | |
if build.head? | |
ENV["CGO_ENABLED"] = "0" | |
ENV["GOGC"] = "off" | |
ENV.prepend_create_path "PATH", "#{HOMEBREW_PREFIX}/bin" | |
ENV.prepend_create_path "PATH", buildpath/"bin" | |
cd buildpath do | |
branch = Utils.safe_popen_read("git", "rev-parse", "--abbrev-ref", "HEAD").strip | |
package = "#{head.url.delete_prefix("https://").delete_suffix(".git")}/pkg/version" | |
binary_version = | |
File.readlines("Makefile").grep(/^VERSION.*[0-9.]+/) do |l| | |
l.split("=")[1].strip.tr('"', "") | |
end[0] += "-#{branch}" | |
ldflags = %W[ | |
-s -w | |
-X #{package}.GitBranch=#{branch} | |
-X #{package}.GitRevision=#{version.commit} | |
-X #{package}.Version=#{binary_version} | |
].join(" ") | |
system "go", "build", *std_go_args(ldflags: ldflags) | |
unless build.without? "completion" | |
output = Utils.safe_popen_read("#{bin}/", "completion", "bash") | |
(bash_completion/"").write output | |
output = Utils.safe_popen_read("#{bin}/", "completion", "zsh") | |
(zsh_completion/"_").write output | |
output = Utils.safe_popen_read("#{bin}/", "completion", "fish") | |
(fish_completion/".fish").write output | |
end | |
end | |
end | |
if OS.mac? && MacOS.version >= :catalina | |
if /com.apple.quarantine/.match?(Utils.safe_popen_read("xattr #{bin}/")) | |
(bin/"").chmod 0755 | |
begin | |
system "xattr", "-d", | |
"com.apple.quarantine", | |
bin/"" | |
ensure | |
(bin/"").chmod 0555 | |
end | |
end | |
end | |
prefix.install_metafiles | |
end | |
def caveats | |
<<~EOS | |
EOS | |
end | |
test do | |
assert_predicate bin/"", :exist? | |
system "", "--help" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment