はじめに
唐突に mrbgem を作りたくなる時、ありますよね。そんな時、なにかいい方法無いかなと思ったら @matsumotory さんの mruby-mrbgem-template というものが非常に便利だったので備忘録がてら使い方を記します。
環境
- OS X El Capitan
- mruby 1.3.0
- mruby-mrbgem-template commit cb5c0a3887d(2017/10/4時点の最新バージョン)
使い方
導入
mruby を取得する
おそらく master でもよいのですが、バージョンを明確にしたかったので mruby 1.3.0 を取得して build することにしました。
$ wget https://github.com/mruby/mruby/archive/1.3.0.zip $ mv 1.3.0.zip mruby-1.3.0.zip $ unzip mruby-1.3.0.zip $ cd mruby-1.3.0
mruby-mrbgem-template のインストール
mruby-1.3.0/build_config.rb を編集し、mruby-mrbgem-template を使用する gem として追加します。具体的には次のように編集します。
MRuby::Build.new do |conf| # load specific toolchain settings # Gets set by the VS command prompts. if ENV['VisualStudioVersion'] || ENV['VSINSTALLDIR'] toolchain :visualcpp else toolchain :gcc end enable_debug # Use mrbgems # conf.gem 'examples/mrbgems/ruby_extension_example' # conf.gem 'examples/mrbgems/c_extension_example' do |g| # g.cc.flags << '-g' # append cflags in this gem # end # conf.gem 'examples/mrbgems/c_and_ruby_extension_example' # conf.gem :core => 'mruby-eval' # conf.gem :mgem => 'mruby-io' # conf.gem :github => 'iij/mruby-io' # conf.gem :git => 'git@github.com:iij/mruby-io.git', :branch => 'master', :options => '-v' # ↓↓↓ 追加 conf.gem github: 'matsumoto-r/mruby-mrbgem-template' conf.gem '../mruby-nmea' # include the default GEMs # (以下省略)
mruby の build
mruby を build します。単純に rake コマンドを実行するだけです。
$ rake
create.rb の作成
mruby-1.3.0 の直下に create.rb を作成します。中身は、次の通り。paramsの各値を編集します。
params = {
:mrbgem_name => 'mruby-example', # mrbgemの名前
:license => 'MIT', # ライセンス
:github_user => 'miyohide', # GitHubのユーザ名
:mrbgem_prefix => '..', # mrbgemのディレクトリのパス
:class_name => 'Example', # 生成するクラス名
:author => 'miyohide', # 作者名
}
c = MrbgemTemplate.new params
c.create
雛形作成
これでmrbgemの雛形をつくる準備が整ったので、
$ ./bin/mruby create.rb
を実行すると、mrbgemの雛形が作られます。上記のようにcreate.rbを設定すると、
[~/work/mruby-1.3.0]$ ./bin/mruby create.rb Generate all files of mruby-example create dir : ../mruby-example create dir : ../mruby-example/src create file: ../mruby-example/src/mrb_nmea.c create file: ../mruby-example/src/mrb_nmea.h create dir : ../mruby-example/mrblib create file: ../mruby-example/mrblib/mrb_nmea.rb create dir : ../mruby-example/test create file: ../mruby-example/test/mrb_nmea.rb create file: ../mruby-example/mrbgem.rake create file: ../mruby-example/Rakefile add gitignore entry: ../mruby-example/.gitignore create file: ../mruby-example/mruby-example.gem create file: ../mruby-example/.travis.yml create file: ../mruby-example/.travis_build_config.rb create file: ../mruby-example/README.md create file: ../mruby-example/LICENSE > create miyohide/mruby-example repository on github. > turn on Travis CI https://travis-ci.org/profile of miyohide/mruby-example repository. > edit your mruby-example code, then run the following command: cd ../mruby-example git init git add . git commit -m "first commit" git remote add origin git@github.com:miyohide/mruby-example.git git push -u origin master > finally, pull-request mruby-example.gem to mgem-list https://github.com/bovi/mgem-list [~/work/mruby-1.3.0]$
という実行結果が出力され、mrbgem_prefix + / + mrbgem_name なディレクトリが作成されます。
あとは実装とテストを書くだけ
あとは、mrbgem_nameで指定したディレクトリに移動して、CやRubyで実装を書くだけです。mrblibディレクトリやlibディレクトリにそれぞれRubyとCでの実装を書き、testにテストを書くみたいです。
また、作成した直後でも rake test を実行することでテストを動かすことができます。
[~/work/mruby-example]$ rake test
git clone --depth=1 git://github.com/mruby/mruby.git
Cloning into 'mruby'...
(省略)
mruby-example
mruby-bin-mrbc - mruby compiler executable
mruby-test - mruby test
================================================
>>> Test host <<<
(省略)
Total: 959
OK: 959
KO: 0
Crash: 0
Time: 0.1 seconds
[~/work/mruby-example]$