Sha256: c40ca418da7e4dbc3fd04e3c4aa71e274b09e51c877083aa89270a2c3a1c5117

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

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

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

    # load any templates defined in project/config/lono/*
    def load_subfolder
      Dir.glob("#{File.dirname(@path)}/lono/**/*").select{ |e| File.file? e }.each do |path|
        instance_eval(File.read(path), path)
      end
    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
      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 CloudFormation templates:" unless @options[:quiet]
      @results.each do |name,json|
        path = "#{output_path}/#{name}"
        puts "  #{path}" unless @options[:quiet]
        ensure_parent_dir(path)
        validate(json, path)
        File.open(path, 'w') do |f|
          f.write(output_json(json))
        end
      end
    end

    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

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

    def ensure_parent_dir(path)
      dir = File.dirname(path)
      FileUtils.mkdir_p(dir) unless File.exist?(dir)
    end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lono-0.5.2 lib/lono/dsl.rb
lono-0.5.1 lib/lono/dsl.rb