Method: PDK::Generate::PuppetObject#render_file

Defined in:
lib/pdk/generators/puppet_object.rb

#render_file(dest_path, template_path, data) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Render a file using the provided template and write it to disk.

Parameters:

  • dest_path (String)

    The path that the rendered file should be written to. Any necessary directories will be automatically created.

  • template_path (String)

    The path on disk to the file containing the template.

  • data (Hash{Object => Object})

    The data to be provided to the template when rendering.

Raises:



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pdk/generators/puppet_object.rb', line 117

def render_file(dest_path, template_path, data)
  PDK.logger.info(_("Creating '%{file}' from template.") % { file: dest_path })
  file_content = PDK::TemplateFile.new(template_path, data).render

  begin
    FileUtils.mkdir_p(File.dirname(dest_path))
  rescue SystemCallError => e
    raise PDK::CLI::FatalError, _("Unable to create directory '%{path}': %{message}") % {
      path:    File.dirname(dest_path),
      message: e.message,
    }
  end

  File.open(dest_path, 'w') { |f| f.write file_content }
rescue SystemCallError => e
  raise PDK::CLI::FatalError, _("Unable to write to file '%{path}': %{message}") % {
    path:    dest_path,
    message: e.message,
  }
end