Created
February 1, 2019 04:30
-
-
Save jvmvik/02947645fdfcb84d39a9a189e722f511 to your computer and use it in GitHub Desktop.
Rakefile + ERB with better error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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