Class: Genosaurus

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/genosaurus.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Genosaurus

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/genosaurus.rb', line 54

def initialize(options = {})
  unless options.is_a?(Hash)
    opts = [options].flatten
    options = {}
    self.class.required_params.each_with_index do |p, i|
      options[p.to_s] = opts[i]
    end
  end
  @options = options
  self.class.required_params.each do |p|
    raise ::ArgumentError.new("The required parameter '#{p.to_s.upcase}' is missing for this generator!") unless param(p)
  end
  @generator_name = self.class.name
  @generator_name_underscore = @generator_name.underscore #String::Style.underscore(@generator_name)#.underscore
  @templates_directory_path = nil
  @manifest_path = nil
  $".each do |f|
    if f.match(/#{@generator_name_underscore}\.rb$/)
      @templates_directory_path = File.join(File.dirname(f), "templates")
      @manifest_path = File.join(File.dirname(f), "manifest.yml")
    end
  end
  setup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Raises:

  • (NoMethodError)


205
206
207
208
209
# File 'lib/genosaurus.rb', line 205

def method_missing(sym, *args)
  p = param(sym)
  return p if p
  raise NoMethodError.new(sym)
end

Class Method Details

.describeObject

Describes the generator.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/genosaurus.rb', line 32

def describe
  text = ["#{self}:"]
  required_params.each do |p|
    text << "Required Parameter: '#{p.to_s.downcase}'"
  end
  dd = description_detail
  unless dd.nil? || dd == ''
    text << "---------------"
    text << dd
  end
  text.join("\n")
end

.description_detailObject

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



46
47
48
# File 'lib/genosaurus.rb', line 46

def description_detail
  ''
end

.require_param(*args) ⇒ Object

Used to define arguments that are required by the generator.



140
141
142
143
# File 'lib/genosaurus.rb', line 140

def self.require_param(*args)
  required_params << args
  required_params.flatten!
end

.required_paramsObject

Returns the required_params array.



146
147
148
# File 'lib/genosaurus.rb', line 146

def self.required_params
  @required_params ||= []
end

.run(options = ENV.to_hash) ⇒ Object

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



25
26
27
28
29
# File 'lib/genosaurus.rb', line 25

def run(options = ENV.to_hash)
  gen = self.new(options)
  gen.generate
  gen
end

Instance Method Details

#after_generateObject

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



106
107
# File 'lib/genosaurus.rb', line 106

def after_generate
end

#before_generateObject

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



101
102
# File 'lib/genosaurus.rb', line 101

def before_generate
end

#copy(input_file, output_file, options = @options) ⇒ Object



181
182
183
184
185
# File 'lib/genosaurus.rb', line 181

def copy(input_file, output_file, options = @options)
  output_file = template_copy_common(output_file, options)
  FileUtils.cp(input_file, output_file)
  puts "Copied: #{output_file}"
end

#directory(output_dir, options = @options) ⇒ Object

Creates the specified directory.



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/genosaurus.rb', line 169

def directory(output_dir, options = @options)
  if $genosaurus_output_directory
    output_dir = File.join($genosaurus_output_directory, output_dir) 
  end
  if File.exists?(output_dir)
    puts "Exists: #{output_dir}"
    return
  end
  mkdir_p(output_dir)
  puts "Created: #{output_dir}"
end

#generateObject

This does the dirty work of generation.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/genosaurus.rb', line 188

def generate
  generate_callbacks do
    manifest.each_value do |info|
      case info["type"]
      when "file"
        template(File.open(info["template_path"]).read, info["output_path"])
      when "directory"
        directory(info["output_path"])
      when "copy"
        copy(info["template_path"], info["output_path"])
      else
        raise "Unknown 'type': #{info["type"]}!"
      end
    end
  end
end

#manifestObject

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/genosaurus.rb', line 113

def manifest
  if templates_directory_path.nil? || manifest_path.nil?
    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."
  end
  if File.exists?(manifest_path)
    # run using the yml file
    template = ERB.new(File.open(manifest_path).read, nil, "->")
    man = YAML.load(template.result(binding))
  else
    files = Dir.glob(File.join(templates_directory_path, "**/*.template"))
    man = {}
    files.each_with_index do |f, i|
      output_path = f.gsub(templates_directory_path, "")
      output_path.gsub!(".template", "")
      output_path.gsub!(/^\//, "")
      man["template_#{i+1}"] = {
        "type" => File.directory?(f) ? "directory" : "file",
        "template_path" => f,
        "output_path" => Erubis::Eruby.new(output_path, :pattern => '% %').result(binding)
      }
    end
  end
  # puts man.inspect
  man
end

#manifest_pathObject

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.



91
92
93
# File 'lib/genosaurus.rb', line 91

def manifest_path
  @manifest_path
end

#param(key) ⇒ Object

Returns a parameter from the initial Hash of parameters.



151
152
153
# File 'lib/genosaurus.rb', line 151

def param(key)
  (@options[key.to_s.downcase] ||= @options[key.to_s.upcase])
end

#setupObject

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



96
97
98
# File 'lib/genosaurus.rb', line 96

def setup
  # does nothing, unless overridden in subclass.
end

#template(input_file, output_file, options = @options) ⇒ Object

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.



159
160
161
162
163
164
165
166
# File 'lib/genosaurus.rb', line 159

def template(input_file, output_file, options = @options)
  output_file = template_copy_common(output_file, options)
  unless output_file.nil?
    # File.open(output_file, "w") {|f| f.puts ERB.new(File.open(input_file).read, nil, "->").result(binding)}
    File.open(output_file, "w") {|f| f.puts ERB.new(input_file, nil, "->").result(binding)}
    puts "Wrote: #{output_file}"
  end
end

#templates_directory_pathObject

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.



83
84
85
# File 'lib/genosaurus.rb', line 83

def templates_directory_path
  @templates_directory_path
end