Skip to content

Instantly share code, notes, and snippets.

@jvmvik
Created February 1, 2019 04:30
Show Gist options
  • Select an option

  • Save jvmvik/02947645fdfcb84d39a9a189e722f511 to your computer and use it in GitHub Desktop.

Select an option

Save jvmvik/02947645fdfcb84d39a9a189e722f511 to your computer and use it in GitHub Desktop.
Rakefile + ERB with better error handling
require 'yaml'
require 'erb'
# render a template
# @param [String] s template
# @param [Binding] ctx binding context
# @return [String] render output
def render(s, ctx)
begin
erb = ERB.new(s)
m = erb.result(ctx)
return m
rescue => e
puts 'ERROR: '
lineno = e.backtrace[0].split(':')[1].to_i
puts " line: #{lineno}"
puts " text: #{s.split("\n")[lineno-1].strip}"
puts " error: #{e.to_s}"
return nil
end
end
task :default => [:awesome]
def create_report
n = 'ok'
s = %Q(
hello world
<%= bad_call %>
test: <%= n %>
)
return render(s, binding)
end
task :awesome => :compile do
puts "report: " + create_report
end
task :compile do
sh "echo 'do compile'"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment