Generator Extensions¶ ↑
Rails gem that adds some useful commands to Generators
Install¶ ↑
gem install generator_extensions --source http://gemcutter.org
Usage¶ ↑
I was bothered by the lack of a recursive directory copy in the Generator commands. As it stands, you can copy individual files like so:
m.file 'layout.html.erb', 'app/views/layouts/application.html.erb'
You can create a directory like this:
m.directory 'public/stylesheets/blueprint'
But you can’t copy an entire directory tree over like this:
m.directory_copy 'blueprint', 'public/stylesheets/blueprint'
Well, with this little gem you can. But wait, there’s more! One fairly common pattern is a straight copy of files without any fancy template work. This gem makes it a cinch. Just create a directory inside your template directory containing a partially duplicated Rails directory structure (obviously, only include the directories you need):
your_generator.rb -templates -mirror -public -stylesheets your_stylesheet.css -app -controllers your_controller.rb
Then put this in your manifest definition:
m.mirror 'name-of-directory-to-mirror'
As an added bonus, if you name your directory ‘mirror’, you can just do this:
m.mirror
There’s also a convenience function for adding a whole chunk of code to routes.rb. Let’s say you set it up like this in your initialize() method:
@route_text = <<END_OF_ROUTES map.root :controller => 'contig' [:find, :graph, :list, :add, :remove].each do |route| map.named_route route.to_s, '/%s' % route.to_s, :controller => 'contig', :action => route.to_s end map.showclique '/clique', :controller => 'contig', :action => 'showclique' map.clearcart '/clearcart', :controller => 'contig', :action => 'clearcart' map.findclique '/clique/find', :controller => 'contig', :action => 'findclique' END_OF_ROUTES
Well, then just call this in your manifest:
m.add_to_routes @route_text
In 0.3.0, I’ve also added a similar function for the initializer in config/environment.rb
, for those things (like config.gem
) that don’t go in a file in initializers/
m.add_to_initializers "config.gem 'foo'\nconfig.gem 'bar'"
They’re both implemented using a generic add_to_file()
, which you use like this:
m.add_to_file "filename", "sentinel", "text to insert"
The sentinel is the text immediately preceding the insert.
One problem with mirroring a directory tree is that at the moment there is no way to distinguish between existing directories (say, the ones rails sets up) and any new ones you’ve implicitly created by including them in the mirrored directory tree in your ‘templates’ directory. So when you run the following:
script/destroy your_generator
it will delete any files you’ve copied over as part of an “m.mirror” command, but none of the new directories.