Hi I was trying to generate a custom generator, So my here my custom generators is Myinitializer
, which has myinitializer
inside it. But I wanted to have more generators inside it, like the Rails
and TestUnit
has. I went thought https://guides.rubyonrails.org/generators.html but i could not find how to create these sub generators or whatever they are called. I tried creating a new file inside the generated directory (/lib/generator/myinitializer
) but it does not do the sub generator thing.
rails -g
Rails:
application_record
assets
channel
...
system_test
task
ActiveRecord:
active_record:application_record
Myinitializer:
myinitializer
TestUnit:
test_unit:channel
test_unit:generator
test_unit:mailbox
test_unit:plugin
So what i wanted to have something like:
MyInitializer:
myinitializer
anothergeneratorhere
What rmlockerd explained only half answered my question, Here's what worked:
I created the custom generators using rails g generator g1
and rails g generator g2
and organized them into the following directory structure:
# directory: /lib/generators
λ tree
.
└── gorking_generators
├── g1
│ ├── g1_generator.rb
│ ├── templates
│ └── USAGE
└── g2
├── g2_generator.rb
├── templates
└── USAGE
The content of the files are as follows:
# file: g1_generator.rb
module GorkingGenerators
module Generators
class G1Generator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
end
end
end
# file: g2_generator.rb
module GorkingGenerators
module Generators
class G2Generator < Rails::Generators::NamedBase
source_root File.expand_path('templates', __dir__)
end
end
end
After this, I am able to see the generators in place when using rails g
:
GorkingGenerators:
gorking_generators:g1
gorking_generators:g2
I can then use them using:
rails g gorking_generators:g1