Class: Lono::Param::Generator
Instance Attribute Summary collapse
Instance Method Summary
collapse
#initialize, #reinitialize, #template_path
#find_blueprint_root, #set_blueprint_root
Instance Attribute Details
#base_path ⇒ Object
set when generate is called
3
4
5
|
# File 'lib/lono/param/generator.rb', line 3
def base_path
@base_path
end
|
#env_path ⇒ Object
set when generate is called
3
4
5
|
# File 'lib/lono/param/generator.rb', line 3
def env_path
@env_path
end
|
Instance Method Details
#context ⇒ Object
Context for ERB rendering. This is where we control what references get passed to the ERB rendering.
40
41
42
|
# File 'lib/lono/param/generator.rb', line 40
def context
@context ||= Lono::Template::Context.new(@options)
end
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/lono/param/generator.rb', line 44
def convert_to_cfn_format(contents, casing=:underscore)
lines = parse_contents(contents)
data = {}
lines.each do |line|
key,value = line.strip.split("=").map {|x| x.strip}
value = remove_surrounding_quotes(value)
data[key] = value
end
params = []
data.each do |key,value|
param = if value == "use_previous_value" || value == "UsePreviousValue"
{
"ParameterKey": key,
"UsePreviousValue": true
}
elsif value
{
"ParameterKey": key,
"ParameterValue": value
}
end
if param
param = param.to_snake_keys if casing == :underscore
params << param
end
end
params
end
|
#generate ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/lono/param/generator.rb', line 5
def generate
puts "Generating parameter files for blueprint #{@blueprint.color(:green)}:"
contents = []
layering = Lono::Layering.new("params", @options, Lono.env)
layering.locations.each do |path|
contents << render_erb(path)
end
contents = contents.compact.join("\n")
data = convert_to_cfn_format(contents)
camel_data = convert_to_cfn_format(contents, :camel)
json = JSON.pretty_generate(camel_data)
write_output(json)
unless @options[:mute]
short_output_path = output_path.sub("#{Lono.root}/","")
puts " #{short_output_path}"
end
data
end
|
#output_path ⇒ Object
99
100
101
|
# File 'lib/lono/param/generator.rb', line 99
def output_path
"#{Lono.root}/output/#{@blueprint}/params/#{@stack}.json"
end
|
#parameters ⇒ Object
27
28
29
|
# File 'lib/lono/param/generator.rb', line 27
def parameters
generate
end
|
#parse_contents(contents) ⇒ Object
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/lono/param/generator.rb', line 78
def parse_contents(contents)
lines = contents.split("\n")
lines.map! { |l| l.sub(/#.*/,'').strip }
lines = lines.reject { |l| l =~ /(^|\s)#/i }
lines = lines.reject { |l| l.strip.empty? }
lines
end
|
#remove_surrounding_quotes(s) ⇒ Object
89
90
91
92
93
94
95
96
97
|
# File 'lib/lono/param/generator.rb', line 89
def remove_surrounding_quotes(s)
if s =~ /^"/ && s =~ /"$/
s.sub(/^["]/, '').gsub(/["]$/,'')
elsif s =~ /^'/ && s =~ /'$/
s.sub(/^[']/, '').gsub(/[']$/,'')
else
s
end
end
|
#render_erb(path) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/lono/param/generator.rb', line 31
def render_erb(path)
return unless path
if File.exist?(path)
RenderMePretty.result(path, context: context)
end
end
|
#write_output(json) ⇒ Object
103
104
105
106
107
|
# File 'lib/lono/param/generator.rb', line 103
def write_output(json)
dir = File.dirname(output_path)
FileUtils.mkdir_p(dir)
IO.write(output_path, json)
end
|