Class: Lono::Template::DSL
- Inherits:
-
Object
- Object
- Lono::Template::DSL
- Defined in:
- lib/lono/template/dsl.rb
Instance Method Summary collapse
- #build_templates ⇒ Object
- #commented(text) ⇒ Object
- #ensure_parent_dir(path) ⇒ Object
- #evaluate_folder(folder) ⇒ Object
- #evaluate_template(name) ⇒ Object
- #evaluate_template_path(path) ⇒ Object
-
#evaluate_templates ⇒ Object
Instance eval’s the template declarations in app/definitions in this order:.
- #handle_yaml_syntax_error(e, path) ⇒ Object
-
#initialize(options = {}) ⇒ DSL
constructor
A new instance of DSL.
- #run(options = {}) ⇒ Object
- #template(name, &block) ⇒ Object
-
#template_evaluation_error(e) ⇒ Object
Prints out a user friendly task_definition error message.
- #validate(path) ⇒ Object
- #write_output ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ DSL
Returns a new instance of DSL.
2 3 4 5 6 7 8 |
# File 'lib/lono/template/dsl.rb', line 2 def initialize(={}) @options = Lono::ProjectChecker.check Lono::ProjectChecker.empty_templates @templates = [] @results = {} end |
Instance Method Details
#build_templates ⇒ Object
83 84 85 86 87 |
# File 'lib/lono/template/dsl.rb', line 83 def build_templates @templates.each do |t| @results[t[:name]] = Lono::Template::Template.new(t[:name], t[:block], @options).build end end |
#commented(text) ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/lono/template/dsl.rb', line 145 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 |
#ensure_parent_dir(path) ⇒ Object
153 154 155 156 |
# File 'lib/lono/template/dsl.rb', line 153 def ensure_parent_dir(path) dir = File.dirname(path) FileUtils.mkdir_p(dir) unless File.exist?(dir) end |
#evaluate_folder(folder) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/lono/template/dsl.rb', line 48 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
31 32 33 34 |
# File 'lib/lono/template/dsl.rb', line 31 def evaluate_template(name) path = "#{Lono.config.definitions_path}/#{name}.rb" evaluate_template_path(path) end |
#evaluate_template_path(path) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lono/template/dsl.rb', line 36 def evaluate_template_path(path) return unless File.exist?(path) begin instance_eval(File.read(path), path) rescue Exception => e template_evaluation_error(e) puts "\nFull error:" raise end end |
#evaluate_templates ⇒ Object
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.
24 25 26 27 28 29 |
# File 'lib/lono/template/dsl.rb', line 24 def evaluate_templates evaluate_template("base") evaluate_folder("base") evaluate_template(Lono.env) evaluate_folder(Lono.env) end |
#handle_yaml_syntax_error(e, path) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/lono/template/dsl.rb', line 114 def handle_yaml_syntax_error(e, path) io = StringIO.new io.puts "Invalid yaml. Output written to debugging: #{path}".colorize(:red) io.puts "ERROR: #{e.}".colorize(:red) # Grab line info. Example error: # ERROR: (<unknown>): could not find expected ':' while scanning a simple key at line 2 column 1 md = e..match(/at line (\d+) column (\d+)/) line = md[1].to_i lines = IO.read(path).split("\n") context = 5 # lines of context top, bottom = [line-context-1, 0].max, line+context-1 spacing = lines.size.to_s.size lines[top..bottom].each_with_index do |line_content, index| line_number = top+index+1 if line_number == line io.printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content) else io.printf("%#{spacing}d %s\n", line_number, line_content) end end if ENV['TEST'] io.string else puts io.string exit 1 end end |
#run(options = {}) ⇒ Object
10 11 12 13 14 |
# File 'lib/lono/template/dsl.rb', line 10 def run(={}) evaluate_templates build_templates write_output end |
#template(name, &block) ⇒ Object
79 80 81 |
# File 'lib/lono/template/dsl.rb', line 79 def template(name, &block) @templates << {name: name, block: block} end |
#template_evaluation_error(e) ⇒ Object
Prints out a user friendly task_definition error message
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/dsl.rb', line 56 def template_evaluation_error(e) error_info = e.backtrace.first path, line_no, _ = error_info.split(':') line_no = line_no.to_i puts "Error evaluating #{path}:".colorize(:red) puts e. puts "Here's the line in #{path} with the error:\n\n" contents = IO.read(path) content_lines = contents.split("\n") context = 5 # lines of context top, bottom = [line_no-context-1, 0].max, line_no+context-1 spacing = content_lines.size.to_s.size content_lines[top..bottom].each_with_index do |line_content, index| line_number = top+index+1 if line_number == line_no printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content) else printf("%#{spacing}d %s\n", line_number, line_content) end end end |
#validate(path) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/lono/template/dsl.rb', line 105 def validate(path) text = IO.read(path) begin YAML.load(text) rescue Psych::SyntaxError => e handle_yaml_syntax_error(e, path) end end |
#write_output ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lono/template/dsl.rb', line 89 def write_output output_path = "#{Lono.config.output_path}/templates" FileUtils.rm_rf(output_path) if @options[:clean] FileUtils.mkdir_p(output_path) puts "Generating CloudFormation templates:" unless @options[:quiet] @results.each do |name,text| path = "#{output_path}/#{name}".sub(/^\.\//,'') # strip leading '.' path += ".yml" puts " #{path}" unless @options[:quiet] ensure_parent_dir(path) text = commented(text) IO.write(path, text) # write file first so validate method is simpler validate(path) end end |