简体   繁体   中英

How do I create a command line rails generator gem?

I know how to create a rails generator gem which is called like:

rails g my_generator command

And I know how to create a thor gem which can be called like:

my_generator command

But I don't know how to create a rails generator that can be called using an executable. I have tried by creating a lib/my_generator/cli.rb file like:

require 'thor'

module Mang
  class Cli < Thor
    include ::Rails::Generators::Base

    desc "install_gem", "install a gem"
    def install_gem
      gem 'thor', "0.18.1"
    end

  end
end

But I get the following error despite having added Rails as a dependency in my gemspec.

uninitialized constant Rails (NameError)

The fix was just a matter of including the rails/generators/actions module.

require 'thor'
require 'rails/generators'
require 'rails/generators/actions'

module Mang
  class Cli < Thor
    include Thor::Actions
    include Rails::Generators::Actions

    desc "install_gem", "install a gem"
    def install_gem
      gem 'thor', "0.18.1"
    end

  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM