10
0

More than 3 years have passed since last update.

Thor と Rubocop

Posted at

目次

今回はThorとRubocopなるものについて触れていく

Thor

以前Rakeを使用して見たが,似たようなものとしてThorというものが存在するようだ.これはコマンドラインを作るgemだ.

まずは初期では入っていないので

sudo gem install thor

でインストール.

では実際に使用してみる.

$ bundle gem hello_rudy -b

こうすることで作業しているディレクトリにhello_rudyというものが作成される.その中に入る.

$ cd hello_rudy
$ ls exe/hello_rudy

このままだとexe/hello_rudyに権限がないので与えておく.

$ chmod a+x exe/hello_rudy

次に「hello_rudy.gemspec」というファイルがあるのでそに中身を

require_relative 'lib/hello_rudy/version'

Gem::Specification.new do |spec|
  spec.name          = "hello_rudy"
  spec.version       = HelloRudy::VERSION
  spec.authors       = ["[yours]"]
  spec.email         = ["[yours]"]

  spec.summary       = %q{hello rudy}
  spec.description   = %q{hello rudy}
  spec.homepage      = "https://hoge.hoge"
  spec.license       = "MIT"
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")

#  spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

  spec.metadata["homepage_uri"] = spec.homepage
#  spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
#  spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files         = Dir.chdir(File.expand_path('..', __FILE__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  end                                                                           
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_runtime_dependency('thor')
end 

と書き換える.

その後,#+begin_example$ sudo exec exe/hello_rudy hello rudy#+end_example

を実行.おそらく動く.動かなかったとしても僕は知りません.直してください.

そして,libの中にあるhello_rudy.rbに以下のコードを書く.

hello_rudy.rb
require "thor"
require "hello_rudy/version"

#module HelloRudy
#class Error < StandardError; end

class HelloRudyCLI < Thor
  desc "hello NAME", "say hello to NAME"
  def hello(name)
    puts "Hello " + name
  end
end

HelloRudyCLI.start(ARGV)

また,「lib/hello_rudy/version.rb」の中身を,

version.rb
class HelloRudy
  VERSION = "0.1.0"
end
$ bundle update
$ bundle install
$ bundle exec exe/hello_rudy
Commands:
  hello_rudy hello NAME      # say hello to NAME
  hello_rudy help [COMMAND]  # Describe available commands or one specific co...
$ bundle exec exe/hello_rudy hello rudy
Hello Rudy

これで動くはず.

目次へ

Rubocop

これはコードをフォーマットしてくれるツールだ.コードが複雑になるとあまりよろしくないので警告を出したりしてくれる.

$ sudo gem install rubocop

でインストール後,

rubocop --auto-correct ファイル名

とすることで使用できる.

まとめ

今回はThorとRubocopを導入した.

参考


  • source ~/prog/github/grad_members_20f/members/miyanagi/post/thor.org
10
0
1

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
10
0