Dr Nic

NewGem Generator – now with script/generate

The New Gem Generator (0.13.0)’s newgem command now behaves like the rails command thanks to RubiGen – a new project that is an extraction of the rails_generator.

Developing a RubyGem? You can now have a script/generate command, just like Rails. Other projects, say like Merb, can do this too.

Awesomeness with a Gold Star. (disclosure: Dr Nic awarded this Gold Star to himself)


This article can get confusing.

Especially around the heading “Generating Generators”.

Plus, this article talks about two projects – an update to New Gem Generator (0.13.0) and the new RubiGen project.

So, give yourself time to digest it all, and then perhaps come back and read it again.

And then perhaps re-write the article for me.

“A generator that can provide generators to your projects, so that you can write generators for your other projects.”

Re-write it – I dare you.


One of the killer features of Rails is the Rails Generator.

It does three things:

  • generates an entire scaffold for your application, thus sharing with you its conventions (over configuration) methodology; and
  • it then allows you to generate more stuff, like models, controllers, plugins etc. Things that are relevant to a Rails app. Finally,
  • it allows you to write your own generators for Rails apps.

Now you can do all this with New Gem.

Killer Feature #1 – upgrade path

Never before have you been able to run ‘newgem’ on top of an existing RubyGem. Why? It used to blow away the entire folder, and then start writing new files… not very friendly.

Now, just like the rails command, you can go into your RubyGem development directory, and run newgem . and you will be asked which files to override.

NOTE: Copy your Rakefile to Rakefile.old, and after running newgem . copy the configuration information into the new file config/hoe.rb.

If you’re using newgem already, I dare you to upgrade as above. (Ok, perhaps make a backup copy of your work first…)

Killer Feature #2 – script/generate

Once you’ve upgraded, or create a new RubyGem (using newgem gemname), you’ll now have two scripts: script/generate and script/destroy, just like Rails has. (and matching generate.cmd and destroy.cmd for Windows users)

Try them out:

Installed Generators
  Rubygems: application_generator, component_generator, executable, install_jruby, install_rspec, install_website
  Builtin: test_unit

Oh yeah, I’ve had some fun extracting things into generators.

  • Install the dubious-looking NewGem website – script/generate install_website.
  • Install RSpec support – script/generate install_rspec.
  • Make the RubyGem a JRuby gem – script/generate install_jruby (the generated gem will have -jruby.gem in its name)
  • Create an executable Ruby app – script/generate executable appname

These generators are also reused via the newgem commands various options (Run newgem to see them.)

Killer Feature #3 – generate generators

I’ll post more about RubiGen later, but you can create a new rails-like command-line app that generates a whole stack load of directories and files using script/generate application_generator appname.

There is a large USAGE rundown if you run script/generate application_generator.

Want to create your own generators for developing RubyGems? (similar to creating a generator for a Rails app, but for RubyGems)

script/generate component_generator foobar rubygems
    create  rubygems_generators/foobar/templates
    exists  test
    create  rubygems_generators/foobar/foobar_generator.rb
    create  test/test_foobar_generator.rb
    create  test/test_generator_helper.rb
    create  rubygems_generators/foobar/USAGE
    readme  readme

Firstly, note test/test_generatorname_generator.rb – that’s right, you get a test stub for your new generator. Start there, write tests, then write your generator. There’s inline help for useful assertions.

Secondly, note rubygems_generators folder. This folder is the “scope” of the generator. As it starts with “rubygems” the generator will only be available when you are developing rubygems. It will not show up in Rails, nor Merb or Camping or any other place that may support RubiGen one day.

Similarly, if you want to write a Rails generator using the component_generator then specify the scope as rails.

script/generate component_generator booya rails
     create  rails_generators/booya/templates
     exists  test
     create  rails_generators/booya/booya_generator.rb
     create  test/test_booya_generator.rb
  identical  test/test_generator_helper.rb
     create  rails_generators/booya/USAGE
     readme  readme

In Edge Rails (and any Rails version after 1.2.3), the script/generate command in Rails will search all RubyGems for /rails_generator/* folders in addition to the existing search paths (ticket).

Thanks goes to Rails Generator

I’ve long been in love with the Rails Generator for what it does, the beautiful syntax for specifying a generator, etc. I don’t know who wrote what bit, e.g. if DHH wrote all of it, or others wrote nice chunks, but its awesome. Thanks DHH and co. Thanks to Jeremy Kemper

RubiGen is 95% Rails Generator code, with extensions to support scoping. Setting Rails Generator free of its Rails constraints is a tribute to it.

Now RubyGems can use generators, and any other frameworks can integrate generators and their developers can write and distribute additional generators for those frameworks.

I award myself 2 gold stars, and a scratch-n-sniff

Related posts:

  1. newgem 1.0.0 all thanks to Cucumber The New Gem Generator (newgem) was exciting, moderately revolutionary, and...
  2. Writing C extensions in RubyGems using newgem generators (plus a free TextMate bundle) Already know C extensions in RubyGems? Cool – then just...
  3. One-stop JavaScript Unit Testing for Rails 2.0 Previously, I mentioned a multi-step/multi-project solution to doing JavaScript Unit...
  4. newjs = newgem for JavaScript projects; free TDD suite Want to start a new JavaScript project for a library...
  5. Merb 0.4 – Installing Edge Merb and using new Generators Merb 0.4 will include generators (via RubiGen) to make it...

11 Responses to “NewGem Generator – now with script/generate”

  1. lazyatom says:

    I think you might’ve just gone nuts. Award yourself another couple of gold stars! :D

  2. Dr Nic says:

    Reported errors: rubigen failing to install ri/rdocs

    You can install rubigen without documentation:

    gem install rubigen --no-ri --no-rdoc

    Future release will fix this issue.

  3. Jeremy Kemper wrote the generator framework.

  4. Dr Nic says:

    @Marcel Molina [via] – thx, updated.

  5. Dr Nic says:

    There is now a patch for Merb to integrate RubiGen into its merb -g command, and for resulting apps to include script/generate+destroy commands.

  6. [...] NewGem Generator – now with script/generate – A generator that can provide generators to your projects, so that you can write generators for your other projects. [...]

  7. Pei Mei says:

    Wow! This is some really cool stuff Nic. I was going to write about creating gems next week, and now I have to include using newgem, thus saving me from having to illustrate another directory structure and a lot of mkdirs.

    I’ll be digging deeper into what this can do for the article.

    Hope you enjoyed Amsterdam! I lived there for 3 years and just returned two months ago.

  8. Dr Nic says:

    @Pei Mei [via] – yeah, I’m glad to get rid of an unmanagable list of mkdirs and File.open(“…”, “w”) in the old newgem script. Now its all grown up :)

  9. [...] newgem: Newgem is another excellent gem by Dr. Nic. It is used for creating gems. You can find more information about it at Dr Nic » NewGem Generator – now with script/generate. [...]

  10. [...] could also write a complex generator for more elaborated [...]

  11. [...] Dr. Nic Williams – Dr. Nic’s presentation is a little easier to sum up but at the same time I can’t really do it justice. Dr. Nic is one of those speakers that if you ever have a chance to see him speak you should definitely take the opportunity. He is hilarious and has a good message. His talk was all about how to contribute back.. how to get involved.. how to participate. Make the future you proud of the you now. Dr. Nic also talked about newgem [...]