Sha256: cb8737e7f8c1eaa24bd701899b44476d5303985b8440db59c51ebed221a5071b

Contents?: true

Size: 1.17 KB

Versions: 17

Compression:

Stored size: 1.17 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".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

17 entries across 17 versions & 1 rubygems

Version Path
ufo-2.3.0 lib/ufo/dsl/outputter.rb
ufo-2.2.2 lib/ufo/dsl/outputter.rb
ufo-2.2.1 lib/ufo/dsl/outputter.rb
ufo-2.2.0 lib/ufo/dsl/outputter.rb
ufo-2.1.0 lib/ufo/dsl/outputter.rb
ufo-2.0.3 lib/ufo/dsl/outputter.rb
ufo-2.0.2 lib/ufo/dsl/outputter.rb
ufo-2.0.1 lib/ufo/dsl/outputter.rb
ufo-2.0.0 lib/ufo/dsl/outputter.rb
ufo-1.7.1 lib/ufo/dsl/outputter.rb
ufo-1.7.0 lib/ufo/dsl/outputter.rb
ufo-1.6.2 lib/ufo/dsl/outputter.rb
ufo-1.6.1 lib/ufo/dsl/outputter.rb
ufo-1.6.0 lib/ufo/dsl/outputter.rb
ufo-1.5.0 lib/ufo/dsl/outputter.rb
ufo-1.2.0 lib/ufo/dsl/outputter.rb
ufo-1.1.0 lib/ufo/dsl/outputter.rb