Module: PDK::Util

Defined in:
lib/pdk/util.rb,
lib/pdk/util/env.rb,
lib/pdk/util/git.rb,
lib/pdk/util/bundler.rb,
lib/pdk/util/version.rb,
lib/pdk/util/windows.rb,
lib/pdk/util/filesystem.rb,
lib/pdk/util/json_finder.rb,
lib/pdk/util/ruby_version.rb,
lib/pdk/util/template_uri.rb,
lib/pdk/util/vendored_file.rb,
lib/pdk/util/puppet_strings.rb,
lib/pdk/util/puppet_version.rb,
lib/pdk/util/changelog_generator.rb

Defined Under Namespace

Modules: Bundler, ChangelogGenerator, Filesystem, Git, PuppetStrings, Version, Windows Classes: Env, GitError, JSONFinder, PuppetVersion, RubyVersion, TemplateURI, VendoredFile

Constant Summary collapse

MODULE_FOLDERS =
%w[
  manifests
  lib/puppet
  lib/puppet_x
  lib/facter
  tasks
  facts.d
  functions
  types
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cachedirString

Returns the fully qualified path to a per-user PDK cachedir.

Returns:

  • (String)

    Fully qualified path to per-user PDK cachedir.



132
133
134
135
136
137
138
# File 'lib/pdk/util.rb', line 132

def cachedir
  if Gem.win_platform?
    File.join(PDK::Util::Env['LOCALAPPDATA'], 'PDK', 'cache')
  else
    File.join(Dir.home, '.pdk', 'cache')
  end
end

.canonical_path(path) ⇒ String

Return an expanded, absolute path

Parameters:

  • path (String)

    Existing path that may not be canonical

Returns:

  • (String)

    Canonical path



85
86
87
88
89
90
91
92
93
94
# File 'lib/pdk/util.rb', line 85

def canonical_path(path)
  if Gem.win_platform?
    unless PDK::Util::Filesystem.exist?(path)
      raise PDK::CLI::FatalError, _("Cannot resolve a full path to '%{path}', as it does not currently exist.") % { path: path }
    end
    PDK::Util::Windows::File.get_long_pathname(path)
  else
    PDK::Util::Filesystem.expand_path(path)
  end
end

.configdirObject



141
142
143
144
145
146
147
# File 'lib/pdk/util.rb', line 141

def configdir
  if Gem.win_platform?
    File.join(PDK::Util::Env['LOCALAPPDATA'], 'PDK')
  else
    File.join(PDK::Util::Env.fetch('XDG_CONFIG_HOME', File.join(Dir.home, '.config')), 'pdk')
  end
end

.deep_duplicate(object) ⇒ Object

Does a deep copy instead of a shallow copy of an object.

Parameters:

  • object (Object)

    The object to duplicate

Returns:

  • (Object)

    duplicate of the original object the current working dir does not appear to be within a module.



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/pdk/util.rb', line 264

def deep_duplicate(object)
  if object.is_a?(Array)
    object.map { |item| deep_duplicate(item) }
  elsif object.is_a?(Hash)
    hash = object.dup
    hash.each_pair { |key, value| hash[key] = deep_duplicate(value) }
    hash
  else
    object
  end
end

.development_mode?Boolean

Returns:

  • (Boolean)


104
105
106
107
108
# File 'lib/pdk/util.rb', line 104

def development_mode?
  require 'pdk/util/version'

  (!PDK::Util::Version.git_ref.nil? || PDK::VERSION.end_with?('.pre'))
end

.find_all_json_in(text) ⇒ Array<Hash>

Iterate through possible JSON documents for all valid JSON

Parameters:

  • text (String)

    the text in which to find JSON document(s)

Returns:

  • (Array<Hash>)

    subset of text as Array of all JSON object found, empty Array if none are found JSON found in the text



209
210
211
# File 'lib/pdk/util.rb', line 209

def find_all_json_in(text)
  PDK::Util::JSONFinder.new(text).objects
end

.find_first_json_in(text) ⇒ Hash?

Iterate through possible JSON documents until we find one that is valid.

Parameters:

  • text (String)

    the text in which to find a JSON document

Returns:

  • (Hash, nil)

    subset of text as Hash of first valid JSON found, or nil if no valid JSON found in the text



199
200
201
# File 'lib/pdk/util.rb', line 199

def find_first_json_in(text)
  find_all_json_in(text).first
end

.find_upwards(target, start_dir = nil) ⇒ String?

Searches upwards from current working directory for the given target file.

Parameters:

  • target (String)

    Name of file to search for.

  • start_dir (String) (defaults to: nil)

    Directory to start searching from, defaults to Dir.pwd

Returns:

  • (String, nil)

    Fully qualified path to the given target file if found, nil if the target file could not be found.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pdk/util.rb', line 53

def find_upwards(target, start_dir = nil)
  previous = nil
  current  = PDK::Util::Filesystem.expand_path(start_dir || Dir.pwd)

  until !PDK::Util::Filesystem.directory?(current) || current == previous
    filename = File.join(current, target)
    return filename if PDK::Util::Filesystem.file?(filename)
    previous = current
    current = PDK::Util::Filesystem.expand_path('..', current)
  end
end

.gem_install?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/pdk/util.rb', line 111

def gem_install?
  !(package_install? || development_mode?)
end

.in_module_root?(path = Dir.pwd) ⇒ boolean

Returns true or false depending on if any of the common directories in a module are found in the specified directory. If a directory is not specified, the current working directory is used.

Returns:

  • (boolean)

    True if any folders from MODULE_FOLDERS are found in the current dir, false otherwise.



189
190
191
# File 'lib/pdk/util.rb', line 189

def in_module_root?(path = Dir.pwd)
  PDK::Util::MODULE_FOLDERS.any? { |dir| PDK::Util::Filesystem.directory?(File.join(path, dir)) }
end

.make_tmpdir_name(base) ⇒ String

Generate a name for a temporary directory.

Parameters:

  • base (String)

    A string to base the name generation off.

Returns:

  • (String)

    The temporary directory path.



71
72
73
74
75
76
77
# File 'lib/pdk/util.rb', line 71

def make_tmpdir_name(base)
  require 'tmpdir'

  t = Time.now.strftime('%Y%m%d')
  name = "#{base}#{t}-#{Process.pid}-#{rand(0x100000000).to_s(36)}"
  File.join(Dir.tmpdir, name)
end

.module_fixtures_dirString

The module’s fixtures directory for spec testing

Returns:

  • (String)
    • the path to the module’s fixtures directory



177
178
179
180
# File 'lib/pdk/util.rb', line 177

def module_fixtures_dir
  dir = module_root
  File.join(module_root, 'spec', 'fixtures') unless dir.nil?
end

.module_metadata(module_path = nil) ⇒ Object

TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead

Parameters:

  • module_path (String) (defaults to: nil)

    The path to the root of the module. Default is determine the module root automatically



230
231
232
233
234
# File 'lib/pdk/util.rb', line 230

def (module_path = nil)
  require 'pdk/module/metadata'
  module_path ||= module_root
  PDK::Module::Metadata.from_file(File.join(module_path, 'metadata.json')).data
end

.module_pdk_compatible?(module_path = nil) ⇒ Boolean

TO-DO: Refactor replacement of lib/pdk/module/build.rb:module_pdk_compatible? to use this function instead

Parameters:

  • module_path (String) (defaults to: nil)

    The path to the root of the module. Default is determine the module root automatically

Returns:

  • (Boolean)


239
240
241
# File 'lib/pdk/util.rb', line 239

def module_pdk_compatible?(module_path = nil)
  ['pdk-version', 'template-url'].any? { |key| (module_path).key?(key) }
end

.module_pdk_versionObject



244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/pdk/util.rb', line 244

def module_pdk_version
   = 

  if .nil? || .fetch('pdk-version', nil).nil?
    nil
  else
    ['pdk-version'].split.first
  end
rescue ArgumentError => e
  PDK.logger.error(e)
  nil
end

.module_rootString?

Returns path to the root of the module being worked on.

Returns:

  • (String, nil)

    Fully qualified base path to module, or nil if the current working dir does not appear to be within a module.



163
164
165
166
167
168
169
170
171
172
# File 'lib/pdk/util.rb', line 163

def module_root
   = find_upwards('metadata.json')
  if 
    File.dirname()
  elsif in_module_root?
    Dir.pwd
  else
    nil
  end
end

.package_cachedirObject



124
125
126
# File 'lib/pdk/util.rb', line 124

def package_cachedir
  File.join(pdk_package_basedir, 'share', 'cache')
end

.package_install?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/pdk/util.rb', line 97

def package_install?
  require 'pdk/util/version'

  !PDK::Util::Version.version_file.nil?
end

.pdk_package_basedirObject



116
117
118
119
120
121
# File 'lib/pdk/util.rb', line 116

def pdk_package_basedir
  raise PDK::CLI::FatalError, _('Package basedir requested for non-package install.') unless package_install?
  require 'pdk/util/version'

  File.dirname(PDK::Util::Version.version_file)
end

.system_configdirObject



150
151
152
153
154
155
156
# File 'lib/pdk/util.rb', line 150

def system_configdir
  return @system_configdir unless @system_configdir.nil?
  return @system_configdir = File.join(File::SEPARATOR, 'opt', 'puppetlabs', 'pdk', 'config') unless Gem.win_platform?

  return @system_configdir = File.join(PDK::Util::Env['ProgramData'], 'PuppetLabs', 'PDK') unless PDK::Util::Env['ProgramData'].nil?
  @system_configdir = File.join(PDK::Util::Env['AllUsersProfile'], 'PuppetLabs', 'PDK')
end

.targets_relative_to_pwd(targets) ⇒ Array<String>

Returns the targets’ paths relative to the working directory

Returns:

  • (Array<String>)

    The absolute or path to the target



217
218
219
220
221
222
223
224
225
# File 'lib/pdk/util.rb', line 217

def targets_relative_to_pwd(targets)
  targets.map do |t|
    if Pathname.new(t).absolute?
      Pathname.new(t).relative_path_from(Pathname.pwd)
    else
      t
    end
  end
end

Instance Method Details

#exit_process(exit_code) ⇒ Object

Calls Kernel.exit with an exitcode



41
42
43
# File 'lib/pdk/util.rb', line 41

def exit_process(exit_code)
  exit exit_code
end