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/windows/file.rb,
lib/pdk/util/vendored_file.rb,
lib/pdk/util/puppet_strings.rb,
lib/pdk/util/puppet_version.rb,
lib/pdk/util/windows/string.rb,
lib/pdk/util/windows/process.rb,
lib/pdk/util/windows/api_types.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 =
['manifests', 'lib/puppet', 'lib/puppet_x', 'lib/facter', 'tasks', 'facts.d', 'functions', 'types'].freeze
Class Method Summary collapse
-
.cachedir ⇒ String
Returns the fully qualified path to a per-user PDK cachedir.
-
.canonical_path(path) ⇒ String
Return an expanded, absolute path.
- .configdir ⇒ Object
-
.deep_duplicate(object) ⇒ Object
Does a deep copy instead of a shallow copy of an object.
- .development_mode? ⇒ Boolean
-
.find_all_json_in(text) ⇒ Array<Hash>
Iterate through possible JSON documents for all valid JSON.
-
.find_first_json_in(text) ⇒ Hash?
Iterate through possible JSON documents until we find one that is valid.
-
.find_upwards(target, start_dir = nil) ⇒ String?
Searches upwards from current working directory for the given target file.
- .gem_install? ⇒ Boolean
-
.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.
-
.make_tmpdir_name(base) ⇒ String
Generate a name for a temporary directory.
-
.module_fixtures_dir ⇒ String
The module’s fixtures directory for spec testing.
-
.module_metadata(module_path = nil) ⇒ Object
TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead.
-
.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.
- .module_pdk_version ⇒ Object
-
.module_root ⇒ String?
Returns path to the root of the module being worked on.
- .package_cachedir ⇒ Object
- .package_install? ⇒ Boolean
- .pdk_package_basedir ⇒ Object
- .system_configdir ⇒ Object
-
.targets_relative_to_pwd(targets) ⇒ Array<String>
Returns the targets’ paths relative to the working directory.
Instance Method Summary collapse
-
#exit_process(exit_code) ⇒ Object
Calls Kernel.exit with an exitcode.
Class Method Details
.cachedir ⇒ String
Returns the fully qualified path to a per-user PDK cachedir.
124 125 126 127 128 129 130 |
# File 'lib/pdk/util.rb', line 124 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/pdk/util.rb', line 77 def canonical_path(path) if Gem.win_platform? raise PDK::CLI::FatalError, format("Cannot resolve a full path to '%{path}', as it does not currently exist.", path: path) unless PDK::Util::Filesystem.exist?(path) PDK::Util::Windows::File.get_long_pathname(path) else PDK::Util::Filesystem.(path) end end |
.configdir ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/pdk/util.rb', line 133 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.
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/pdk/util.rb', line 256 def deep_duplicate(object) case object when Array object.map { |item| deep_duplicate(item) } when Hash hash = object.dup hash.each_pair { |key, value| hash[key] = deep_duplicate(value) } hash else object end end |
.development_mode? ⇒ Boolean
95 96 97 98 99 |
# File 'lib/pdk/util.rb', line 95 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
201 202 203 |
# File 'lib/pdk/util.rb', line 201 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.
191 192 193 |
# File 'lib/pdk/util.rb', line 191 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.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pdk/util.rb', line 44 def find_upwards(target, start_dir = nil) previous = nil current = PDK::Util::Filesystem.(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.('..', current) end end |
.gem_install? ⇒ Boolean
102 103 104 |
# File 'lib/pdk/util.rb', line 102 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.
180 181 182 183 |
# File 'lib/pdk/util.rb', line 180 def in_module_root?(path = Dir.pwd) PDK::Util::MODULE_FOLDERS.any? { |dir| PDK::Util::Filesystem.directory?(File.join(path, dir)) } || PDK::Util::Filesystem.file?(File.join(path, 'metadata.json')) end |
.make_tmpdir_name(base) ⇒ String
Generate a name for a temporary directory.
63 64 65 66 67 68 69 |
# File 'lib/pdk/util.rb', line 63 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_dir ⇒ String
The module’s fixtures directory for spec testing
168 169 170 171 |
# File 'lib/pdk/util.rb', line 168 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
222 223 224 225 226 |
# File 'lib/pdk/util.rb', line 222 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
231 232 233 |
# File 'lib/pdk/util.rb', line 231 def module_pdk_compatible?(module_path = nil) ['pdk-version', 'template-url'].any? { |key| (module_path).key?(key) } end |
.module_pdk_version ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/pdk/util.rb', line 236 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_root ⇒ String?
Returns path to the root of the module being worked on.
156 157 158 159 160 161 162 163 |
# File 'lib/pdk/util.rb', line 156 def module_root = find_upwards('metadata.json') if File.dirname() elsif in_module_root? Dir.pwd end end |
.package_cachedir ⇒ Object
116 117 118 |
# File 'lib/pdk/util.rb', line 116 def package_cachedir File.join(pdk_package_basedir, 'share', 'cache') end |
.package_install? ⇒ Boolean
88 89 90 91 92 |
# File 'lib/pdk/util.rb', line 88 def package_install? require 'pdk/util/version' !PDK::Util::Version.version_file.nil? end |
.pdk_package_basedir ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/pdk/util.rb', line 107 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_configdir ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'lib/pdk/util.rb', line 142 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
209 210 211 212 213 214 215 216 217 |
# File 'lib/pdk/util.rb', line 209 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
32 33 34 |
# File 'lib/pdk/util.rb', line 32 def exit_process(exit_code) exit exit_code end |