1
0

HomebrewのFormulaを作成して公開する

Posted at

はじめに

自作したコマンドをhomebrewでインストールして利用できるようにするまでの流れです。

結論こうなります。

$ brew install foobar-cli
$ foobar -v

コマンドを作成する

インストールするコマンドを作成します。
これはどの言語でもいいのですが、インストール時にbuildしたりするよりもbinにしておいたほうがすんなり導入できると思います。

前回作成したgoのreplをコマンドとしてインストールするためgithubに置きます。
適当なタグをつけ、Releasesを作成してbinファイルをアップロードしておきます。

コマンド側の準備はこれで終わりです。

Formulaを作成する

brewコマンドからあたらしいFormulaを作成します。
上記のReleaseにあるappファイルのURLを控えておきます。

$ brew create --set-name=go-repl https://github.com/runeleaf/go-repl/releases/download/v0.1.0/app

私の環境だと/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/go-repl.rbこちらにファイルが作成され、vscodeで開かれました。

このファイルをいったん適当な場所に移動させます。

$ mkdir -p go-brew/Formula
$ mv /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/go-repl.rb go-brew/Formula

移動させたファイルを編集します。
最終的なファイルの内容は以下。
appコマンドをbin.installするという内容です。

# Documentation: https://docs.brew.sh/Formula-Cookbook
#                https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class GoRepl < Formula
  desc "A simple REPL for Go"
  homepage "https://github.com/runeleaf"
  url "https://github.com/runeleaf/go-repl/releases/download/v0.1.0/app"
  sha256 "0784f7ab8163a0ff2ddaf3cbc5ce7e2930287c1c75617614ba79d2a3d65314f7"
  license "MIT"

  def install
    bin.install "app"
  end

  test do
    system "true"
  end
end

ファイルの構成はこのような感じ。

$ cd go-repl
$ ls -T
.
├── Formula
│  └── go-repl.rb
└── README.md

上記のファイルをgithubにpushしておきます。
その際のリポジトリ名ですが、
homebrew-go-repl
というように先頭にhomebrew-をつけておきます。

brewコマンドでインストールする

まずtapし、そのあとinstallをします。

$ brew tap runeleaf/go-repl # homebrew-go-replのこと
$ brew install go-repl

これでgoで作成したコマンドのappが利用できるようになりました。

$ which app
/usr/local/bin/app
$ app
go> foobar
foobar
go> exit
Bye!

コマンドがアップデートされたら上記のhomebrew-go-replを更新しておけば、brewコマンドで更新をすることができます。

削除するときは以下のコマンドで削除できます。

$ brew remove go-repl
$ brew untap runeleaf/go-repl

作成したサンプルコード

1
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
1
0