Class: Lono::Template::Erb

Inherits:
Base
  • Object
show all
Includes:
Evaluate, Util
Defined in:
lib/lono/template/erb.rb

Instance Method Summary collapse

Methods included from Util

#ensure_parent_dir, #handle_yaml_syntax_error, #validate_yaml

Methods included from Evaluate

#evaluate_template_path, #template_evaluation_error

Methods included from Blueprint::Root

#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root

Constructor Details

#initialize(blueprint, options = {}) ⇒ Erb

Returns a new instance of Erb.



6
7
8
9
10
# File 'lib/lono/template/erb.rb', line 6

def initialize(blueprint, options={})
  super
  @templates = []
  @results = {}
end

Instance Method Details

#build_templatesObject



49
50
51
52
53
# File 'lib/lono/template/erb.rb', line 49

def build_templates
  @templates.each do |t|
    @results[t[:name]] = Lono::Template::Template.new(@blueprint, t[:name], t[:block], @options).build
  end
end

#commented(text) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/lono/template/erb.rb', line 74

def commented(text)
  comment =<<~EOS
    # This file was generated with lono. Do not edit directly, the changes will be lost.
    # More info: http://lono.cloud
  EOS
  "#{comment}#{text}"
end

#evaluate_folder(folder) ⇒ Object



38
39
40
41
42
43
# File 'lib/lono/template/erb.rb', line 38

def evaluate_folder(folder)
  paths = Dir.glob("#{Lono.config.definitions_path}/#{folder}/**/*")
  paths.select{ |e| File.file?(e) }.each do |path|
    evaluate_template_path(path)
  end
end

#evaluate_template(name) ⇒ Object



33
34
35
36
# File 'lib/lono/template/erb.rb', line 33

def evaluate_template(name)
  path = "#{Lono.config.definitions_path}/#{name}.rb"
  evaluate_template_path(path)
end

#evaluate_templatesObject

Instance eval’s the template declarations in app/definitions in this order:

app/definitions/base.rb
app/definitions/base - all files in folder
app/definitions/[Lono.env].rb
app/definitions/[Lono.env] - all files in folder

So Lono.env specific template declarations override base template declarations.



26
27
28
29
30
31
# File 'lib/lono/template/erb.rb', line 26

def evaluate_templates
  evaluate_template("base")
  evaluate_folder("base")
  evaluate_template(Lono.env)
  evaluate_folder(Lono.env)
end

#run(options = {}) ⇒ Object



12
13
14
15
16
# File 'lib/lono/template/erb.rb', line 12

def run(options={})
  evaluate_templates
  build_templates
  write_output
end

#template(name, &block) ⇒ Object



45
46
47
# File 'lib/lono/template/erb.rb', line 45

def template(name, &block)
  @templates << {name: name, block: block}
end

#write_outputObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lono/template/erb.rb', line 55

def write_output
  output_path = "#{Lono.config.output_path}/#{@blueprint}/templates"
  FileUtils.rm_rf(Lono.config.output_path) if @options[:clean] # removes entire output folder. params and templates
  FileUtils.mkdir_p(output_path)
  puts "Generating CloudFormation templates for blueprint #{@blueprint.color(:green)}:" unless @options[:quiet]
  @results.each do |name,text|
    path = "#{output_path}/#{name}".sub(/^\.\//,'') # strip leading '.'
    path += ".yml"
    unless @options[:quiet]
      pretty_path = path.sub("#{Lono.root}/",'')
      puts "  #{pretty_path}"
    end
    ensure_parent_dir(path)
    text = commented(text)
    IO.write(path, text) # write file first so validate method is simpler
    validate_yaml(path)
  end
end