Sha256: 6d2dac1f81194983a9fd73f0a4996f638cbc0036a7da2de95a449514883fa025

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

require 'rake'
require 'rake/tasklib'

require 'cfndsl'

module CfnDsl
  # Rake Task
  class RakeTask < Rake::TaskLib
    attr_accessor :cfndsl_opts

    def initialize(name = nil)
      yield self if block_given?

      desc 'Generate Cloudformation' unless ::Rake.application.last_comment
      task(name || :generate) do |_t, _args|
        cfndsl_opts[:files].each do |opts|
          generate(opts)
        end
      end
    end

    private

    def generate(opts)
      log(opts)
      outputter(opts) do |output|
        output.puts cfndsl_opts[:pretty] ? JSON.pretty_generate(model(opts[:filename])) : model(opts[:filename]).to_json
      end
    end

    def log(opts)
      type = opts[:output].nil? ? 'STDOUT' : opts[:output]
      verbose.puts("Writing to #{type}") if verbose
    end

    def outputter(opts)
      opts[:output].nil? ? yield(STDOUT) : file_output(opts[:output]) { |f| yield f }
    end

    def model(filename)
      raise "#{filename} doesn't exist" unless File.exist?(filename)
      verbose.puts("using extras #{extra}") if verbose
      CfnDsl.eval_file_with_extras(filename, extra, verbose)
    end

    def extra
      cfndsl_opts.fetch(:extras, [])
    end

    def verbose
      cfndsl_opts[:verbose] && STDERR
    end

    def file_output(path)
      File.open(File.expand_path(path), 'w') { |f| yield f }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cfndsl-0.6.1 lib/cfndsl/rake_task.rb
cfndsl-0.6.0 lib/cfndsl/rake_task.rb
cfndsl-0.5.2 lib/cfndsl/rake_task.rb
cfndsl-0.5.1 lib/cfndsl/rake_task.rb