14

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!

Ben
  • 553
  • 1
  • 13
  • 25

4 Answers4

23

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

Community
  • 1
  • 1
Mike Farmer
  • 2,992
  • 4
  • 28
  • 32
9

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.

Community
  • 1
  • 1
Julian Kniephoff
  • 1,003
  • 7
  • 14
  • you can also call invoke with a generator too e.g. invoke 'cucumber:install' ... the advantage of this is that it will correctly *undo* the command when you're trying to rollback the generator (with rails d mygenerator:mytask) .. hope that makes sense! – patrickdavey Dec 16 '12 at 22:42
  • `Rails::Generators.invoke 'active_record:model', ["bar"], :migration => false` ignores the migration false flag or any options. Digging deeper it looks like the invoke method that is called when you just do "invoke ..." is not the same as Rails::Generators.invoke. So if you need to pass in generator options: `Rails::Generators.invoke 'active_record:model', ["bar", "--no-migration" ])` – ErJab Mar 26 '14 at 00:05
1

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.)

Kalsan
  • 822
  • 1
  • 8
  • 19
monocle
  • 5,866
  • 2
  • 26
  • 22
0

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.

thekindofme
  • 3,846
  • 26
  • 29