Class Genosaurus
In: lib/genosaurus.rb
Parent: Object

Methods

Included Modules

FileUtils

Public Class methods

Takes any options needed for this generator. If the generator requires any parameters an ArgumentError exception will be raised if those parameters are found in the options Hash. The setup method is called at the end of the initialization.

[Source]

    # File lib/genosaurus.rb, line 25
25:   def initialize(options = {})
26:     @options = options
27:     self.class.required_params.each do |p|
28:       raise ::ArgumentError.new("The required parameter '#{p.to_s.upcase}' is missing for this generator!") unless param(p)
29:     end
30:     @generator_name = self.class.name
31:     @generator_name_underscore = @generator_name.underscore
32:     @templates_directory_path = nil
33:     @manifest_path = nil
34:     $".each do |f|
35:       if f.match(/#{@generator_name_underscore}\.rb$/)
36:         @templates_directory_path = File.join(File.dirname(f), "templates")
37:         @manifest_path = File.join(File.dirname(f), "manifest.yml")
38:       end
39:     end
40:     setup
41:   end

Used to define arguments that are required by the generator.

[Source]

     # File lib/genosaurus.rb, line 103
103:   def self.require_param(*args)
104:     required_params << args
105:     required_params.flatten!
106:   end

Returns the required_params array.

[Source]

     # File lib/genosaurus.rb, line 109
109:   def self.required_params
110:     @required_params ||= []
111:   end

Instantiates a new Genosaurus, passing the ENV hash as options into it, runs the generate method, and returns the Genosaurus object.

[Source]

    # File lib/genosaurus.rb, line 15
15:     def run(options = ENV.to_hash)
16:       gen = self.new(options)
17:       gen.generate
18:       gen
19:     end

Public Instance methods

To be overridden in subclasses to do work after the generate method is run. This is a simple way to call other generators.

[Source]

    # File lib/genosaurus.rb, line 70
70:   def after_generate
71:   end

To be overridden in subclasses to do work before the generate method is run.

[Source]

    # File lib/genosaurus.rb, line 65
65:   def before_generate
66:   end

Creates the specified directory.

[Source]

     # File lib/genosaurus.rb, line 141
141:   def directory(output_dir, options = @options)
142:     if $genosaurus_output_directory
143:       output_dir = File.join($genosaurus_output_directory, output_dir) 
144:     end
145:     if File.exists?(output_dir)
146:       puts "Exists: #{output_dir}"
147:       return
148:     end
149:     mkdir_p(output_dir)
150:     puts "Created: #{output_dir}"
151:   end

This does the dirty work of generation.

[Source]

     # File lib/genosaurus.rb, line 154
154:   def generate
155:     generate_callbacks do
156:       manifest.each_value do |info|
157:         case info["type"]
158:         when "file"
159:           template(info["template_path"], info["output_path"])
160:         when "directory"
161:           directory(info["output_path"])
162:         else
163:           raise "Unknown 'type': #{info["type"]}!"
164:         end
165:       end
166:     end
167:   end

Returns the manifest for this generator, which is used by the generate method to do the dirty work. If there is a manifest.yml, defined by the manifest_path method, then the contents of that file are processed with ERB and returned. If there is not manifest.yml then an implied manifest is generated from the contents of the templates_directory_path.

[Source]

     # File lib/genosaurus.rb, line 77
 77:   def manifest
 78:     ivar_cache do 
 79:       if File.exists?(manifest_path)
 80:         # run using the yml file
 81:         template = ERB.new(File.open(manifest_path).read, nil, "->")
 82:         man = YAML.load(template.result(binding))
 83:       else
 84:         files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
 85:         man = {}
 86:         files.each_with_index do |f, i|
 87:           output_path = f.gsub(templates_directory_path, "")
 88:           output_path.gsub!(".template", "")
 89:           output_path.gsub!(/^\//, "")
 90:           man["template_#{i+1}"] = {
 91:             "type" => File.directory?(f) ? "directory" : "file",
 92:             "template_path" => f,
 93:             "output_path" => Erubis::Eruby.new(output_path, :pattern => '% %').result(binding)
 94:           }
 95:         end
 96:       end
 97:       # puts man.inspect
 98:       man
 99:     end
100:   end

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 method returns nil, then an implied manifest is used based on the templates_directory_path contents. IMPORTANT: Genosaurus will attempt to find this location automatically, HOWEVER, if there is a problem, or you want to be special, you can override this method in your generator and have it return the correct path.

[Source]

    # File lib/genosaurus.rb, line 55
55:   def manifest_path
56:     @manifest_path
57:   end

[Source]

     # File lib/genosaurus.rb, line 169
169:   def method_missing(sym, *args)
170:     p = param(sym)
171:     return p if p
172:     raise NoMethodError.new(sym)
173:   end

Returns a parameter from the initial Hash of parameters.

[Source]

     # File lib/genosaurus.rb, line 114
114:   def param(key)
115:     (@options[key.to_s.downcase] ||= @options[key.to_s.upcase])
116:   end

To be overridden in subclasses to do any setup work needed by the generator.

[Source]

    # File lib/genosaurus.rb, line 60
60:   def setup
61:     # does nothing, unless overridden in subclass.
62:   end

Takes an input_file runs it through ERB and saves it to the specified output_file. If the output_file exists it will be skipped. If you would like to force the writing of the file, use the :force => true option.

[Source]

     # File lib/genosaurus.rb, line 122
122:   def template(input_file, output_file, options = @options)
123:     if File.exists?(output_file)
124:       unless options[:force]
125:         puts "Skipped: #{output_file}"
126:         return
127:       end
128:     end
129:     # incase the directory doesn't exist, let's create it.
130:     directory(File.dirname(output_file))
131:     # puts "input_file: #{input_file}"
132:     # puts "output_file: #{output_file}"
133:     if $genosaurus_output_directory
134:       output_file = File.join($genosaurus_output_directory, output_file) 
135:     end
136:     File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read, nil, "->").result(binding)}
137:     puts "Wrote: #{output_file}"
138:   end

Returns the path to the templates directory. IMPORTANT: The location of the templates_directory_path is VERY important! Genosaurus will attempt to find this location automatically, HOWEVER, if there is a problem, or you want to be special, you can override this method in your generator and have it return the correct path.

[Source]

    # File lib/genosaurus.rb, line 47
47:   def templates_directory_path
48:     @templates_directory_path
49:   end

[Validate]