Sha256: e33618098ad2b8c93c05cd5b1d9ead0c35912310b15724416b076172792f945e

Contents?: true

Size: 1.18 KB

Versions: 31

Compression:

Stored size: 1.18 KB

Contents

class Lono::Template
  module Evaluate
    def evaluate_template_path(path)
      return unless File.exist?(path)

      begin
        instance_eval(File.read(path), path)
      rescue Exception => e
        template_evaluation_error(e)
        puts "\nFull error:"
        raise
      end
    end

    # Prints out a user friendly task_definition error message
    def template_evaluation_error(e)
      error_info = e.backtrace.first
      path, line_no, _ = error_info.split(':')
      line_no = line_no.to_i
      puts "Error evaluating #{path}:".color(:red)
      puts e.message
      puts "Here's the line in #{path} with the error:\n\n"

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

Version data entries

31 entries across 31 versions & 1 rubygems

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