Class: Lono::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DSL

Returns a new instance of DSL.



3
4
5
6
7
8
9
# File 'lib/lono/dsl.rb', line 3

def initialize(options={})
  @options = options
  @options[:project_root] ||= '.'
  @path = "#{@options[:project_root]}/config/lono.rb"
  @templates = []
  @results = {}
end

Instance Method Details

#buildObject



27
28
29
30
31
# File 'lib/lono/dsl.rb', line 27

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

#evaluateObject



11
12
13
14
# File 'lib/lono/dsl.rb', line 11

def evaluate
  instance_eval(File.read(@path), @path)
  load_subfolder
end

#load_subfolderObject

load any templates defined in project/config/lono/*



17
18
19
20
21
# File 'lib/lono/dsl.rb', line 17

def load_subfolder
  Dir.glob("#{File.dirname(@path)}/lono/*").each do |path|
    instance_eval(File.read(path), path)
  end
end

#outputObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lono/dsl.rb', line 33

def output
  output_path = "#{@options[:project_root]}/output"
  FileUtils.rm_rf(output_path) if @options[:clean]
  FileUtils.mkdir(output_path) unless File.exist?(output_path)
  puts "Generating Cloud Formation templates:" unless @options[:quiet]
  @results.each do |name,json|
    path = "#{output_path}/#{name}"
    puts "  #{path}" unless @options[:quiet]
    validate(json, path)
    File.open(path, 'w') {|f| f.write(output_json(json)) }
  end
end

#output_json(json) ⇒ Object



56
57
58
# File 'lib/lono/dsl.rb', line 56

def output_json(json)
  @options[:pretty] ? JSON.pretty_generate(JSON.parse(json)) : json
end

#run(options = {}) ⇒ Object



60
61
62
63
64
# File 'lib/lono/dsl.rb', line 60

def run(options={})
  evaluate
  build
  output
end

#template(name, &block) ⇒ Object



23
24
25
# File 'lib/lono/dsl.rb', line 23

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

#validate(json, path) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/lono/dsl.rb', line 46

def validate(json, path)
  begin
    JSON.parse(json)
  rescue JSON::ParserError => e
    puts "Invalid json.  Output written to #{path} for debugging".colorize(:red)
    File.open(path, 'w') {|f| f.write(json) }
    exit 1
  end
end