LoginSignup
5
2

More than 3 years have passed since last update.

Homebrewを使って新しいMacでサクッと環境構築したい・改

Last updated at Posted at 2019-12-05

はじめに

MacBook Proを買いました(まだ家に届いていない)。

新しいMacで開発環境を整えるために初期設定を行っていく必要があります。

時は2016年。Hamee Advent Calendar 2016 に投稿した 「Homebrewを使って新しいMacでサクッと環境構築したい」 という記事の通り、新しいMacを迎え入れる際の作業はかなり省略されているのですが、まだまだ手作業でやることは多い。そんな中、

brew tapでGolangの自作アプリケーションを公開する方法

という記事をみて、自分専用のtapを作ると更に省略可できそうだな。ということで試してみることにした。

brew tap とはなんぞや

brew tap は、公式で指定されている formula以外の を追加することのできる Homebrew のコマンドの事。

$ brew tap <username>/<repository>

ここで指定したusername、repositoryはGitHub公開リポジトリはにおいてhttps://github.com/<username>/homebrew-<repository>のような関係になっています。
また、リポジトリ名は "homebrew-xxx" となるようにする必要があります。

配布したいツールのブランチとformula置き場としてのブランチの2を用意する方法がありますが、今回は1つのブランチで試してみます。

手順

まずはGitHub上で公開リポジトリ(今回はhomebrew-hello-world)を作成し、お決まりのコマンドでfirst commitしておく

$ echo "# homebrew-hello-world" >> README.md
$ git init
$ git add README.md
$ git commit -m "first commit"
$ git remote add origin https://github.com/yamadayamada-jp/homebrew-hello-world.git
$ git push -u origin master

1. 自作ツールの準備

自作ツールをpushする(簡単にhello world!)するだけのものをサンプルとして追加してpushする。

helloworld.sh
#!/bin/sh

echo "Hello, World!"

pushした内容に対して適当なtagを追加する。

$ git tag 0.0.1
$ git push origin 0.0.1

GitHubでリポジトリを開きReleaseタブからtagが反映されたか確認する。
先程pushした0.0.1があればOK

このタグを使ってGitHub Releasesを行う。
GitHub Releasesについては こちらの記事 でわかりやすく解説されている。

この時点でリポジトリは以下のような構成になっているはずです。

homebrew-hello-world
|--helloworld.sh
|--README.md

2. formulaを作成する。

まずはformulaを格納するディレクトリを作成する

mkdir formula

formulaファイルを作成する。
手で書いても良いが、生成コマンドはちゃんとあるのでそちらを利用する。

$ brew create https://github.com/yamadayamada-jp/homebrew-hello-world/releases/download/0.0.1/helloworld.sh

すると以下のようなファイルが生成される

homebrew-hello-world.rb
# Documentation: https://docs.brew.sh/Formula-Cookbook
#                https://rubydoc.brew.sh/Formula
# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST!
class HomebrewHelloWorld < Formula
  desc ""
  homepage ""
  url "https://github.com/yamadayamada-jp/homebrew-hello-world/releases/download/0.0.2/helloworld"
  sha256 "88fd602a930bc7c0bb78c385f3cb70e976a0cdc3517020be32f19aae8c8eba17"

  # depends_on "cmake" => :build

  def install
    # ENV.deparallelize  # if your formula fails when building in parallel
    # Remove unrecognized options if warned by configure
# 〜〜〜以下略〜〜〜
  end
end

deschomepageは必要があれば記述する(今回は省略)
insntallメソッドにはインストール方法を記述されていますがツールの樹類によって書き分けが必要。
今回はbin.installでバイナリのファイル名を指定する方法で自作ツールのインストールをします。

homebrew-hello-world.rb
class HomebrewHelloWorld < Formula
  desc ""
  homepage ""
  url "https://github.com/yamadayamada-jp/homebrew-hello-world/releases/download/0.0.1/helloworld"
  sha256 "88fd602a930bc7c0bb78c385f3cb70e976a0cdc3517020be32f19aae8c8eba17"

  def install
    bin.install "helloworld"
  end
end

このファイルを先程作成したformulaディレクトリに設置してpushします。
この時点でリポジトリは以下のような構成になっているはずです。

homebrew-hello-world
|--helloworld
|--README.md
|--formula
  |--homebrew-hello-world.rb

3. brewで自作ツールを取り込む

ここまで出来たら後はtapしてinstallするだけ。

$ brew tap yamadayamada-jp/homebrew-hello-world
$ brew install yamadayamada-jp/hello-world/homebrew-hello-world
$ helloworld
Hello, World!

正しくインストールされました👍

ここまでの作業で僅か2回コマンドを打てばインストール出来るようになったので、更に初期設定が更に楽になりました。

新しいMacのお迎えが楽しみです。

参考

5
2
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
5
2