Sha256: f2dae95fa3a5fbd032cd8d5479eaf02f19326a33bba5b24814fb2eed3811f2ac

Contents?: true

Size: 1.34 KB

Versions: 31

Compression:

Stored size: 1.34 KB

Contents

class Lono::Template
  module Util
    def ensure_parent_dir(path)
      dir = File.dirname(path)
      FileUtils.mkdir_p(dir) unless File.exist?(dir)
    end

    def validate_yaml(path)
      text = IO.read(path)
      begin
        YAML.load(text)
      rescue Psych::SyntaxError => e
        handle_yaml_syntax_error(e, path)
      end
    end

    def handle_yaml_syntax_error(e, path)
      io = StringIO.new
      io.puts "Invalid yaml.  Output written to debugging: #{path}".color(:red)
      io.puts "ERROR: #{e.message}".color(:red)

      # Grab line info.  Example error:
      #   ERROR: (<unknown>): could not find expected ':' while scanning a simple key at line 2 column 1
      md = e.message.match(/at line (\d+) column (\d+)/)
      line = md[1].to_i

      lines = IO.read(path).split("\n")
      context = 5 # lines of context
      top, bottom = [line-context-1, 0].max, line+context-1
      spacing = lines.size.to_s.size
      lines[top..bottom].each_with_index do |line_content, index|
        line_number = top+index+1
        if line_number == line
          io.printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
        else
          io.printf("%#{spacing}d %s\n", line_number, line_content)
        end
      end

      if ENV['TEST']
        io.string
      else
        puts io.string
        exit 1
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
lono-6.1.11 lib/lono/template/util.rb
lono-6.1.10 lib/lono/template/util.rb
lono-6.1.9 lib/lono/template/util.rb
lono-6.1.8 lib/lono/template/util.rb
lono-6.1.7 lib/lono/template/util.rb
lono-6.1.6 lib/lono/template/util.rb
lono-6.1.5 lib/lono/template/util.rb
lono-6.1.4 lib/lono/template/util.rb
lono-6.1.3 lib/lono/template/util.rb
lono-6.1.2 lib/lono/template/util.rb
lono-6.1.1 lib/lono/template/util.rb
lono-6.1.0 lib/lono/template/util.rb
lono-6.0.1 lib/lono/template/util.rb
lono-6.0.0 lib/lono/template/util.rb
lono-5.3.4 lib/lono/template/util.rb
lono-5.3.3 lib/lono/template/util.rb
lono-5.3.2 lib/lono/template/util.rb
lono-5.3.1 lib/lono/template/util.rb
lono-5.3.0 lib/lono/template/util.rb
lono-5.2.8 lib/lono/template/util.rb