Class: PDK::Template::Renderer::V1::Renderer
- Inherits:
-
AbstractRenderer
- Object
- AbstractRenderer
- PDK::Template::Renderer::V1::Renderer
- Defined in:
- lib/pdk/template/renderer/v1/renderer.rb
Instance Attribute Summary
Attributes inherited from AbstractRenderer
#context, #template_root, #template_uri
Instance Method Summary collapse
-
#files_in_template(dirs, glob_suffix = ['**', '*']) ⇒ Hash{String => String}
private
Returns all files in the given template directories.
- #has_single_item?(item_path) ⇒ Boolean
-
#new_legacy_template_dir(context, uri, path, module_metadata = {}) ⇒ Object
private
Helper method used during testing :nocov:.
-
#new_template_file(template_file, template_data_hash) ⇒ Object
private
Helper method used during testing :nocov:.
- #render(template_type, _name, options = {}) ⇒ Object
-
#render_module(options = {}) ⇒ Object
private
Renders a new module.
- #render_single_item(relative_file_path, template_data_hash) ⇒ Object
-
#single_item_path(item_path) ⇒ String
private
Returns the full path for a single item.
Methods inherited from AbstractRenderer
Constructor Details
This class inherits a constructor from PDK::Template::Renderer::AbstractRenderer
Instance Method Details
#files_in_template(dirs, glob_suffix = ['**', '*']) ⇒ Hash{String => String}
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.
Returns all files in the given template directories
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 112 def files_in_template(dirs, glob_suffix = ['**', '*']) temp_paths = [] dirlocs = [] dirs.each do |dir| raise ArgumentError, _("The directory '%{dir}' doesn't exist") % { dir: dir } unless PDK::Util::Filesystem.directory?(dir) temp_paths += PDK::Util::Filesystem.glob(File.join(dir, *glob_suffix), File::FNM_DOTMATCH).select do |template_path| if PDK::Util::Filesystem.file?(template_path) && !PDK::Util::Filesystem.symlink?(template_path) dirlocs << dir end end temp_paths.map do |template_path| template_path.sub!(%r{\A#{Regexp.escape(dir)}#{Regexp.escape(File::SEPARATOR)}}, '') end end Hash[temp_paths.zip dirlocs] end |
#has_single_item?(item_path) ⇒ Boolean
15 16 17 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 15 def has_single_item?(item_path) # rubocop:disable Naming/PredicateName PDK::Util::Filesystem.exist?(single_item_path(item_path)) end |
#new_legacy_template_dir(context, uri, path, module_metadata = {}) ⇒ Object
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.
Helper method used during testing :nocov:
50 51 52 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 50 def new_legacy_template_dir(context, uri, path, = {}) LegacyTemplateDir.new(context, uri, path, ) end |
#new_template_file(template_file, template_data_hash) ⇒ Object
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.
Helper method used during testing :nocov:
42 43 44 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 42 def new_template_file(template_file, template_data_hash) TemplateFile.new(template_file, template_data_hash) end |
#render(template_type, _name, options = {}) ⇒ Object
10 11 12 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 10 def render(template_type, _name, = {}) render_module() { |*args| yield(*args) } if template_type == PDK::Template::MODULE_TEMPLATE_TYPE end |
#render_module(options = {}) ⇒ Object
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.
Renders a new module
:nocov: This is tested in acceptance and packaging tests
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 61 def render_module( = {}) require 'pdk/template/renderer/v1/template_file' moduleroot_dir = File.join(template_root, 'moduleroot') moduleroot_init = File.join(template_root, 'moduleroot_init') dirs = [moduleroot_dir] dirs << moduleroot_init if [:include_first_time] legacy_template_dir = new_legacy_template_dir(context, template_uri, template_root, [:module_metadata] || {}) files_in_template(dirs).each do |template_file, template_loc| template_file = template_file.to_s PDK.logger.debug(_("Rendering '%{template}'...") % { template: template_file }) dest_path = template_file.sub(%r{\.erb\Z}, '') config = legacy_template_dir.config_for(dest_path) dest_status = if template_loc.start_with?(moduleroot_init) :init else :manage end if config['unmanaged'] dest_status = :unmanage elsif config['delete'] dest_status = :delete else begin dest_content = new_template_file(File.join(template_loc, template_file), configs: config, template_dir: legacy_template_dir).render rescue => error error_msg = _( "Failed to render template '%{template}'\n" \ '%{exception}: %{message}', ) % { template: template_file, exception: error.class, message: error. } raise PDK::CLI::FatalError, error_msg end end yield dest_path, dest_content, dest_status end end |
#render_single_item(relative_file_path, template_data_hash) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 20 def render_single_item(relative_file_path, template_data_hash) template_file = single_item_path(relative_file_path) return nil unless PDK::Util::Filesystem.file?(template_file) && PDK::Util::Filesystem.readable?(template_file) PDK.logger.debug(_("Rendering '%{template}'...") % { template: template_file }) new_template_file(template_file, template_data_hash).render end |
#single_item_path(item_path) ⇒ String
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.
Returns the full path for a single item
:nocov:
34 35 36 |
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 34 def single_item_path(item_path) File.join(template_root, 'object_templates', item_path) end |