gem 中的 Rails 3 生成器

发布于 2024-10-31 08:38:10 字数 628 浏览 3 评论 0原文

听起来可能是一个简单的问题,但我很困惑。

我创建了一个本质上包含生成器的 gem。

它包含以下结构:

lib
 - generators
    - my_generator
          my_generator_generator.rb (see below)
          - templates
             my_template_files...     
 - my_generator.rb (empty file)
test
 -test files
GemFile
etc..

但是,当我将此 Gem 添加到我的 gem 文件并运行 Rails g 时,它没有列出。我需要做任何额外的配置吗?

我的生成器大致是这样的......

class MyGeneratorGenerator < Rails::Generators::NamedBase
      source_root File.expand_path('../templates', __FILE__)
      generator code....
 end

奇怪的是,它在 Cygwin 中工作,但在 Ubuntu 中不起作用......

Might sound like a simple question, but I'm stumped.

I've created a gem that essentially contains a generator.

It contains the following structure:

lib
 - generators
    - my_generator
          my_generator_generator.rb (see below)
          - templates
             my_template_files...     
 - my_generator.rb (empty file)
test
 -test files
GemFile
etc..

However when I add this Gem to my gem file and run rails g, it's not listed. Is there any additional config that I need to do?

My generator roughly looks like this...

class MyGeneratorGenerator < Rails::Generators::NamedBase
      source_root File.expand_path('../templates', __FILE__)
      generator code....
 end

The strange thing is, it works in Cygwin, but not in Ubuntu...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇年纸鸢 2024-11-07 08:38:10

我花了一点时间才弄清楚,但我遇到了同样的问题。这是我修复它的方法。

树结构如下所示:

lib
  - generators
    - gemname
      install_generator.rb
      - templates
        (template files)

install_generator.rb 的代码

#lib/generators/gemname/install_generator.rb
require 'rails/generators'
module Gemname
  class InstallGenerator < Rails::Generators::Base
    desc "Some description of my generator here"

    # Commandline options can be defined here using Thor-like options:
    class_option :my_opt, :type => :boolean, :default => false, :desc => "My Option"

    # I can later access that option using:
    # options[:my_opt]


    def self.source_root
      @source_root ||= File.join(File.dirname(__FILE__), 'templates')
    end

    # Generator Code. Remember this is just suped-up Thor so methods are executed in order


  end
end

这是当我运行时
我看到

Gemname
   gemname:install

您可能需要设置的其他一些东西:

#lib/gemname.rb
module Gemname
  require 'gemname/engine' if defined?(Rails)
  # any additional requires
end

以及

#/lib/gemname/engine.rb
require 'rails'
module Gemname
  class Engine < Rails::Engine
  end
end

我在这方面找到的一些很好的参考资料是:

This took a little bit for me to figure out, but I've run into the same problem. Here is how I fixed it.

Tree structure looks like this:

lib
  - generators
    - gemname
      install_generator.rb
      - templates
        (template files)

Here's the code for install_generator.rb

#lib/generators/gemname/install_generator.rb
require 'rails/generators'
module Gemname
  class InstallGenerator < Rails::Generators::Base
    desc "Some description of my generator here"

    # Commandline options can be defined here using Thor-like options:
    class_option :my_opt, :type => :boolean, :default => false, :desc => "My Option"

    # I can later access that option using:
    # options[:my_opt]


    def self.source_root
      @source_root ||= File.join(File.dirname(__FILE__), 'templates')
    end

    # Generator Code. Remember this is just suped-up Thor so methods are executed in order


  end
end

When I run
rails g

I see:

Gemname
   gemname:install

Some other things you may need to setup:

#lib/gemname.rb
module Gemname
  require 'gemname/engine' if defined?(Rails)
  # any additional requires
end

and

#/lib/gemname/engine.rb
require 'rails'
module Gemname
  class Engine < Rails::Engine
  end
end

Some good references I've found on this are:

蓝礼 2024-11-07 08:38:10

如果您使用 Railtie,您可以在任何可以使用的地方定义生成器:

generators do
    require "path/to/my_railtie_generator"
end

在 Railtie 类中。

If you use Railtie, you can define your generator wherever it could be using:

generators do
    require "path/to/my_railtie_generator"
end

in Railtie class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文