如何让生成器调用 Rails 3 中的其他生成器
我正在尝试宝石开发,现在特别是发电机。到目前为止,我已经成功创建了两个能够完美完成其工作的生成器。这两个生成器位于同一目录中。
然而,现在我必须分别给他们每个人打电话。
我想做的就是只调用一个生成器,然后让该生成器调用所有其他生成器。只需输入
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在你的生成器中,你可以调用
例如:
顺便说一句,如果你正在使用 Rails 3 gems,这个问题也可以帮助你:
在 gem 中使用 Rails 3 生成器
In your generator, you can just call
for example:
By the way, if you are working on Rails 3 gems, this question can also help out:
Rails 3 generators in gem
另一种可能性是使用
类似于
generate
那样干净的东西,但有一个优点:当你的生成器通过rails destroy
被调用时,这个调用 - 就像其他调用一样Thors 操作 - 将尝试撤销您调用
的生成器的操作。然而,有一个问题:可能是由于 Thors 依赖管理,这对于您想要调用的每个生成器仅有效一次,这意味着同一生成器的第二次
调用
将不会执行任何操作。这可以通过使用类似的语句来避免。在这种情况下,您必须显式传递生成器的
行为
(这是一个返回诸如:invoke
、:revoke
之类的值的方法,并且可能其他的,取决于哪个命令 -railsgenerate
、railsdestroy
、railsupdate
等 - 称为您的生成器)来实现相同的效果结果如上。如果您不这样做,则在使用rails destroy
运行生成器时,使用Rails::Generators.invoke
调用的生成器也会被执行。或者,您可以坚持使用
invoke
并尝试篡改 Thors 调用系统。例如,另请参阅此处。Another possibility is to use something like
which is not as clean as
generate
, but has one advantage: When your generator gets called viarails destroy
, this call -- like may other of Thors actions -- will try to revoke the action of the generator youinvoke
.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 likeinstead. 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 withRails::Generators.invoke
will also be executed when running your generator withrails destroy
.Alternatively you could stick to
invoke
and try to tamper with Thors invocation system. See also here for example.生成器基于 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.)
看看带有导轨的脚手架生成器。
这里脚手架生成器使用模型生成器。所以看一下依赖方法。您可以在此处找到它的 API 文档。
Take a look at the scaffold generator that comes with rails.
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.