Sha256: 0052963f23a591a9ee3dce6d07a3d5be9cefdc61384fb0e3c0768307bdcdb742

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 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_description
      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|
        if cfndsl_opts[:outformat] == 'yaml'
          data = model(opts[:filename]).to_json
          output.puts JSON.parse(data).to_yaml
        else
          output.puts cfndsl_opts[:pretty] ? JSON.pretty_generate(model(opts[:filename])) : model(opts[:filename]).to_json
        end
      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

7 entries across 7 versions & 1 rubygems

Version Path
cfndsl-0.16.6 lib/cfndsl/rake_task.rb
cfndsl-0.16.5 lib/cfndsl/rake_task.rb
cfndsl-0.16.3 lib/cfndsl/rake_task.rb
cfndsl-0.16.2 lib/cfndsl/rake_task.rb
cfndsl-0.16.1 lib/cfndsl/rake_task.rb
cfndsl-0.15.3 lib/cfndsl/rake_task.rb
cfndsl-0.15.2 lib/cfndsl/rake_task.rb