diff options
author | sersut <serdar@opscode.com> | 2013-05-30 13:38:32 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2013-05-30 13:44:50 -0700 |
commit | 32172b48de16b884a539ee4358b52d711f86b368 (patch) | |
tree | 79613bb71025a7a46f4b8ce8b868e6d56a628e44 /lib/chef/mixin/template.rb | |
parent | aeee4bf96582769207780311b7b882c387656567 (diff) | |
download | chef-32172b48de16b884a539ee4358b52d711f86b368.tar.gz |
Make sure that windows line endings are protected during template rendering.
Diffstat (limited to 'lib/chef/mixin/template.rb')
-rw-r--r-- | lib/chef/mixin/template.rb | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/lib/chef/mixin/template.rb b/lib/chef/mixin/template.rb index 9892447ca4..d6cf564f40 100644 --- a/lib/chef/mixin/template.rb +++ b/lib/chef/mixin/template.rb @@ -93,14 +93,47 @@ class Chef partial_context._extend_modules(@_extension_modules) template_location = @template_finder.find(partial_name, options) - eruby = Erubis::Eruby.new(IO.read(template_location)) - eruby.evaluate(partial_context) + _render_template(IO.binread(template_location), partial_context) + end + + def render_template(template_location) + _render_template(IO.binread(template_location), self) + end + + def render_template_from_string(template) + _render_template(template, self) end ### # INTERNAL PUBLIC API ### + def _render_template(template, context) + # CHEF-2991 + # Erubis always emits unix line endings during template + # rendering. This results in automatic conversion of windows + # line endings to linux line endings if the original template + # contains windows line endings. In order to fix this we + # determine the line ending style of the template before + # rendering and convert the line endings of the output if needed + # If template contains any windows line endings we emit + # the template result with windows line endings. + windows_line_endings = template.include? "\r\n" + + begin + eruby = Erubis::Eruby.new(template) + output = eruby.evaluate(context) + rescue Object => e + raise TemplateError.new(e, template, context) + end + + if windows_line_endings + output = output.gsub("\n","\r\n") + end + + output + end + def _extend_modules(module_names) module_names.each do |mod| [:node, :render].each do |core_method| @@ -129,23 +162,6 @@ class Chef end end - - # Render a template with Erubis. Takes a template as a string, and a - # context hash. - def render_template(template, context) - begin - eruby = Erubis::Eruby.new(template) - output = eruby.evaluate(context) - rescue Object => e - raise TemplateError.new(e, template, context) - end - Tempfile.open("chef-rendered-template") do |tempfile| - tempfile.print(output) - tempfile.close - yield tempfile - end - end - class TemplateError < RuntimeError attr_reader :original_exception, :context SOURCE_CONTEXT_WINDOW = 2 |