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 24
24:   def initialize(options = {})
25:     @options = options
26:     self.class.required_params.each do |p|
27:       raise ::ArgumentError.new("The required parameter '#{p.to_s.upcase}' is missing for this generator!") unless param(p)
28:     end
29:     @generator_name = self.class.name
30:     @generator_name_underscore = @generator_name.underscore
31:     @templates_directory_path = nil
32:     @manifest_path = nil
33:     $".each do |f|
34:       if f.match(/#{@generator_name_underscore}\.rb$/)
35:         @templates_directory_path = File.join(File.dirname(f), "templates")
36:         @manifest_path = File.join(File.dirname(f), "manifest.yml")
37:       end
38:     end
39:     setup
40:   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 14
14:     def run(options = ENV.to_hash)
15:       gen = self.new(options)
16:       gen.generate
17:       gen
18:     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 69
69:   def after_generate
70:   end

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

[Source]

    # File lib/genosaurus.rb, line 64
64:   def before_generate
65:   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 76
76:   def manifest
77:     ivar_cache do 
78:       if File.exists?(manifest_path)
79:         # run using the yml file
80:         template = ERB.new(File.open(manifest_path).read, nil, "->")
81:         man = YAML.load(template.result(binding))
82:       else
83:         files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
84:         man = {}
85:         files.each_with_index do |f, i|
86:           output_path = f.gsub(templates_directory_path, "")
87:           output_path.gsub!(".template", "")
88:           output_path.gsub!(/^\//, "")
89:           man["template_#{i+1}"] = {
90:             "type" => File.directory?(f) ? "directory" : "file",
91:             "template_path" => f,
92:             "output_path" => ERB.new(output_path, nil, "->").result(binding)
93:           }
94:         end
95:       end
96:       # puts man.inspect
97:       man
98:     end
99:   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 54
54:   def manifest_path
55:     @manifest_path
56:   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 59
59:   def setup
60:     # does nothing, unless overridden in subclass.
61:   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 46
46:   def templates_directory_path
47:     @templates_directory_path
48:   end

[Validate]