So this week I took a small break away from Mack to build another library, Genosaurus. I found that in Mack I had basically written a generator system, so I extracted it out into a stand alone library that anybody can use, and I called it Genosaurus. Genosaurus is, in my opinion, an incredibly easy to use generator framework. Let’s be honest, we’ve all basically built a generator system at one point or another, so I’ve just wrapped it up nicely. The next release of Mack, due out the end of this week, will have all it’s generators converted to use Genosaurus. Let’s take a look at a section of the README for Genosaurus:
Implied Manifests
The easiest way to use Genosaurus is to let it do the work for you. Let‘s looked at what‘s called an ‘implied’ manifest:
dir:
simple_generator.rb
templates:
hello_world.txt.template
That‘s our folder structure. Now let‘s look at simple_generator.rb:
require 'rubygems'
require 'genosaurus'
class SimpleGenerator < Genosaurus
end
Now if we run that generator:
$irb: SimpleGenerator.run
We should get a file called hello_world.txt generated in the current directory. Yes, it truly is that simple! With implied manifests our directory structure under ‘templates’ tells the whole story, and Genosaurus is smart enough to figure it out. All the file names, and the same goes for folders, need to end in .template, and Genosaurus will do the rest. All the files will go through ERB before they generated, so you can put all your lovely little dynamic goodies in there. File, and folder, names also get run through ERB so you can even make the file name dynamic too! Let‘s look at a more complex example:
dir:
complex_generator.rb
templates:
app:
views:
<%=param(:name).plural%>.template:
hello_world.html.erb
models:
<%=param(:name)%>.rb.template
Let‘s run our complex_generator.rb file:
require 'rubygems'
require 'genosaurus'
class ComplexGenerator < Genosaurus
require_param: name
end
Now if we run that generator:
$irb: ComplexGenerator.run("name" => "user")
Now you should end up with the following:
app:
views:
users:
hello_world.html.erb
models:
user.rb.template
In the ComplexGenerator we told Genosaurus that we are requiring that the parameter, name, be passed into it. We are then using that parameter to generate the names of some files and folders. Pretty cool, eh? See how simple that is.
Explicit Manifests
Explicit manifests are used when there is a manifest.yml supplied at the same level as the generator. If there is a manifest.yml file then implied manifests are not used. This means you have to define the entire generation process. This is great if you have a pretty complicated generator, as the manifest.yml is also sent through ERB before being loaded. Let‘s look at the manifest.yml file for our simple_generator example:
template_1:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "hello_world.txt.template")
output_path: hello_world.txt
Pretty simple. We give the template a name, template_1, it really doesn‘t matter what it is, but Hash objects need keys. The ‘type’ parameter is either file or directory. The template_path is the path to the template. Finally, the output_path is the where you want the file to be generated. Let‘s look at our more complex example. We can change the directory structure a bit, since we really don‘t need ERB in the file names now:
dir:
complex_generator.rb
templates:
hello_world.html.erb.template
model.rb.template
Our manifest.yml file would look like this:
hello_world_template:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "hello_world.html.erb")
output_path: <%= File.join("app", "views", param(:name).plural, "hello_world.html.erb") %>
model_template:
type: file
template_path: <%= File.join(templates_directory_path, "templates", "model.html.erb")
output_path: <%= File.join("app", "models", "#{param(:name)}.rb") %>
This will generate the exact same thing as our implied manifest.