Creating Custom Generators with Sprouts
Posted by Luke Bayes Thu, 21 Feb 2008 18:25:00 GMT
As Sprouts starts to pick up momentum, we’re starting to get questions about creating custom generators. So I thought I’d throw a quick post up to share the info more broadly.
I’m actively working on the ‘developer’ gem so that you will eventually be able to stub new sprout gems for tools, libraries, bundles, tasks and generators. Unfortunately that gem isn’t quite ready yet so for the time being, you’ll have to build your generators manually.
If you’re looking for code generators within the context of an existing project, here’s how to do it:
- Create a new folder in your project called ‘generators’. If you don’t like this location, you can create a new ‘generators’ folder inside the script folder, but I discovered that this starts to conflict with terminal completion when you’re trying to run script/generate. You can also use #{project_name}/vendor/generators or a variety of other locations that I’d rather not describe just yet….
- Find your gem installation folder for sprout-as3-bundle (use the as2-bundle if you’re working in an as2 project). This is where gems puts all of your installed gem sources. Mine is at:
/usr/lib/ruby/user-gems/1.8/gems/sprout-as3-bundle-x.x.x/ - From the gem installer, copy the lib/sprout/generators/class directory out of this location into the generators folder.
- Rename that copied directory and it’s child .rb file to the generator name you’d like. If you’re creating a generator called ‘command’, you should have a project folder set up kind of like:
generators/command generators/command/command_generator.rb generators/command/templates/
Now, edit the command_generator.rb and the associated templates however you wish.
Be sure to update the class name from ClassGenerator to CommandGenerator too.
This will let you create new (and override existing) generators in a specific project, if you’d like to create your own complete project generators, dig into our ‘bundles’ in svn and check out the mxml bundle at trunk/sprouts/bundles/mxml. This is a good example of a bundle that is essentially just a project generator and it should map pretty closely to whatever you’d like to build.
The good news is that our generator implementation is built on top of Rails Generators, so you can learn more about working with generators here: http://wiki.rubyonrails.org/rails/pages/UnderstandingGenerators
If you’re considering building a project generator for a common open source tool set like PureMVC, Cairngorm, or Papervision (to name a few), please let us know as we would love to include it in our official gem listing!












