使用Homebrew Tap发布个人工具


作为MacOS用户,Homebrew 是必不可少的工具之一,正如它的描述所示 The missing package manager for macOS —— 它提供了很多 App Store 没有的软件和工具。

而什么是 TapTap 全称 Third-Party Repositories,顾名思义为第三方仓库,它可以创建属于自己的软件集合,不用将软件发布到官方仓库,避免了和官方仓库的其它软件同名,也不用发PR,等审核等诸多优势。此次,我将创建一个 Homebrew Tap,用于为我自己开发的部分软件和工具提供更加方便的安装方式。

创建Tap

使用 brew 命令创建

 1└──╼ brew tap-new honmaple/tap
 2Initialized empty Git repository in /usr/local/Homebrew/Library/Taps/honmaple/homebrew-tap/.git/
 3[main (root-commit) 7d893ca] Create honmaple/tap tap
 4 3 files changed, 90 insertions(+)
 5 create mode 100644 .github/workflows/publish.yml
 6 create mode 100644 .github/workflows/tests.yml
 7 create mode 100644 README.md
 8==> Created honmaple/tap
 9/usr/local/Homebrew/Library/Taps/honmaple/homebrew-tap
10
11When a pull request making changes to a formula (or formulae) becomes green
12(all checks passed), then you can publish the built bottles.
13To do so, label your PR as `pr-pull` and the workflow will be triggered.

创建后会提示在 /usr/local/Homebrew/Library/Taps/honmaple/homebrew-tap/ 目录生成一个 Git 仓库

生成模版

比如我想要为已经打包好的工具 Snow 0.1.2 生成模版,注意指定 --tap 参数

1 brew create https://github.com/honmaple/snow/releases/download/v0.1.2/snow-darwin.tar.gz --tap honmaple/tap
2Formula name [snow]:
3...

在输入提示的工具名称后(也可以保持默认),这时会生成并使用默认编辑器打开 /usr/local/Homebrew/Library/Taps/honmaple/homebrew-tap/Formula/snow.rb,内容如下:

 1# Documentation: https://docs.brew.sh/Formula-Cookbook
 2#                https://rubydoc.brew.sh/Formula
 3# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
 4class Snow < Formula
 5  desc "static site generator"
 6  homepage ""
 7  url "https://github.com/honmaple/snow/releases/download/v0.1.2/snow-darwin.tar.gz"
 8  sha256 "78a65c740eff24193132e063654bd1d7023f6687c94ce852e50a03a43eaba558"
 9  license "BSD-3-Clause"
10
11  # depends_on "cmake" => :build
12
13  def install
14    # Remove unrecognized options if they cause configure to fail
15    # https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
16    system "./configure", "--disable-silent-rules", *std_configure_args
17    # system "cmake", "-S", ".", "-B", "build", *std_cmake_args
18  end
19
20  test do
21    # `test do` will create, run in and delete a temporary directory.
22    # ...
23    system "false"
24  end
25end

修改模版

因为我此次发布的只是一个 Go 编译的二进制文件,只用将文件添加到可执行目录即可。如需其它操作,请查阅Homebrew Tap官方文档

修改模版后内容如下:

 1class Snow < Formula
 2  homepage "https://github.com/honmaple/snow"
 3  desc "static site generator"
 4  url "https://github.com/honmaple/snow/releases/download/v0.1.2/snow-darwin.tar.gz"
 5  sha256 "78a65c740eff24193132e063654bd1d7023f6687c94ce852e50a03a43eaba558"
 6  license "BSD-3-Clause"
 7
 8  def install
 9    bin.install "snow"
10  end
11
12  test do
13    assert_match "snow version #{version}", shell_output("#{bin}/snow --version")
14  end
15end

从模版文件中就能看出,如果我们需要发布下一版本,只需要修改 urlsha256 两个参数,其中 sha256 可以使用 sha256sum v0.1.2/snow-darwin.tar.gz 获得

安装软件

1brew install honmaple/tap/snow

注意:不能直接使用 brew install snow,因为如果在 homebrew/core 有相同名称的包,默认将会安装 homebrew/core 上的包,所以必须指明使用哪个 Tap

上传到Github

  1. 首先需要到 Github 创建一个公共仓库,取名 homebrew-{Tap名称}, 比如我的是 honmaple/homebrew-tap,注意名称必须是 homebrew- 开头,方便后续直接使用 {用户名}/{Tap名称}。创建好后先不要克隆到本地

  2. 复制之前创建好的 honmaple/tap 目录,方便后续维护

    1cp -r /usr/local/Homebrew/Library/Taps/honmaple/homebrew-tap ~/Git/
  3. 修改仓库

    1cd ~/Git/homebrew-tap
    2# 删除默认的Github Action
    3rm -rf .github
    4# 添加远程
    5git remote add origin [email protected]:honmaple/homebrew-tap.git
    6# 添加软件
    7git add Formula/snow.rb
    8git commit -m "add snow.rb"
    9git push origin master
  4. 重置本地tap,首先需要卸载之前创建好的 honmaple/tap

    1brew untap honmaple/tap

    如果已经安装了 homebrew/tap/snow, 需要先卸载

    1brew uninstall honmaple/tap/snow

    在执行完 修改仓库 这一步后,就可以重新添加 tap

    1brew tap honmaple/tap

    并重新安装想要的软件

    1brew install honample/tap/snow

结语

至此,创建一个私人使用的 MacOS 软件包集合就此结束,我可以往里面添加一些自己写的软件和工具,可以是 Bash 脚本,也可以是 Go 编译好的二进制等,后面就不用再指定具体目录或者手动复制文件到 $PATH/bin 目录下了。OK

作者: honmaple
链接: https://honmaple.me/articles/2024/03/shi-yong-homebrew-tapfa-bu-ge-ren-gong-ju.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论