Sha256: f128b182cc708fd691abf4da3215ddc041e588a1f1f73fd5794558ff91932d78
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
require 'erb' require 'json' module Lono class DSL def initialize(options={}) @options = options @path = options[:config_path] || 'config/lono.rb' @templates = [] @results = {} end def evaluate instance_eval(File.read(@path), @path) end def template(name, &block) @templates << {:name => name, :block => block} end def build @templates.each do |t| @results[t[:name]] = Template.new(t[:name], t[:block], @options).build end end 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 def run(options={}) evaluate build options.empty? ? output : output(options) end end class Template include ERB::Util def initialize(name, block, options={}) @name = name @block = block @options = options @options[:project_root] ||= '.' end def build instance_eval(&@block) template = IO.read(@source) ERB.new(template).result(binding) end def source(path) @source = "#{@options[:project_root]}/templates/#{path}" end def variables(vars={}) vars.each do |var,value| instance_variable_set("@#{var}", value) end end def user_data(path) path = "#{@options[:project_root]}/templates/user_data/#{path}" template = IO.read(path) ERB.new(template).result(binding).split("\n").collect {|l| "#{l}\n"}.to_json end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lono-0.1.8 | lib/lono/dsl.rb |