使用 Homebrew 发布软件

1 相关概念

  • Keg(酒桶):安装好的脚本、软件等;
  • Cellar(酒窖):所有用 Homebrew 安装在本地的脚本、软件组成的集合;
  • Formula(配方):定义如何下载、编译和安装脚本或软件的 Ruby 脚本;
  • Tap:一个包含若干 Formula 的 GitHub 专案。

2 创建 Formula

假如有一个软件的二进制包,需要先上传到网络上,并获取下载链接,如: https://github.com/Bytom/bytom/releases/download/v1.0.7/bytom-1.0.7-darwin_amd64.tgz

然后执行如下命令:

$ brew create https://github.com/Bytom/bytom/releases/download/v1.0.7/bytom-1.0.7-darwin_amd64.tgz

之后 brew 就在其目录 /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/中自动创建一个 ruby 文件 bytom.rb:

# Documentation: https://docs.brew.sh/Formula-Cookbook
#                https://www.rubydoc.info/github/Homebrew/brew/master/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Bytom < Formula
  desc "Official Go implementation of the Bytom protocol "
  homepage "https://bytom.io/"
  url "https://github.com/Bytom/bytom/releases/download/v1.0.7/bytom-1.0.7-darwin_amd64.tgz"
  sha256 "25dd62343157fe6eb7a983edb1455f457cfca07552f02e1f9142227bd961a4a5"
  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install" # if this fails, try separate make/make install steps
  end

  test do
    # `test do` will create, run in and delete a temporary directory.
    #
    # This test will fail and we won't accept that! For Homebrew/homebrew-core
    # this will need to be a test that verifies the functionality of the
    # software. Run the test with `brew test bytom`. Options passed
    # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
    #
    # The installed folder is not in the path, so use the entire path to any
    # executables being tested: `system "#{bin}/program", "do", "something"`.
    system "false"
  end
end

之后按照软件的实际需求对该文件进行编辑,可以修改或者增加以下内容:

desc           # 说明,默认已填写
homepage       # 主页,默认已填写
url            # 文件下载地址,默认已填写
version        # 软件版本号,该字段可以自己添加,强烈建议添加
sha256         # 信息摘要,默认已填写

文件的 sha256 摘要可以使用 openssl 命令获取:

$ openssl sha256 bytom-1.0.7-darwin_amd64.tgz
SHA256(bytom-1.0.7-darwin_amd64.tgz)= 25dd62343157fe6eb7a983edb1455f457cfca07552f02e1f9142227bd961a4a5

之后是文件依赖部分:

# depends_on "cmake" => :build

该部分按照实际需求填写。之后是安装部分:

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
    system "./configure", "--disable-debug",
                          "--disable-dependency-tracking",
                          "--disable-silent-rules",
                          "--prefix=#{prefix}"
    # system "cmake", ".", *std_cmake_args
    system "make", "install" # if this fails, try separate make/make install steps
  end

system 后面是需要执行的安装命令,这个根据实际软件编译过程进行修改。

因为 bytom 是已经编译好的二进制软件包,所以实际的文件进行修改如下:

class Bytom < Formula
    desc "Official Go implementation of the Bytom protocol "
    homepage "https://bytom.io/"
    url "https://github.com/Bytom/bytom/releases/download/v1.0.7/bytom-1.0.7-darwin_amd64.tgz"
    version "1.0.7"
    sha256 "25dd62343157fe6eb7a983edb1455f457cfca07552f02e1f9142227bd961a4a5"
  
    def install
        system 'mv bytomd-darwin_amd64 bytomd'
        system 'mv bytomcli-darwin_amd64 bytomcli'
        bin.install "bytomd"
        bin.install "bytomcli"
    end
  
    test do
      # `test do` will create, run in and delete a temporary directory.
      #
      # This test will fail and we won't accept that! For Homebrew/homebrew-core
      # this will need to be a test that verifies the functionality of the
      # software. Run the test with `brew test bytom`. Options passed
      # to `brew install` such as `--HEAD` also need to be provided to `brew test`.
      #
      # The installed folder is not in the path, so use the entire path to any
      # executables being tested: `system "#{bin}/program", "do", "something"`.
      system "false"
    end
  end

安装过程的 bin.install 是调用 Homebrew 安装二进制软件。保存后,执行安装过程:

$ brew install bytom

之后软件就自动安装在 /usr/local/bin 文件夹下。

3 创建 Tap

之后,为了使其他用户也能安装上面的配置文件进行安装软件,则需要将上一步制作的 bytom.rb 文件上传到 GitHub 上,以供其他用户下载。

创建的 GitHub 仓库名的前缀统一为 homebrew-。例如将 bytom.rb 文件放置在 Bytom 组织的 homebrew-bytom 仓库下。

4 安装创建的软件

初次安装需要指定 tap:

$ brew tap bytom/bytom

该命令将用户名为 bytom 的 homebrew-bytom 联系起来,之后安装 bytom 软件时候就不需要再次指定了。然后进行安装:

$ brew install bytom

验证 bytomd 和 bytomcli 是否正确安装:

$ bytomd version
1.0.7+96a0b65f
$ bytomcli version
Bytomcli v1.0.7+96a0b65f darwin/amd64

5 参考资料

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值