简体   繁体   English

如何创建命令行rails generator gem?

[英]How do I create a command line rails generator gem?

I know how to create a rails generator gem which is called like: 我知道如何创建一个名为rails generator的宝石:

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. 但是我不知道如何创建可以使用可执行文件调用的rails生成器。 I have tried by creating a lib/my_generator/cli.rb file like: 我尝试通过创建一个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

But I get the following error despite having added Rails as a dependency in my gemspec. 但是,尽管我在gemspec中添加了Rails作为依赖项,但仍收到以下错误。

uninitialized constant Rails (NameError)

The fix was just a matter of including the rails/generators/actions module. 修复仅是包含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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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