Module: PDK::Util

Defined in:
lib/pdk/util.rb,
lib/pdk/util/git.rb,
lib/pdk/util/bundler.rb,
lib/pdk/util/version.rb,
lib/pdk/util/ruby_version.rb,
lib/pdk/util/vendored_file.rb,
lib/pdk/util/puppet_version.rb

Defined Under Namespace

Modules: Bundler, Git, Version Classes: PuppetVersion, RubyVersion, VendoredFile

Constant Summary collapse

MODULE_FOLDERS =
%w[
  manifests
  lib
  tasks
  facts.d
  functions
  types
].freeze

Class 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.



98
99
100
101
102
103
104
# File 'lib/pdk/util.rb', line 98

def cachedir
  if Gem.win_platform?
    File.join(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



56
57
58
59
60
61
62
63
64
65
# File 'lib/pdk/util.rb', line 56

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

.default_template_refObject



227
228
229
230
231
232
# File 'lib/pdk/util.rb', line 227

def default_template_ref
  # TODO: This should respect a --template-ref option if we add that
  return 'origin/master' if default_template_url != puppetlabs_template_url

  puppetlabs_template_ref
end

.default_template_urlObject



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pdk/util.rb', line 199

def default_template_url
  answer_file_url = PDK.answers['template-url']

  return puppetlabs_template_url if answer_file_url.nil?

  # Ignore answer file template-url if the value is the old or new default.
  return puppetlabs_template_url if answer_file_url == 'https://github.com/puppetlabs/pdk-module-template'
  return puppetlabs_template_url if answer_file_url == puppetlabs_template_url

  unless PDK::Util::Git.repo?(answer_file_url)
    PDK.logger.warn(_("Unable to access the previously used template '%{template}', using the default template instead.") % { template: answer_file_url })
    PDK.answers.update!('template-url' => nil)
    return puppetlabs_template_url
  end

  answer_file_url
end

.development_mode?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/pdk/util.rb', line 73

def development_mode?
  (!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



148
149
150
# File 'lib/pdk/util.rb', line 148

def find_all_json_in(text)
  find_valid_json_in(text, break_on_first: false)
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



138
139
140
# File 'lib/pdk/util.rb', line 138

def find_first_json_in(text)
  find_valid_json_in(text)
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.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pdk/util.rb', line 26

def find_upwards(target, start_dir = nil)
  previous = nil
  current  = File.expand_path(start_dir || Dir.pwd)

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

.find_valid_json_in(text, opts = {}) ⇒ 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

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

    options

Options Hash (opts):

  • :break_on_first (Boolean)

    Whether or not to break after valid JSON is found, defaults to true

Returns:

  • (Hash, Array<Hash>, nil)

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/pdk/util.rb', line 163

def find_valid_json_in(text, opts = {})
  break_on_first = opts.key?(:break_on_first) ? opts[:break_on_first] : true

  json_result = break_on_first ? nil : []

  text.scan(%r{\{(?:[^{}]|(?:\g<0>))*\}}x) do |str|
    begin
      if break_on_first
        json_result = JSON.parse(str)
        break
      else
        json_result.push(JSON.parse(str))
      end
    rescue JSON::ParserError
      next
    end
  end

  json_result
end

.gem_install?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/pdk/util.rb', line 78

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

.in_module_root?boolean

Returns true or false depending on if any of the common directories in a module are found in the current directory

Returns:

  • (boolean)

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



128
129
130
# File 'lib/pdk/util.rb', line 128

def in_module_root?
  PDK::Util::MODULE_FOLDERS.any? { |dir| File.directory?(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.



44
45
46
47
48
# File 'lib/pdk/util.rb', line 44

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

.module_metadataObject

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



245
246
247
# File 'lib/pdk/util.rb', line 245

def 
  PDK::Module::Metadata.from_file(File.join(module_root, 'metadata.json')).data
end

.module_pdk_compatible?Boolean

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

Returns:

  • (Boolean)


251
252
253
# File 'lib/pdk/util.rb', line 251

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

.module_pdk_versionObject



256
257
258
259
260
261
262
263
264
# File 'lib/pdk/util.rb', line 256

def module_pdk_version
   = 

  if !.nil? && .include?('pdk-version')
    ['pdk-version'].split.first
  else
    nil
  end
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.



111
112
113
114
115
116
117
118
119
120
# File 'lib/pdk/util.rb', line 111

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

.package_cachedirObject



90
91
92
# File 'lib/pdk/util.rb', line 90

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

.package_install?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/pdk/util.rb', line 68

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

.pdk_package_basedirObject



83
84
85
86
87
# File 'lib/pdk/util.rb', line 83

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

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

.puppetlabs_template_refObject



235
236
237
238
239
240
241
# File 'lib/pdk/util.rb', line 235

def puppetlabs_template_ref
  if PDK::Util.development_mode?
    'origin/master'
  else
    PDK::TEMPLATE_REF
  end
end

.puppetlabs_template_urlObject



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

def puppetlabs_template_url
  if package_install?
    'file://' + File.join(package_cachedir, 'pdk-templates.git')
  else
    'https://github.com/puppetlabs/pdk-templates'
  end
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



188
189
190
191
192
193
194
195
196
# File 'lib/pdk/util.rb', line 188

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