Skip to content

Instantly share code, notes, and snippets.

@heathd
Last active December 14, 2015 07:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathd/5047997 to your computer and use it in GitHub Desktop.
Save heathd/5047997 to your computer and use it in GitHub Desktop.
Hello <%= name %>!
#!/usr/bin/env ruby -w
require 'erb'
require 'yaml'
require 'optparse'
require 'ostruct'
variables = OpenStruct.new
def render_erb(template_name, variables)
template = File.read(template_name)
ERB.new(template).result(variables.instance_eval { binding })
end
options = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename(__FILE__)} [options] <my_template.erb>"
opts.on("-d varname=value", "Define a variable") do |v|
varname, value = v.split('=')
raise "Invalid varname #{varname}" unless varname =~ /\A[a-zA-Z_-][a-zA-Z0-9_-]*\Z/
variables.send(:"#{varname}=", value)
end
opts.on("-y somefile.yml", "Define variables from yml") do |yaml_filename|
YAML.load_file(yaml_filename).each do |varname, value|
raise "Invalid varname #{varname}" unless varname =~ /\A[a-zA-Z_-][a-zA-Z0-9_-]*\Z/
case value
when Hash, Array then
$stderr.puts "Warning: ignoring Hash/Array #{value}"
else
variables.send(:"#{varname}=", value)
end
end
end
end
begin
options.parse!
if ARGV.size == 1
puts render_erb(ARGV[0], variables)
else
$stderr.puts options.help
end
rescue Errno::ENOENT => e
$stderr.puts "ERROR: #{e}"
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment