class Bauk::Gen::Generator
This class is the base generator that all others should extend from. It contains methods for listing inputs, outputs and trnasformations. The input generators cteate a map of items. These items are then handed to the output generators. In the base example, the only inout is from Templates and the output is the Filesystem output. So the templates are parsed to a hash, which is then passed to any transformers. After transformation, the items are passed to the Filesystem output.
Constants
- CONTENT_KEYS
Public Class Methods
# File lib/bauk/gen/generator.rb, line 39 def initialize(data) @input_config = data[:config] end
Public Instance Methods
This function obtains the config for this generator Example config: c = {
name: "Project Name", description: "Project Description", name => { custom_conf: 123 },
}
# File lib/bauk/gen/generator.rb, line 142 def config if @config return @config if name.empty? return @config.deep_merge! @config[:generators][name] end @config = default_config.deep_merge!({generators:{name => default_generator_config }}).deep_merge!(@input_config) config_generators.each do |c_gen| c_gen.new.add_config! @config end unless @config log.error 'No config found' return {} end log.debug "Obtained config: #{@config}" config end
This function contains the default list of configs
# File lib/bauk/gen/generator.rb, line 49 def config_generators [ Bauk::Gen::Configs::Files ] end
This method can be overridden to provide default values to config. These should be enough to get the generator/module working and are placed into the init config file
# File lib/bauk/gen/generator.rb, line 119 def default_config { config: { name: "ExampleName", description: "Example project description" } } end
Default config specific to this generator (injected at the generator level)
# File lib/bauk/gen/generator.rb, line 129 def default_generator_config {} end
# File lib/bauk/gen/generator.rb, line 76 def generate log.warn "Generating #{name} generator" validate_config input_items validate_items output_items log.warn "Finished generating #{@items.size} items" end
This function contains the default list of inputs
# File lib/bauk/gen/generator.rb, line 56 def input_generators [ Bauk::Gen::Inputs::Templates ] end
This function gets the items from the inputs
# File lib/bauk/gen/generator.rb, line 86 def input_items log.info "Generating items for generator: #{name} (#{self.class.name})" @items = {} modules.each do |mod| mod.input_items(@items) end input_generators.each do |i_gen| i_gen.new(self, config).input_items(@items) end end
This method lists modules that you want to include
# File lib/bauk/gen/generator.rb, line 44 def modules [] end
Method to get generator name from class
# File lib/bauk/gen/generator.rb, line 161 def name underscore(self.class.name.split('::').join('_').sub(/_*generator$/i, '')).to_sym end
This function contains the default list of outputs
# File lib/bauk/gen/generator.rb, line 63 def output_generators [ Bauk::Gen::Outputs::Filesystem ] end
This function writes the items to outputs
# File lib/bauk/gen/generator.rb, line 111 def output_items output_generators.each do |o_gen| o_gen.new(self, config).output_items(@items) end end
This function contains the default transformations applied to each template TODO: It is still not implemented
# File lib/bauk/gen/generator.rb, line 71 def transformations [ ] end
# File lib/bauk/gen/generator.rb, line 97 def validate_items @items.each do |name, item| item[:name] ||= name item[:attributes] ||= {} raise Inputs::Error, "No content found for item: #{name}" unless item[:content] unless item[:content].respond_to? :content raise Inputs::Error, "Invalid content found found. Found #{item[:content]}" end end raise Inputs::Error, 'No items found to generate' if @items.empty? end