Module: PDK::Util

Defined in:
lib/pdk/util.rb,
lib/pdk/util/bundler.rb,
lib/pdk/util/version.rb

Defined Under Namespace

Modules: Bundler, Version

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.



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

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/pdk/util.rb', line 44

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

.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



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

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



109
110
111
# File 'lib/pdk/util.rb', line 109

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.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pdk/util.rb', line 16

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/pdk/util.rb', line 134

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)


61
62
63
# File 'lib/pdk/util.rb', line 61

def gem_install?
  !package_install?
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.



34
35
36
# File 'lib/pdk/util.rb', line 34

def make_tmpdir_name(base)
  Dir::Tmpname.make_tmpname(File.join(Dir.tmpdir, base), 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.



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

def module_root
   = find_upwards('metadata.json')
  if 
    File.dirname()
  else
    nil
  end
end

.package_cachedirObject



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

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

.package_install?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/pdk/util.rb', line 56

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

.pdk_package_basedirObject



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

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

.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



159
160
161
162
163
164
165
166
167
# File 'lib/pdk/util.rb', line 159

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