LoginSignup
1
0

More than 5 years have passed since last update.

Go Mage 備忘録

Last updated at Posted at 2018-12-05

普段あまり Go を使わない人が magefile を使ったソースで仕事する羽目になった時用のメモ。Mage とは Make の代わりに使う Go 用のビルドツールです。

インストール方法

https://github.com/magefile/mage#installation には go run bootstrap.go をしろと書いてあるが、mage -version が動かないのだけ我慢すれば

go get github.com/magefile/mage

でも特に不便は感じない。

magefile

Mage はこういう行で始まるファイルを magefile と呼ぶ。ファイル名は何でも良い。

// +build mage

package main

先頭の // +build mageBuild Constraints または Build tag と言って、#ifdef のような条件付きビルドを指定するのに使う。

Build Constraints には、予め決まったビルド条件を表すキーワードの他、build/Context BuildTags を使ってプログラマブルに作る事も出来る。この例では、magefile がプログラム本体と混ざってビルドされないように mage を Build tag として使う。

Mage の動作

mage を実行するとこうなる。

  • カレントディレクトリから mage build constraints (// +build mage) が有効になったファイル、すなわち magefile を探す。
  • magefile をパースしてビルドターゲットを探す。
  • magefile から実行ファイルを作る。magefile に変更が無ければ hash 化されたキャッシュを使う。
  • キャッシュは $HOME/.magfile にキャッシュされる。

依存関係

ビルドターゲットの中に mg.Dep で依存を指定できる。

mg.Dep(関数, 関数, ...)

関数はビルドターゲットでも良いし、普通の関数でも良い。型は https://magefile.org/dependencies/ 参照。単なる関数呼び出しと違い、mg.Dep では一度だけ呼ばれる。可能であれば関数は平行に呼ばれる。

更新比較

ファイルの更新時刻を比較するには target.Path を使う。

target.Path(dst, sources, ...)

dst が無かったり、dst より source が新しくて再ビルドが必要な場合は true を返す。

ヒント

  • たまに良くわからない動きになった時はキャッシュを読まない mage -f を使う。
  • magefile 内に書いたログは mage -v で表示。

参考

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