0
@@ -4,22 +4,13 @@ require 'fileutils'
0
-# All generator classes should extend this class if they're expected to be used by the rake generate:<generator_name> task.
0
-# A generator must by name in the following style: <name>Generator.
0
-# class MyCoolGenerator < Genosaurus
0
-# require_param :name, :foo
0
-# rake generate:my_cool # => calls MyCoolGenerator
0
+ # Instantiates a new Genosaurus, passing the ENV hash as options into it, runs the generate method, and returns the Genosaurus object.
0
def run(options = ENV.to_hash)
0
gen = self.new(options)
0
@@ -28,6 +19,8 @@ class Genosaurus
0
+ # Takes any options needed for this generator. If the generator requires any parameters an ArgumentError exception will be
0
+ # raised if those parameters are found in the options Hash. The setup method is called at the end of the initialization.
0
def initialize(options = {})
0
self.class.required_params.each do |p|
0
@@ -35,47 +28,62 @@ class Genosaurus
0
@generator_name = self.class.name
0
@generator_name_underscore = @generator_name.underscore
0
- @generator_file_path = nil
0
- @generator_directory_path = nil
0
+ @templates_directory_path = nil
0
if f.match(/#{@generator_name_underscore}\.rb$/)
0
- @generator_file_path = f
0
- @generator_directory_path = File.dirname(@generator_file_path)
0
+ @templates_directory_path = File.join(File.dirname(f), "templates")
0
+ @manifest_path = File.join(File.dirname(f), "manifest.yml")
0
- def generator_file_path
0
+ # Returns the path to the templates directory.
0
+ # IMPORTANT: The location of the templates_directory_path is VERY important! Genosaurus will attempt to find this location
0
+ # automatically, HOWEVER, if there is a problem, or you want to be special, you can override this method in your generator
0
+ # and have it return the correct path.
0
+ def templates_directory_path
0
+ @templates_directory_path
0
- def generator_directory_path
0
- @generator_directory_path
0
+ # Returns the path to the manifest.yml. This is only used if you have a manifest.yml file, if there is no file, or this
0
+ # method returns nil, then an implied manifest is used based on the templates_directory_path contents.
0
+ # IMPORTANT: Genosaurus will attempt to find this location automatically, HOWEVER, if there is a problem, or you want to
0
+ # be special, you can override this method in your generator and have it return the correct path.
0
+ # To be overridden in subclasses to do any setup work needed by the generator.
0
# does nothing, unless overridden in subclass.
0
+ # To be overridden in subclasses to do work before the generate method is run.
0
+ # To be overridden in subclasses to do work after the generate method is run.
0
+ # This is a simple way to call other generators.
0
+ # Returns the manifest for this generator, which is used by the generate method to do the dirty work.
0
+ # If there is a manifest.yml, defined by the manifest_path method, then the contents of that file are processed
0
+ # with ERB and returned. If there is not manifest.yml then an implied manifest is generated from the contents
0
+ # of the templates_directory_path.
0
- yml = File.join(generator_directory_path, "manifest.yml")
0
+ if File.exists?(manifest_path)
0
# run using the yml file
0
- template = ERB.new(File.open(
yml).read, nil, "->")
0
+ template = ERB.new(File.open(
manifest_path).read, nil, "->")
0
man = YAML.load(template.result(binding))
0
- files = Dir.glob(File.join(
generator_directory_path, "**/*.template"))
0
+ files = Dir.glob(File.join(
templates_directory_path, "**/*.template"))
0
files.each_with_index do |f, i|
0
- output_path = f.gsub(
File.join(generator_directory_path, "templates"), "")
0
+ output_path = f.gsub(
templates_directory_path, "")
0
output_path.gsub!(".template", "")
0
output_path.gsub!(/^\//, "")
0
man["template_#{i+1}"] = {
0
@@ -141,7 +149,8 @@ class Genosaurus
0
puts "Created: #{output_dir}"
0
+ # This does the dirty work of generation.
0
manifest.each_value do |info|
Comments
No one has commented yet.