LoginSignup
3
0

More than 1 year has passed since last update.

単一のバイナリファイルをミニマムにHomebrewで公開する方法

Last updated at Posted at 2022-12-20

概要

homebrewでバイナリ実行ファイルを公開する方法についてまとめました。

下記のアドベントカレンダー記事でJSON整形コマンドを自作したという話をしましたが、
下記記事で作成したJSONを整形するバイナリファイルを例に用いて、
バイナリファイルを公開する方法について説明させていただきます。

Homebrewでミニマムにコマンドを公開する例として参考になればと思います。

1. バイナリファイルを公開

まずバイナリファイルをGithub Release等で公開します。

2. brew createコマンドで雛形を作る

以下のbrew createコマンドで雛形を作ります。 --set-nameにはパッケージの名前を指定します。
引数には公開したいバイナリファイルのURL(1の手順で公開したURL)を指定します。

$ brew create --set-name=jsonfmt https://github.com/cacapouh/jsonfmt/releases/download/0.0.1/jsonfmt

上記のコマンドを実行すると、jsonfmt.rbというファイルがエディタで開かれます。

jsonfmt.rb
# Documentation: https://docs.brew.sh/Formula-Cookbook
#                https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class Jsonfmt < Formula
  desc ""
  homepage ""
  url "https://github.com/cacapouh/jsonfmt/releases/download/0.0.1/jsonfmt"
  sha256 "1b6855b02854192e996611c472bbd1eeb007e14416357cf43a410f024f07d9a1"
  license ""

  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
    # https://rubydoc.brew.sh/Formula.html#std_configure_args-instance_method
    system "./configure", *std_configure_args, "--disable-silent-rules"
    # system "cmake", "-S", ".", "-B", "build", *std_cmake_args
  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 jsonfmt`. 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

開かれたファイルですが、コメントやtest doのところは消してしまって問題ないです。
今回のケースですと、installメソッドを以下のように書き換えるだけでOKです。

def install
  bin.install "jsonfmt"
end

bin.installの引数にはバイナリ実行ファイル名を渡してやります。

3. 公開用のリポジトリを作成する

次にHomebrewパッケージ用のリポジトリを作成します。
リポジトリ名には、homebrew-というプレフィックスをつける必要があります。
JSON整形コマンドの例ですと、homebrew-jsonfmtという名前にしてリポジトリを作成しました。

リポジトリには、先ほど作成したRubyファイルを入れてやる必要があります。

上記のリポジトリでは、Formulaというディレクトリの中にjsonfmt.rbを入れていますが、
リポジトリのルートのところにjsonfmt.rbを配置してやっても挙動は変わらないです。

.
└── Formula
    └── jsonfmt.rb

4. インストール

あとは、通常通りhomebrewでインストールしてやるだけです。

$ brew tap cacapouh/jsonfmt
$ brew install cacapouh/jsonfmt/jsonfmt

おわりに

ご閲覧ありがとうございました!

参考になった文献

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0