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

Methods

Included Modules

FileUtils

Public Class methods

Describes the generator.

[Source]

    # File lib/genosaurus.rb, line 32
32:     def describe
33:       text = ["#{self}:"]
34:       required_params.each do |p|
35:         text << "Required Parameter: '#{p.to_s.downcase}'"
36:       end
37:       dd = description_detail
38:       unless dd.nil? || dd == ''
39:         text << "---------------"
40:         text << dd
41:       end
42:       text.join("\n")
43:     end

Override this method in your generator to append to the describe method.

[Source]

    # File lib/genosaurus.rb, line 46
46:     def description_detail
47:       ''
48:     end

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 54
54:   def initialize(options = {})
55:     unless options.is_a?(Hash)
56:       opts = [options].flatten
57:       options = {}
58:       self.class.required_params.each_with_index do |p, i|
59:         options[p.to_s] = opts[i]
60:       end
61:     end
62:     @options = options
63:     self.class.required_params.each do |p|
64:       raise ::ArgumentError.new("The required parameter '#{p.to_s.upcase}' is missing for this generator!") unless param(p)
65:     end
66:     @generator_name = self.class.name
67:     @generator_name_underscore = @generator_name.underscore #String::Style.underscore(@generator_name)#.underscore
68:     @templates_directory_path = nil
69:     @manifest_path = nil
70:     $".each do |f|
71:       if f.match(/#{@generator_name_underscore}\.rb$/)
72:         @templates_directory_path = File.join(File.dirname(f), "templates")
73:         @manifest_path = File.join(File.dirname(f), "manifest.yml")
74:       end
75:     end
76:     setup
77:   end

Used to define arguments that are required by the generator.

[Source]

     # File lib/genosaurus.rb, line 140
140:   def self.require_param(*args)
141:     required_params << args
142:     required_params.flatten!
143:   end

Returns the required_params array.

[Source]

     # File lib/genosaurus.rb, line 146
146:   def self.required_params
147:     @required_params ||= []
148:   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 25
25:     def run(options = ENV.to_hash)
26:       gen = self.new(options)
27:       gen.generate
28:       gen
29:     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 106
106:   def after_generate
107:   end

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

[Source]

     # File lib/genosaurus.rb, line 101
101:   def before_generate
102:   end

[Source]

     # File lib/genosaurus.rb, line 181
181:   def copy(input_file, output_file, options = @options)
182:     output_file = template_copy_common(output_file, options)
183:     FileUtils.cp(input_file, output_file)
184:     puts "Copied: #{output_file}"
185:   end

Creates the specified directory.

[Source]

     # File lib/genosaurus.rb, line 169
169:   def directory(output_dir, options = @options)
170:     if $genosaurus_output_directory
171:       output_dir = File.join($genosaurus_output_directory, output_dir) 
172:     end
173:     if File.exists?(output_dir)
174:       puts "Exists: #{output_dir}"
175:       return
176:     end
177:     mkdir_p(output_dir)
178:     puts "Created: #{output_dir}"
179:   end

This does the dirty work of generation.

[Source]

     # File lib/genosaurus.rb, line 188
188:   def generate
189:     generate_callbacks do
190:       manifest.each_value do |info|
191:         case info["type"]
192:         when "file"
193:           template(File.open(info["template_path"]).read, info["output_path"])
194:         when "directory"
195:           directory(info["output_path"])
196:         when "copy"
197:           copy(info["template_path"], info["output_path"])
198:         else
199:           raise "Unknown 'type': #{info["type"]}!"
200:         end
201:       end
202:     end
203:   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 113
113:   def manifest
114:     if templates_directory_path.nil? || manifest_path.nil?
115:       raise "Unable to dynamically figure out your templates_directory_path and manifest_path!\nPlease implement these methods and let Genosaurus know where to find these things. Thanks."
116:     end
117:     if File.exists?(manifest_path)
118:       # run using the yml file
119:       template = ERB.new(File.open(manifest_path).read, nil, "->")
120:       man = YAML.load(template.result(binding))
121:     else
122:       files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
123:       man = {}
124:       files.each_with_index do |f, i|
125:         output_path = f.gsub(templates_directory_path, "")
126:         output_path.gsub!(".template", "")
127:         output_path.gsub!(/^\//, "")
128:         man["template_#{i+1}"] = {
129:           "type" => File.directory?(f) ? "directory" : "file",
130:           "template_path" => f,
131:           "output_path" => Erubis::Eruby.new(output_path, :pattern => '% %').result(binding)
132:         }
133:       end
134:     end
135:     # puts man.inspect
136:     man
137:   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 91
91:   def manifest_path
92:     @manifest_path
93:   end

[Source]

     # File lib/genosaurus.rb, line 205
205:   def method_missing(sym, *args)
206:     p = param(sym)
207:     return p if p
208:     raise NoMethodError.new(sym)
209:   end

Returns a parameter from the initial Hash of parameters.

[Source]

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

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

[Source]

    # File lib/genosaurus.rb, line 96
96:   def setup
97:     # does nothing, unless overridden in subclass.
98:   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 159
159:   def template(input_file, output_file, options = @options)
160:     output_file = template_copy_common(output_file, options)
161:     unless output_file.nil?
162:       # File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read, nil, "->").result(binding)}
163:       File.open(output_file, "w") {|f| f.puts ERB.new(input_file, nil, "->").result(binding)}
164:       puts "Wrote: #{output_file}"
165:     end
166:   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 83
83:   def templates_directory_path
84:     @templates_directory_path
85:   end

[Validate]