LoginSignup
39
40

More than 5 years have passed since last update.

RubyでオレオレCLIツールを作って大人しめに公開する

Last updated at Posted at 2014-08-22

環境

  • MacOS
  • Ruby 2.0.0p481
  • RubyGems 2.4.1
  • Bundler version 1.6.5

RubyでCLIツールを作りたい!

Rubyで簡単なスクリプトを書いてちょこちょこ使っていたのですが、
毎回そのディレクトリに移動して「ruby script.rb」とかやるのが面倒でした。

色々試行錯誤しながら、こんな書き方をしてみたり。。。

cli.rb
eval "Tools::Cli.#{ARGV[0]}"
ruby cli.rb hello

みたいなね。。。結局rubyコマンド叩いてるからあんま意味無いですね。

結局Google先生に聞いてみたところ、参考になりそうな記事が見つかったので、それを参考にやってみました。

作り方

そもそもbundlerにgemを作るためのコマンドが存在していたようです。
それを実行することでツールのひな形を作成することができます。

bundle gem oreore -b
tree
.
└── oreore
    ├── Gemfile
    ├── LICENSE.txt
    ├── README.md
    ├── Rakefile
    ├── bin
    │   └── oreore
    ├── lib
    │   ├── oreore
    │   │   └── version.rb
    │   └── oreore.rb
    └── oreore.gemspec

4 directories, 8 files

まずは、.gemspecファイルを編集します。
パラメータの解析用にThorを使うよう記述していますが、今回は特に使いません。

oreore.gemspec
Gem::Specification.new do |spec|
  spec.name          = "oreore"
  spec.version       = Oreore::VERSION
  spec.authors       = ["Jacoyutorius"]
  spec.email         = ["jacoyutorius@oreore.jp"]
  spec.summary       = %q{TODO: Write a short summary. Required.}
  spec.description   = %q{TODO: Write a longer description. Optional.}

    ~(省略)~

  spec.add_dependency "thor"
end

summaryやdescriptionにある"TODO"の文字列が残っていると、後にgem installする際にエラーになります。
やる気があるうちにしっかり書いておいたほうが良さげです。

Rebuild.fmでもReadme driven developmentの話題が出てましたしね。

次に実際のコード部分。
lib/oreore以下に実際のコマンドを実装するcli.rbを作成します。

lib/oreore/cli.rb
require "oreore"
require "thor"

module Oreore
  class CLI < Thor 
    desc "hello", "say 'hello world!'."
    def hello
      puts "Hello World!"
    end
  end
end

lib以下のoreore.rbにcli.rbをrequireします。

lib/oreore.rb
require "oreore/version"
require "oreore/cli"

module Oreore
  # Your code goes here...
end
bin/oreore
#!/usr/bin/env ruby
require 'oreore'

Oreore::CLI.start

では実行してみます。

bundle install

chmod 777 bin/oreore
bundle exec bin/oreore hello
=> Hello World!

自分の環境では、「bundler: not executable: bin/oreore」というエラーが出たのでパーミッションを変更しています。

あとはgitにコミットすればOKですね。

git commit -am "initial commit"
git remote add origin git@github.com:jacoyutorius/oreore-cli-tool.git
git push -u origin master

ここで作ったコードは

https://github.com/jacoyutorius/oreore-cli-tool

にあります。

大人しめに公開する(Rubygems.org無視)

gitにコミットしたgemは、Gemfileを作ってbundle installすればいいのですが、これだとGemfileの居るディレクトリでしかツールを使用できません。

Rubygemsに登録すれば良いのですが、オレオレツールを世界規模で公開するのは気が引けます。
社内向けにしか公開したくない場合もありますしね。

そこで、specific_installというgemを使って、gitにあるオレオレcliをグローバルな場所(rbenvを使っていれば~/.rbenv/shims)にインストールします。
こうすることで、どこにいてもオレオレcliのコマンドが実行できます。

gem install specific_install
gem specific_install -l "git@github.com:jacoyutorius/oreore-cli-tool.git"

oreore hello
=> Hello World!

.gemspecをきちんと記述しないとWARNINGがたくさん出てしまいますが、ここはオレオレツールってことで気にしないでおきますね。。。

参考

以上、ドヤ顔で説明しましたが、ほとんど以下のQiita記事を参考にしてます。
大変参考になりました。

39
40
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
39
40