Sha256: 24fa4dcf69f51ad4829326c724a594b18be4931f1511faa77fe4613fff9cac16

Contents?: true

Size: 1.19 KB

Versions: 10

Compression:

Stored size: 1.19 KB

Contents

module Ufo
  class DSL
    class Outputter
      def initialize(name, erb_result, options={})
        @name = name
        @erb_result = erb_result
        @options = options
        @pretty = options[:pretty].nil? ? true : options[:pretty]
      end

      def write
        output_path = "#{@options[:project_root]}/ufo/output"
        FileUtils.rm_rf(output_path) if @options[:clean]
        FileUtils.mkdir(output_path) unless File.exist?(output_path)

        path = "#{output_path}/#{@name}.json"
        puts "Generated task definition at: #{path}" unless @options[:quiet]
        validate(@erb_result, path)
        json = @pretty ?
          JSON.pretty_generate(JSON.parse(@erb_result)) :
          @erb_result
        File.open(path, 'w') {|f| f.write(output_json(json)) }
      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
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ufo-1.0.1 lib/ufo/dsl/outputter.rb
ufo-1.0.0 lib/ufo/dsl/outputter.rb
ufo-0.1.6 lib/ufo/dsl/outputter.rb
ufo-0.1.5 lib/ufo/dsl/outputter.rb
ufo-0.1.4 lib/ufo/dsl/outputter.rb
ufo-0.1.3 lib/ufo/dsl/outputter.rb
ufo-0.1.2 lib/ufo/dsl/outputter.rb
ufo-0.1.1 lib/ufo/dsl/outputter.rb
ufo-0.1.0 lib/ufo/dsl/outputter.rb
ufo-0.0.6 lib/ufo/dsl/outputter.rb