如何让生成器调用 Rails 3 中的其他生成器

发布于 2024-11-05 00:08:26 字数 254 浏览 2 评论 0原文

我正在尝试宝石开发,现在特别是发电机。到目前为止,我已经成功创建了两个能够完美完成其工作的生成器。这两个生成器位于同一目录中。

然而,现在我必须分别给他们每个人打电话。

我想做的就是只调用一个生成器,然后让该生成器调用所有其他生成器。只需输入

rails g generator_name

即可调用 x 个其他生成器。

有谁知道我该怎么做?

非常感谢帮助,谢谢!

I am experimenting with gem development, right now specifically generators. So far I have successfully created two generators that do their job just perfectly. These two generators are in the same directory.

However, right now I have to call each of them separately.

What I'd like to do is just call one generator and have that generator call all the other ones. Just would type

rails g generator_name

and this would call x other generators.

Does anyone know how would I got about doing this?

Help is much appreciated, thanks!

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

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

发布评论

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

评论(4

落在眉间の轻吻 2024-11-12 00:08:26

在你的生成器中,你可以调用

generate "some:generator" # can be anything listed by 'rails g'

例如:

module MyGem
  class InstallGenerator < Rails::Generators::Base

    def run_other_generators
      generate "jquery:install" # or whatever you want here
    end

  end
end

顺便说一句,如果你正在使用 Rails 3 gems,这个问题也可以帮助你:

在 gem 中使用 Rails 3 生成器

In your generator, you can just call

generate "some:generator" # can be anything listed by 'rails g'

for example:

module MyGem
  class InstallGenerator < Rails::Generators::Base

    def run_other_generators
      generate "jquery:install" # or whatever you want here
    end

  end
end

By the way, if you are working on Rails 3 gems, this question can also help out:

Rails 3 generators in gem

月亮是我掰弯的 2024-11-12 00:08:26

另一种可能性是使用

invoke 'active_record:model', 'foo bar:string baz:float'

类似于 generate 那样干净的东西,但有一个优点:当你的生成器通过 rails destroy 被调用时,这个调用 - 就像其他调用一样Thors 操作 - 将尝试撤销您调用的生​​成器的操作。

然而,有一个问题:可能是由于 Thors 依赖管理,这对于您想要调用的每个生成器仅有效一次,这意味着同一生成器的第二次调用将不会执行任何操作。这可以通过使用类似的语句来避免

Rails::Generators.invoke 'active_record:model', '...', behavior: behavior

。在这种情况下,您必须显式传递生成器的行为(这是一个返回诸如:invoke:revoke之类的值的方法,并且可能其他的,取决于哪个命令 - railsgeneraterailsdestroyrailsupdate 等 - 称为您的生成器)来实现相同的效果结果如上。如果您不这样做,则在使用 rails destroy 运行生成器时,使用 Rails::Generators.invoke 调用的生成器也会被执行。

或者,您可以坚持使用 invoke 并尝试篡改 Thors 调用系统。例如,另请参阅此处

Another possibility is to use something like

invoke 'active_record:model', 'foo bar:string baz:float'

which is not as clean as generate, but has one advantage: When your generator gets called via rails destroy, this call -- like may other of Thors actions -- will try to revoke the action of the generator you invoke.

There's a catch however: Probably due to Thors dependency management, this only works once per generator you want to call, meaning that a second invoke of the same generator will do nothing. This can be circumvented by using a statement like

Rails::Generators.invoke 'active_record:model', '...', behavior: behavior

instead. In this case you have to explicitly pass through the behavior of your generator (which is a method returning values like :invoke, :revoke and possibly others, depending on which command -- rails generate, rails destroy, rails update, etc. -- called your generator) to achieve the same result as above. If you don't do this, the generator you call with Rails::Generators.invoke will also be executed when running your generator with rails destroy.

Alternatively you could stick to invoke and try to tamper with Thors invocation system. See also here for example.

土豪 2024-11-12 00:08:26

生成器基于 Thor,因此您可以使用 apply 方法。

这就是 Rails Templater gem 的作用。 (这里介绍了 Rails Templater gem。)

Generators are based off of Thor, so you can use the apply method.

This is what the Rails Templater gem does. (Here's a walk through the Rails Templater gem.)

青柠芒果 2024-11-12 00:08:26

看看带有导轨的脚手架生成器。

/Users/XYZ/sources/rails/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb

def manifest
    record do |m|
      #....rest of the source is removed for brevity....
      m.dependency 'model', [name] + @args, :collision => :skip
    end
  end

这里脚手架生成器使用模型生成器。所以看一下依赖方法。您可以在此处找到它的 API 文档。

Take a look at the scaffold generator that comes with rails.

/Users/XYZ/sources/rails/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb

def manifest
    record do |m|
      #....rest of the source is removed for brevity....
      m.dependency 'model', [name] + @args, :collision => :skip
    end
  end

Here the scaffold generator is using the model generator. So take a look at the dependency method. You can find the API docs for it over here.

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