如何创建命令行rails generator gem?

时间:2014-02-05 20:31:34

标签: ruby-on-rails ruby-on-rails-4 thor

我知道如何创建一个名为:

的rails生成器gem
rails g my_generator command

我知道如何创建一个可以被称为:

的thor宝石
my_generator command

但我不知道如何创建可以使用可执行文件调用的rails生成器。我试过创建一个lib / my_generator / cli.rb文件,如:

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

但是,尽管在我的gemspec中添加了Rails作为依赖项,但我得到了以下错误。

uninitialized constant Rails (NameError)

1 个答案:

答案 0 :(得分:0)

修复只是包含rails / generators / actions模块。

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