Matthewの備忘録

忘れたときはここを見ろ。何か書いてある。

RubyでCLIコマンド作成 その1

 某プロジェクトでRailsアプリサイトを作る前にRubyに慣れるために既存のソフトウェアのRuby版を作ることにした。とりあず、次のサイトを参考にして、「サクッ」とスケルトンぐらいはつくることにした。
qiita.com
www.mk-mode.com
morizyun.github.io


bundlerのインストール

 rbenvを使って、利用するRubyのバージョンを選択できるようにはしていたが、bundlerをインストールしていなかった。rbenvでRubyをインストールしているため、それぞれのバージョンごとにRubyGemsが管理されているようだが、今回利用する2.2.4(heroku向け)にbundlerをインストールしておく。次のサイトを参考にした。
橋本商会 » Ruby書くならBundler使え
アップデートをコマンドを実行すると次のようにRubyGemsのインストール先が確認できる。

$ gem update --system
・・・
RubyGems installed the following executables:
	$HOME/.rbenv/versions/2.2.4/bin/gem
・・・
$ gem -v
2.6.8

bundlerをインストール

% gem install bundler

Bundler 1.13.6をインストールできた。

ケルトンの生成

 "-b"オプションでbin/campus_codeというバイナリ(コマンド)を生成する手配まで指定して実行してみた。このオプションはコマンド実行後に作成されるexeディレクトリに実行可能コマンドを生成する。テストはminitest、ライセンスはMITライセンス、よくわからないハラスメント対策のためのものを全て選んでいる。

$ bundle gem campus_code -b
Creating gem 'campus_code'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): minitest
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at http://choosealicense.com/licenses/mit. y/(n): y
MIT License enabled in config
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see http://bit.ly/coc-enforcement. y/(n): y
Code of conduct enabled in config
      create  campus_code/Gemfile
      create  campus_code/.gitignore
      create  campus_code/lib/campus_code.rb
      create  campus_code/lib/campus_code/version.rb
      create  campus_code/campus_code.gemspec
      create  campus_code/Rakefile
      create  campus_code/README.md
      create  campus_code/bin/console
      create  campus_code/bin/setup
      create  campus_code/.travis.yml
      create  campus_code/test/test_helper.rb
      create  campus_code/test/campus_code_test.rb
      create  campus_code/LICENSE.txt
      create  campus_code/CODE_OF_CONDUCT.md
      create  campus_code/exe/campus_code
Initializing git repo in $HOME/...hogehoge.../campus_code
$ 

大掛りになってしまった。

ケルトンのコンフィギュレーション

 よくわからないハラスメント対策のためのものを全て選ぶとおそらくCODE_OF_CONDUCT.mdのスケルトンが生成されているはずだが、削除した。

 上記の三つの質問は初めてbundle gemを実行するときに尋ねてくるようだ。二回目の実行時には尋ねられない。設定は$HOME/.bundle/configに格納されている。bundle configコマンドで設定状態を確認したり、変更したりできる。次の例はgem.cocの設定値をtrueからfalseに変更し、CODE_OF_CONDUCT.mdのスケルトンを吐かなくさせてみた例である。

$ bundle config
Settings are listed in order of priority. The top value will be used.
gem.test
Set for the current user ($HOME/.bundle/config): "minitest"

gem.mit
Set for the current user ($HOME/.bundle/config): "true"

gem.coc
Set for the current user ($HOME/.bundle/config): "true"

$ bundle config gem.coc false
You are replacing the current global value of gem.coc, which is currently "true"
matthew@LUKE:rb_version$ bundle config
Settings are listed in order of priority. The top value will be used.
gem.test
Set for the current user ($HOME/.bundle/config): "minitest"

gem.mit
Set for the current user ($HOME/.bundle/config): "true"

gem.coc
Set for the current user ($HOME/.bundle/config): "false"

$ 

 testはminitestを指定してしまったがrspecでもテストスイートのスケルトンなし(none)でも次のようにデフォルトを指定できる。

$ bundle config gem.test none

これを実行すると、minitestから切り替えた場合は、test/test_helper.rb、test/campus_code_test.rb、およびtestディレクトリが、rspecから切り替えた場合は、.rspec、spec/sepc_helper.rb、spec/campus_code_spec.rb、およびspedディレクトリが作成されなくなる。デフォルトをnoneの指定なしにしておいて、bundle gemでgemを作る際にテストを次のように指定するのがよいかもしれない。

$ bundle gem gemname_hogehoge -b --test=TEST_FRAME_WORK_NAME

example$ bundle gem gemname_hogehoge -b --test=minitest
example$ bundle gem gemname_hogehoge -b --test=rspec

 オプションについて知るにはbundle help gemを実行してマニュアルを読むとよい。

gitリポジトリにスケルトンを追加

 bundle gemコマンドでスケルトンを生成すると、gitリポジトリに対してステージング状態、つまり生成したスケルトン(ファイル)をaddした状態にされる。次のコマンド三つを実行して、ローカルリポジトリに対してコミット、リモートのリポジトリ(大抵はgithub.comだろうが)の決定、リポジトリにファイルが登録が行う。

$ git commit -m "first commit"
$ git remote add origin https://github.com/ユーザー名/sample.git
$ git push -u origin master

organaizationで既にリポジトリにファイルがある場合

 organaization内でリポジトリが作られ、さらにREADME.mdやLICENSEなどのファイルがある場合、条件によって違うだろうが、gitで作ったREADME.mdファイル、MITライセンスのLICENSEがある場合を考える。次のようにすればよい。

$ git rm --cache READEME.md LICENSE.txt
$ mv README.md ../どこかのフォルダへ退避
$ mv LICENSE.txt ../どこかのフォルダへ退避 (または削除してもよい)
$ git remote add origin git@github.com:[organization name]/[repository name].git
$ git pull origin master (これでorigin/masterのREADEM.mdとLICENSEをマージする)
$ git mv LICENSE LICENSE.txt (bundle gemが生成したライセンスは拡張子が.txtであったので)
$ git commit -m "generated gem skelton"
$ git push origin master:master