Class: Lono::Template
- Inherits:
-
Object
- Object
- Lono::Template
- Includes:
- ERB::Util
- Defined in:
- lib/lono/template.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #base64(value) ⇒ Object
-
#bracket_positions(line) ⇒ Object
Input: String Output: Array of parse positions.
- #build ⇒ Object
- #cfn_object?(s) ⇒ Boolean
-
#decompose(line) ⇒ Object
Input String line of code to decompose into chunks, some can be transformed into objects Output Array of strings, some can be transformed into objects.
-
#encode_base64(text) ⇒ Object
For simple just parameters files that can also be generated with lono, the CFN Fn::Base64 function is not available and as lono is not being used in the context of CloudFormation.
- #erb_result(path, template) ⇒ Object
- #evaluate(line) ⇒ Object
- #find_in_map(*args) ⇒ Object
- #get_att(*args) ⇒ Object
- #get_azs(region = "AWS::Region") ⇒ Object
-
#indent(result, indentation_amount) ⇒ Object
add indentation.
-
#initialize(name, block, options = {}) ⇒ Template
constructor
A new instance of Template.
- #join(delimiter, values) ⇒ Object
-
#parse_positions(line) ⇒ Object
Input: Array - bracket_positions Ouput: Array - positions that can be use to determine what to parse.
- #partial(path, vars = {}, options = {}) ⇒ Object
- #recompose(decomposition) ⇒ Object
- #ref(name) ⇒ Object
- #select(index, list) ⇒ Object
- #source(path) ⇒ Object
-
#transform(data) ⇒ Object
transform each line of bash script to array with cloudformation template objects.
- #transform_array(arr) ⇒ Object
- #user_data(path, vars = {}) ⇒ Object
- #variables(vars = {}) ⇒ Object
Constructor Details
#initialize(name, block, options = {}) ⇒ Template
Returns a new instance of Template.
10 11 12 13 14 |
# File 'lib/lono/template.rb', line 10 def initialize(name, block, ={}) @name = name @block = block @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/lono/template.rb', line 9 def name @name end |
Instance Method Details
#base64(value) ⇒ Object
101 102 103 |
# File 'lib/lono/template.rb', line 101 def base64(value) %Q|{"Fn::Base64"=>"#{value}"}| end |
#bracket_positions(line) ⇒ Object
Input:
String
Output:
Array of parse positions
The positions of tokens taking into account when brackets start and close, handles nested brackets.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/lono/template.rb', line 143 def bracket_positions(line) positions,pair,count = [],[],0 line.split('').each_with_index do |char,i| pair << i if pair.empty? first_pair_char = line[pair[0]] if first_pair_char == '{' # object logic if char == '{' count += 1 end if char == '}' count -= 1 if count == 0 pair << i positions << pair pair = [] end end else # string logic lookahead = line[i+1] if lookahead == '{' pair << i positions << pair pair = [] end end end # end of loop # for string logic when lookahead does not contain a object token # need to clear out what's left to match the final pair if !pair.empty? pair << line.size - 1 positions << pair end positions end |
#build ⇒ Object
16 17 18 19 20 |
# File 'lib/lono/template.rb', line 16 def build instance_eval(&@block) template = IO.read(@source) erb_result(@source, template) end |
#cfn_object?(s) ⇒ Boolean
230 231 232 233 234 235 236 |
# File 'lib/lono/template.rb', line 230 def cfn_object?(s) exact = %w[Ref] pattern = %w[Fn::] exact_match = !!exact.detect {|word| s.include?(word)} pattern_match = !!pattern.detect {|p| s =~ Regexp.new(p)} (exact_match || pattern_match) && s =~ /^{/ && s =~ /=>/ end |
#decompose(line) ⇒ Object
Input
String line of code to decompose into chunks, some can be transformed into objects
Output
Array of strings, some can be transformed into objects
Example: line = ‘abcd{dd}e’ # nested brackets template.decompose(line).should == [‘a’,‘b’,‘c’,‘d{dd}’,‘e’]
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/lono/template.rb', line 200 def decompose(line) positions = parse_positions(line) return [line] if positions.empty? result = [] str = '' until positions.empty? left = positions.shift right = positions.shift token = line[left..right] # if cfn object, add to the result set but after clearing out # the temp str that is being built up when the token is just a string if cfn_object?(token) unless str.empty? # first token might be a object result << str str = '' end result << token else str << token # keeps building up the string end end # at the of the loop there's a leftover string, unless the last token # is an object result << str unless str.empty? result end |
#encode_base64(text) ⇒ Object
For simple just parameters files that can also be generated with lono, the CFN Fn::Base64 function is not available and as lono is not being used in the context of CloudFormation. So this can be used in it’s place.
249 250 251 |
# File 'lib/lono/template.rb', line 249 def encode_base64(text) Base64.strict_encode64(text).strip end |
#erb_result(path, template) ⇒ Object
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 77 |
# File 'lib/lono/template.rb', line 48 def erb_result(path, template) begin ERB.new(template, nil, "-").result(binding) rescue Exception => e puts e # how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051 # syntax errors have the (erb):xxx info in e.message # undefined variables have (erb):xxx info in e.backtrac error_info = e..split("\n").grep(/\(erb\)/)[0] error_info ||= e.backtrace.grep(/\(erb\)/)[0] raise unless error_info # unable to find the (erb):xxx: error line line = error_info.split(':')[1].to_i puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '')}" template_lines = template.split("\n") context = 5 # lines of context top, bottom = [line-context, 0].max, line+context spacing = template_lines.size.to_s.size template_lines[top..bottom].each_with_index do |line_content, index| line_number = top+index+1 if line_number == line printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content) else printf("%#{spacing}d %s\n", line_number, line_content) end end exit 1 unless ENV['TEST'] end end |
#evaluate(line) ⇒ Object
242 243 244 |
# File 'lib/lono/template.rb', line 242 def evaluate(line) recompose(decompose(line)) end |
#find_in_map(*args) ⇒ Object
97 98 99 |
# File 'lib/lono/template.rb', line 97 def find_in_map(*args) %Q|{"Fn::FindInMap" => [ #{transform_array(args)} ]}| end |
#get_att(*args) ⇒ Object
105 106 107 |
# File 'lib/lono/template.rb', line 105 def get_att(*args) %Q|{"Fn::GetAtt" => [ #{transform_array(args)} ]}| end |
#get_azs(region = "AWS::Region") ⇒ Object
109 110 111 |
# File 'lib/lono/template.rb', line 109 def get_azs(region="AWS::Region") %Q|{"Fn::GetAZs"=>"#{region}"}| end |
#indent(result, indentation_amount) ⇒ Object
add indentation
42 43 44 45 46 |
# File 'lib/lono/template.rb', line 42 def indent(result, indentation_amount) result.split("\n").map do |line| " " * indentation_amount + line end.join("\n") end |
#join(delimiter, values) ⇒ Object
113 114 115 |
# File 'lib/lono/template.rb', line 113 def join(delimiter, values) %Q|{"Fn::Join" => ["#{delimiter}", [ #{transform_array(values)} ]]}| end |
#parse_positions(line) ⇒ Object
Input:
Array - bracket_positions
Ouput:
Array - positions that can be use to determine what to parse
187 188 189 190 |
# File 'lib/lono/template.rb', line 187 def parse_positions(line) positions = bracket_positions(line) positions.flatten end |
#partial(path, vars = {}, options = {}) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/lono/template.rb', line 32 def partial(path,vars={}, ={}) path = "#{@options[:project_root]}/templates/partial/#{path}" template = IO.read(path) variables(vars) result = erb_result(path, template) result = indent(result, [:indent]) if [:indent] result end |
#recompose(decomposition) ⇒ Object
238 239 240 |
# File 'lib/lono/template.rb', line 238 def recompose(decomposition) decomposition.map { |s| cfn_object?(s) ? eval(s) : s } end |
#ref(name) ⇒ Object
93 94 95 |
# File 'lib/lono/template.rb', line 93 def ref(name) %Q|{"Ref"=>"#{name}"}| end |
#select(index, list) ⇒ Object
117 118 119 |
# File 'lib/lono/template.rb', line 117 def select(index, list) %Q|{"Fn::Select" => ["#{index}", [ #{transform_array(list)} ]]}| end |
#source(path) ⇒ Object
22 23 24 |
# File 'lib/lono/template.rb', line 22 def source(path) @source = path[0..0] == '/' ? path : "#{@options[:project_root]}/templates/#{path}" end |
#transform(data) ⇒ Object
transform each line of bash script to array with cloudformation template objects
127 128 129 130 131 132 133 134 |
# File 'lib/lono/template.rb', line 127 def transform(data) data = evaluate(data) if data[-1].is_a?(String) data[0..-2] + ["#{data[-1]}\n"] else data + ["\n"] end end |
#transform_array(arr) ⇒ Object
121 122 123 124 |
# File 'lib/lono/template.rb', line 121 def transform_array(arr) arr.map! {|x| x =~ /=>/ ? x : x.inspect } arr.join(',') end |
#user_data(path, vars = {}) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/lono/template.rb', line 79 def user_data(path, vars={}) path = "#{@options[:project_root]}/templates/user_data/#{path}" template = IO.read(path) variables(vars) result = erb_result(path, template) output = [] result.split("\n").each do |line| output += transform(line) end json = output.to_json json[0] = '' # remove first char: [ json.chop! # remove last char: ] end |
#variables(vars = {}) ⇒ Object
26 27 28 29 30 |
# File 'lib/lono/template.rb', line 26 def variables(vars={}) vars.each do |var,value| instance_variable_set("@#{var}", value) end end |