Class: Bauk::Gen::Generator

Inherits:
Object
  • Object
show all
Includes:
ConfigUtils, Init, Utils::Log
Defined in:
lib/bauk/gen/generator.rb

Overview

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.

Constant Summary collapse

CONTENT_KEYS =
%w[string file].freeze

Instance Method Summary collapse

Methods included from Init

#init, #init_config_file, #init_files, #init_templates

Methods included from ConfigUtils

#underscore, #validate_config, #validate_config_item

Constructor Details

#initialize(data) ⇒ Generator

Returns a new instance of Generator.



39
40
41
# File 'lib/bauk/gen/generator.rb', line 39

def initialize(data)
  @input_config = data[:config]
end

Instance Method Details

#configObject

This function obtains the config for this generator Example config: c = {

name: "Project Name",
description: "Project Description",
name => {
  custom_conf: 123
},

}



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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

#config_generatorsObject

This function contains the default list of configs



49
50
51
52
53
# File 'lib/bauk/gen/generator.rb', line 49

def config_generators
  [
    Bauk::Gen::Configs::Files
  ]
end

#default_configObject

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



119
120
121
122
123
124
125
126
# File 'lib/bauk/gen/generator.rb', line 119

def default_config
  {
    config: {
      name: "ExampleName",
      description: "Example project description"
    }
  }
end

#default_generator_configObject

Default config specific to this generator (injected at the generator level)



129
130
131
# File 'lib/bauk/gen/generator.rb', line 129

def default_generator_config
  {}
end

#generateObject



76
77
78
79
80
81
82
83
# 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

#input_generatorsObject

This function contains the default list of inputs



56
57
58
59
60
# File 'lib/bauk/gen/generator.rb', line 56

def input_generators
  [
    Bauk::Gen::Inputs::Templates
  ]
end

#input_itemsObject

This function gets the items from the inputs



86
87
88
89
90
91
92
93
94
95
# 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

#modulesObject

This method lists modules that you want to include



44
45
46
# File 'lib/bauk/gen/generator.rb', line 44

def modules
  []
end

#nameObject

Method to get generator name from class



161
162
163
# File 'lib/bauk/gen/generator.rb', line 161

def name
  underscore(self.class.name.split('::').join('_').sub(/_*generator$/i, '')).to_sym
end

#output_generatorsObject

This function contains the default list of outputs



63
64
65
66
67
# File 'lib/bauk/gen/generator.rb', line 63

def output_generators
  [
    Bauk::Gen::Outputs::Filesystem
  ]
end

#output_itemsObject

This function writes the items to outputs



111
112
113
114
115
# 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

#transformationsObject

This function contains the default transformations applied to each template TODO: It is still not implemented



71
72
73
74
# File 'lib/bauk/gen/generator.rb', line 71

def transformations
  [
  ]
end

#validate_itemsObject

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
# 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