rubigen

Heard on twitter ('rubigen')...

Ruby Generator Framework

What

A framework to allow Ruby applications to generate file/folder stubs (like the rails command does for Ruby on Rails, and the ‘script/generate’ command within a Rails application during development).

The RubyConf 2007 presentation is now online together with the theme from the A-Team.

Background

RubiGen is originally extracted from Ruby on Rails (specifically the rails_generator from its railties gem).

The rails_generator was hardcoded with Rails-specific dependencies (RAILS_ROOT), Rails generators (‘app’ = Rails application; ‘model’ = Rails model+tests+migration), and generally assumed it was the only generator framework within the Ruby world (it was). So, any RubyGem whose name ended with ‘_generator’ was assumed to be a generator for a Rails application.

But if you are developing an Adhearsion application, then you may want a different set of generators.
If you are developing a RubyGem, then you will want a different set of generators.

RubiGen exists to give different development environments their own generator framework.

Installing

RubiGen is only required at development time, and normally isn’t required at deployment time (unless your application uses it to generate files etc for its users).

On your development machine:

sudogeminstallrubigen

Usage

RubiGen will be normally integrated into another RubyGem, such as newgem or rails or camping, rather than be used on its own.

These frameworks might use RubiGen for two reasons:

To generate an initial stub for developers, e.g. rails generated a stub to write a Rails application. newgem generates a stub to write a RubyGem. BTW – RubiGen has a builtin application ruby_app which generates a bare-bones Ruby application stub (lib, test, and script folders, plus a Rakefile, and a script/generate script)

To generate components within their development areas, e.g. Rails had its script/generate script within each Rails application, which hooked back into the rails_generator to lookup and execute generators.

So, there are two steps to integrating RubiGen into your framework:

Use it to generate an initial stub for the developers of your framework. This would create the folders
(lib/app, test, script, doc, log, etc) and starting files (Rakefile, README.txt, test/test_helper.rb etc). Importantly, it would generate a script/generate file.
The script/generate file (example below) will allow developers of your framework to
generate components/extensions within the framework.
RubiGen allows you to restrict which generators are available. For example, within
RubyGem development environment (as generated by newgem), the script/generator
only shows rubygem-related generators. Rails could restrict script/generator
to only show Rails related generators

Your framework RubyGem (e.g. newgem or rails RubyGems) needs to add rubigen as a
dependency, so that users of your RubyGem can access the generator framework.

Creating generators

There are two types of generators:

Application Generators – used by developers of your framework to get started.
Generally, you will create one Application Generator for your framework.
It generates a base stub (such as the rails stub for new Rails applications)
for your framework users.

Component Generators – used by developers to extend their application.
You may include 1+ built-in generators with your framework.
Developers can also write generators for your framework, and like Rails’ generator
install them in various places and have access to their via RubiGen.

Creating an Application Generator for your Framework

Easy way

newgem (v0.13.0+) can generate an Application Generator
for a RubyGem.

Create new RubyGem: newgem foobar

Create generator: script/generator application_generator foobar

Update tests + generator

Install

Run with: foobar

For more documentation, run script/generator application_generator

DIY

Without RubiGen, to give your users a head start and create a stub for them, you will
copiously use mkdir_p and File.open. Your script will either be primitive (only
create the bare folders and very few files) or it will be very long and unreadable
(ok, perhaps I’m just talking about the newgem script, which I am dubiously responsible
for… :P).

With RubiGen, you can create stubs using powerful, yet simple, syntax. Templates are
extracted into a templates folder, and activating the generator from a script requires
only a few lines of code.

You can copy and paste this for your own generator script, and place it in your RubyGem’s bin folder.
Change newgem to your RubyGem’s name in the script above (and in all the folders listed above too)

NOTE: If you leave newgem there, then it will execute the newgem_generator.rb generator;
as the generators are loaded from all RubyGem’s having /app_generators folders.

So, for your RubyGem, you need to keep the /app_generators folder (as you are creating an
Application Generator, not a Component Generator), but change newgem to your gem name in
all the subfolders and files. ESPECIALLYnewgem_generator.rb → yourgem_generator.rb,
as this is how the generator is discovered (via RubiGen::Base.use_application_sources!).

All the generator work is performed within yourgem_generator.rb. A stub for it will be: