ローファイ日記

出てくるコード片、ぼくが書いたものは断りがない場合 MIT License としています http://udzura.mit-license.org/

mruby-mrbgems-template を cli tool 化した

f:id:udzura:20191001213708p:plain

一般にパッケージを作る場合テンプレートジェネレーターがあると便利です。mrubyも例外でなく、mrubyでmgemを作る場合は mruby-mrbgem-template が定番です。

github.com

実は今まで以下のような手順でmgemを作っていました。DSLを残せるのはいいのですが、CRuby含む他の言語に比べてちょっと手数が多いかもしれません。

# mrbgem-template のビルド
$ git clone https://github.com/matsumotory/mruby-mrbgem-template; ( cd mruby-mrbgem-template; rake )
$ mv mruby-mrbgem-template/mruby/bin/mruby /usr/local/bin/mruby
$ cat <<RUBY > ./template.rb
MrbgemTemplate.new({
  mrbgem_name: 'mruby-example',
  license: 'MIT',
  github_user: 'your-github-username',
  mrbgem_prefix: File.expand_path('.'),
  class_name: 'Example',
  author: 'your-name',
}).create
RUBY
$ mruby template.rb

もうちょっと手軽な方法もあるといいなーとコマンドラインツールを追加するP/Rをしたらマージされたので:

github.com

一緒にHomebrewのフォーミュラも整備しました。ということで現在は(Macなら)以下のようにmgemの雛形を作れるようになりました。やった!

Homebrew で cli をインストール

$ brew tap mrbgems/mrbgem-template
$ brew install mrbgem-template

ref:

github.com

コマンドを実行

$ mrbgem-template -v
mrbgem-template version 0.2.1

$ mrbgem-template -h
Usage: mrbgem-template [options] mrbgem_name
    -h, --help                       Show usage
    -v, --version                    Show version
    -l, --license [LICENSE]          Set license
    -u, --github-user [USER]         Set user name on github
    -p, --mrbgem-prexif [PREFIX]     Set prefix dir to mgem project
    -c, --class-name [CLASS]         Set class name
    -a, --author [AUTHOR]            Set the author of this mgem
    -m, --mruby-version [VERSION]    Set target mruby version
    -B, --bin-name [BIN_NAME]        Set and generate binary tools
    -b, --[no-]local-builder         Enable or disable local builder
    -C, --[no-]ci                    Enable or disable CI by travis

$ mrbgem-template -c FooBar mruby-foobar
Generate all files of mruby-foobar
create dir : ./mruby-foobar
create dir : ./mruby-foobar/src
create file: ./mruby-foobar/src/mrb_foobar.c
create file: ./mruby-foobar/src/mrb_foobar.h
create dir : ./mruby-foobar/mrblib
create file: ./mruby-foobar/mrblib/mrb_foobar.rb
create dir : ./mruby-foobar/test
create file: ./mruby-foobar/test/mrb_foobar.rb
create file: ./mruby-foobar/mrbgem.rake
create file: ./mruby-foobar/Rakefile
add gitignore entry: ./mruby-foobar/.gitignore
create file: ./mruby-foobar/mruby-foobar.gem
create file: ./mruby-foobar/.travis.yml
create file: ./mruby-foobar/.travis_build_config.rb
create file: ./mruby-foobar/README.md
create file: ./mruby-foobar/LICENSE

  > create udzura/mruby-foobar repository on github.
  > turn on Travis CI https://travis-ci.org/profile of udzura/mruby-foobar repository.
  > edit your mruby-foobar code, then run the following command:

  cd ./mruby-foobar
  git init
  git add .
  git commit -m "first commit"
  git remote add origin git@github.com:udzura/mruby-foobar.git
  git push -u origin master

  > finally, pull-request mruby-foobar.gem to mgem-list https://github.com/bovi/mgem-list

ちなみにcli化の周辺で色々と追加した機能もあり、たとえば

  • -m VERSION オプションで、ビルドにで利用する mruby のバージョンを指定できる(デフォルトはstableの最新、今は 2.0.1 )
  • -B/--bin-name BIN_NAME オプションで、mgemの雛形の中にコマンドラインツールを加えてくれる。
    • そのままビルドすれば BIN_NAME というコマンドが mruby/bin の中にできる
    • mruby-cli と違い、クロスコンパイルなどは必要ない場合にお使いください。2019年では一番手軽な方法だと思います。
      • この手順のブログも書こう...

詳細はヘルプを。

是非使ってみてください! See ya!!

( title logo from: https://github.com/h2so5/mruby-logo )