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.



6
7
8
9
10
11
# File 'lib/lono/dsl.rb', line 6

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

Instance Method Details

#buildObject



29
30
31
32
33
# File 'lib/lono/dsl.rb', line 29

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

#evaluateObject



13
14
15
16
# File 'lib/lono/dsl.rb', line 13

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

#load_subfolderObject

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



19
20
21
22
23
# File 'lib/lono/dsl.rb', line 19

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

#output(options = {}) ⇒ Object



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

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

#run(options = {}) ⇒ Object



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

def run(options={})
  evaluate
  build
  options.empty? ? output : output(options)
end

#template(name, &block) ⇒ Object



25
26
27
# File 'lib/lono/dsl.rb', line 25

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