Class: PDK::Template::Renderer::V1::Renderer

Inherits:
AbstractRenderer show all
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

Methods inherited from AbstractRenderer

#initialize

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

Parameters:

  • dirs (Array[String])

    Directories to search in

  • glob_suffix (Array[String]) (defaults to: ['**', '*'])

    File glob to use when searching for files. Defaults to [‘**’, ‘*’]

Returns:

  • (Hash{String => String})

    Key is the template file relative path and the value is the absolute path to the template directory



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

Returns:

  • (Boolean)

See Also:

  • AbstractRenderer.has_single_item?


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

See Also:

  • AbstractRenderer.render


10
11
12
# File 'lib/pdk/template/renderer/v1/renderer.rb', line 10

def render(template_type, _name, options = {})
  render_module(options) { |*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

Parameters:

  • options (Hash{Object => Object}) (defaults to: {})

    A list of options to pass through to the renderer. See PDK::Template::TemplateDir helper methods for other options

See Also:



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(options = {})
  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 options[:include_first_time]

  legacy_template_dir = new_legacy_template_dir(context, template_uri, template_root, options[: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.message }
        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

See Also:

  • AbstractRenderer.render_single_item


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:

Parameters:

  • item_path (String)

    The path of the single item to render

Returns:

  • (String)


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