Sha256: 679d20eed2e9216ae52151d2117a19361756ea082ef5920dac615902bf1c1962

Contents?: true

Size: 1.16 KB

Versions: 38

Compression:

Stored size: 1.16 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 = "#{Ufo.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".sub(/^\.\//,'')
        puts "  #{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

38 entries across 38 versions & 1 rubygems

Version Path
ufo-4.1.10 lib/ufo/dsl/outputter.rb
ufo-4.1.9 lib/ufo/dsl/outputter.rb
ufo-4.1.8 lib/ufo/dsl/outputter.rb
ufo-4.1.7 lib/ufo/dsl/outputter.rb
ufo-4.1.6 lib/ufo/dsl/outputter.rb
ufo-4.1.5 lib/ufo/dsl/outputter.rb
ufo-4.1.4 lib/ufo/dsl/outputter.rb
ufo-4.1.2 lib/ufo/dsl/outputter.rb
ufo-4.1.1 lib/ufo/dsl/outputter.rb
ufo-4.1.0 lib/ufo/dsl/outputter.rb
ufo-4.0.3 lib/ufo/dsl/outputter.rb
ufo-4.0.2 lib/ufo/dsl/outputter.rb
ufo-4.0.1 lib/ufo/dsl/outputter.rb
ufo-4.0.0 lib/ufo/dsl/outputter.rb
ufo-3.5.7 lib/ufo/dsl/outputter.rb
ufo-3.5.6 lib/ufo/dsl/outputter.rb
ufo-3.5.5 lib/ufo/dsl/outputter.rb
ufo-3.5.4 lib/ufo/dsl/outputter.rb
ufo-3.5.3 lib/ufo/dsl/outputter.rb
ufo-3.5.2 lib/ufo/dsl/outputter.rb