Module: PDK::Module::TemplateDir

Defined in:
lib/pdk/module/template_dir.rb,
lib/pdk/module/template_dir/git.rb,
lib/pdk/module/template_dir/base.rb,
lib/pdk/module/template_dir/local.rb

Defined Under Namespace

Classes: Base, Git, Local

Class Method Summary collapse

Class Method Details

.files_in_template(dirs) ⇒ Hash{String=>String}

Get a list of template files in the template directory.

value locations.

Returns:

  • (Hash{String=>String})

    A hash of key file names and



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pdk/module/template_dir.rb', line 97

def self.files_in_template(dirs)
  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, '**', '*'), 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

.moduleroot_dir(template_root_dir) ⇒ Object



50
51
52
# File 'lib/pdk/module/template_dir.rb', line 50

def self.moduleroot_dir(template_root_dir)
  File.join(template_root_dir, 'moduleroot')
end

.moduleroot_init(template_root_dir) ⇒ Object



54
55
56
# File 'lib/pdk/module/template_dir.rb', line 54

def self.moduleroot_init(template_root_dir)
  File.join(template_root_dir, 'moduleroot_init')
end

.validate_module_template!(template_root_dir) ⇒ void

This method returns an undefined value.

Validate the content of the template directory.

a directory called ‘moduleroot’.

Raises:

  • (ArgumentError)

    If the specified path is not a directory.

  • (ArgumentError)

    If the template directory does not contain



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pdk/module/template_dir.rb', line 67

def self.validate_module_template!(template_root_dir)
  # rubocop:disable Style/GuardClause
  unless PDK::Util::Filesystem.directory?(template_root_dir)
    require 'pdk/util'

    if PDK::Util.package_install? && PDK::Util::Filesystem.fnmatch?(File.join(PDK::Util.package_cachedir, '*'), template_root_dir)
      raise ArgumentError, _('The built-in template has substantially changed. Please run "pdk convert" on your module to continue.')
    else
      raise ArgumentError, _("The specified template '%{path}' is not a directory.") % { path: template_root_dir }
    end
  end

  unless PDK::Util::Filesystem.directory?(moduleroot_dir(template_root_dir))
    raise ArgumentError, _("The template at '%{path}' does not contain a 'moduleroot/' directory.") % { path: template_root_dir }
  end

  unless PDK::Util::Filesystem.directory?(moduleroot_init(template_root_dir))
    # rubocop:disable Metrics/LineLength
    raise ArgumentError, _("The template at '%{path}' does not contain a 'moduleroot_init/' directory, which indicates you are using an older style of template. Before continuing please use the --template-url flag when running the pdk new commands to pass a new style template.") % { path: template_root_dir }
    # rubocop:enable Metrics/LineLength
  end
  # rubocop:enable Style/GuardClause
end

.with(uri, module_metadata = {}, init = false) {|self| ... } ⇒ Object

Creates a TemplateDir object with the path or URL to the template and the block of code to run to be run while the template is available.

The template directory is only guaranteed to be available on disk within the scope of the block passed to this method.

template or a URI to a git repository. Defaults to an empty Hash. the template available on disk.

Examples:

Using a git repository as a template

PDK::Module::TemplateDir.with('https://github.com/puppetlabs/pdk-templates') do |t|
  t.render do |filename, content|
    File.open(filename, 'w') do |file|
      file.write(content)
    end
  end
end

Parameters:

  • uri (PDK::Util::TemplateURI)

    The path to a directory to use as the

  • module_metadata (Hash) (defaults to: {})

    A Hash containing the module metadata.

Yield Parameters:

Raises:

  • (ArgumentError)

    If no block is given to this method.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pdk/module/template_dir.rb', line 33

def self.with(uri,  = {}, init = false)
  unless block_given?
    raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name }
  end
  unless uri.is_a? PDK::Util::TemplateURI
    raise ArgumentError, _('%{class_name}.with must be passed a PDK::Util::TemplateURI, got a %{uri_type}') % { uri_type: uri.class, class_name: name }
  end

  if PDK::Util::Git.repo?(uri.bare_uri)
    require 'pdk/module/template_dir/git'
    PDK::Module::TemplateDir::Git.new(uri, , init) { |value| yield value }
  else
    require 'pdk/module/template_dir/local'
    PDK::Module::TemplateDir::Local.new(uri, , init) { |value| yield value }
  end
end