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/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
Defined Under Namespace
Modules: Bundler, Filesystem, Git, PuppetStrings, Version, Windows Classes: Env, GitError, 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
-
.cachedir ⇒ String
Returns the fully qualified path to a per-user PDK cachedir.
-
.canonical_path(path) ⇒ String
Return an expanded, absolute path.
- .configdir ⇒ 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.
-
.find_valid_json_in(text, opts = {}) ⇒ Hash, ...
Iterate through possible JSON documents until we find one that is valid.
- .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 ⇒ Object
TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead.
-
.module_pdk_compatible? ⇒ 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
-
.targets_relative_to_pwd(targets) ⇒ Array<String>
Returns the targets’ paths relative to the working directory.
Class Method Details
.cachedir ⇒ String
Returns the fully qualified path to a per-user PDK cachedir.
120 121 122 123 124 125 126 |
# File 'lib/pdk/util.rb', line 120 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
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/pdk/util.rb', line 73 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 PDK::Util::Windows::File.get_long_pathname(path) else File.(path) end end |
.configdir ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/pdk/util.rb', line 129 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 |
.development_mode? ⇒ Boolean
92 93 94 95 96 |
# File 'lib/pdk/util.rb', line 92 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
188 189 190 |
# File 'lib/pdk/util.rb', line 188 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.
178 179 180 |
# File 'lib/pdk/util.rb', line 178 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.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/pdk/util.rb', line 41 def find_upwards(target, start_dir = nil) previous = nil current = File.(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.('..', current) end end |
.find_valid_json_in(text, opts = {}) ⇒ Hash, ...
Iterate through possible JSON documents until we find one that is valid.
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/pdk/util.rb', line 203 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
99 100 101 |
# File 'lib/pdk/util.rb', line 99 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.
168 169 170 |
# File 'lib/pdk/util.rb', line 168 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.
59 60 61 62 63 64 65 |
# File 'lib/pdk/util.rb', line 59 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
156 157 158 159 |
# File 'lib/pdk/util.rb', line 156 def module_fixtures_dir dir = module_root File.join(module_root, 'spec', 'fixtures') unless dir.nil? end |
.module_metadata ⇒ Object
TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead
240 241 242 243 244 |
# File 'lib/pdk/util.rb', line 240 def require 'pdk/module/metadata' 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
248 249 250 |
# File 'lib/pdk/util.rb', line 248 def module_pdk_compatible? ['pdk-version', 'template-url'].any? { |key| .key?(key) } end |
.module_pdk_version ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/pdk/util.rb', line 253 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.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/pdk/util.rb', line 142 def module_root = find_upwards('metadata.json') if File.dirname() elsif in_module_root? Dir.pwd else nil end end |
.package_cachedir ⇒ Object
112 113 114 |
# File 'lib/pdk/util.rb', line 112 def package_cachedir File.join(pdk_package_basedir, 'share', 'cache') end |
.package_install? ⇒ Boolean
85 86 87 88 89 |
# File 'lib/pdk/util.rb', line 85 def package_install? require 'pdk/util/version' !PDK::Util::Version.version_file.nil? end |
.pdk_package_basedir ⇒ Object
104 105 106 107 108 109 |
# File 'lib/pdk/util.rb', line 104 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 |
.targets_relative_to_pwd(targets) ⇒ Array<String>
Returns the targets’ paths relative to the working directory
228 229 230 231 232 233 234 235 236 |
# File 'lib/pdk/util.rb', line 228 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 |