[RUBY] Thor & Rubocop

Document Links

-Summary of multi-scale simulation -Chart type ruby-appendix-VI (thor, rubocop)

Thor

Basic knowledge of Thor

Thor seems to be a Gem that supports the creation of CLI.

Learn more while actually handling it.

Also, with the help of volunteers

-Introduction to Thor --From Rake to Thor- --About the basics of thor gem to help you create Ruby CLI tools.

Make your own CLI

First, install thor.

$ sudo gem install thor

I will actually create it,

-Chart type ruby-appendix-III (bundler)

Please install the bundler you studied in.

Now, practice! Before that, the workshop this time seems to be troublesome with the usual grad ~/menber/[yours](I don't know the reason), so please go to the Home Directory.

$ pwd

/ home/username # Start working in your home directory! $ bundle gem hello_rudy -b #gem hello_rudy is created $ cd hello_rudy Check if there is a $ ls exe/#exe file hello_rudy $ chmod a + x exe/hello_rudy # Grant permissions

It will not work as it is, so we will make it possible to actually execute it.

$ emacs -nw hello_rudy.gemspec

So, what's inside

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 

Please correct it like this.

$ bundle exec exe/hello_rudy hello rudy

It's okay if you don't get any error with this. If you get an error, see the Error statement and correct it.

Next, I will write the contents of the behavior.

$ emacs -nw lib/hello_rudy.rb

So, what's inside

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)

Please.

Also,

$ emacs -nw lib/hello_rudy/version.rb

The contents

version.rb


class HelloRudy
  VERSION = "0.1.0"
end

Please.

$ 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

Complete!

Since it is a good idea, we will make it possible to execute it from anywhere.

$ sudo rake install:local
$ hello_rudy hello boy
Hello boy

Now you can run hello \ _rudy from anywhere.

Rubocop

Basic knowledge of Rubocop

It seems to be a Gem that checks whether it conforms to the coding standard.

This is also studied with the help of volunteers.

-RuboCop is what? -About Rubocop

Practice

I will actually use it.

Install

$ sudo gem install rubocop

This time I use & # x2013; auto-correct.

$ rubocop --auto-correct file.rb

And it automatically organizes the Code.

See for yourself what happens.

Tightening

This time I learned about Thor and Rubocop.


Recommended Posts

Thor & Rubocop
13th (thor, rubocop)
Chart type ruby-appendix-VI (thor, rubocop)
RuboCop settings
[Week 12-13] thor, robocop
Ruby setting 3 Rubocop