class Baha::PreBuild::Module::Template

Constants

LOG

Public Class Methods

execute(mod) click to toggle source
# File lib/baha/pre_build/template.rb, line 27
def self.execute(mod)   
  LOG.debug("template(#{mod.args.inspect})")

  template = mod.args['template']
  src = mod.config.resolve_file(mod.args['template'])
  dest = mod.image.workspace + mod.args['dest']
  raise ArgumentError.new("Unable to find template file #{template}") if src.nil?  
  LOG.info { "Loading template #{src}" }
  template_str = File.read(src)
  erb = ERB.new(template_str,0,'-')
  environment = mod.image.env
  environment.merge!(Hash[mod.args.map{|k,v| [k.to_sym, v]}])
  LOG.debug { "template environment: #{environment.inspect}" }
  LOG.info { "Writing to #{dest}" }
  File.open(dest,"w") do |f|
    f.write(erb.result(ErbBinding.new(environment,mod.config).get_binding))
  end
end