Module: PDK::Util
- Defined in:
- lib/pdk/util.rb,
lib/pdk/util/git.rb,
lib/pdk/util/bundler.rb,
lib/pdk/util/version.rb
Defined Under Namespace
Modules: Bundler, Git, Version
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.
- .default_template_ref ⇒ Object
- .default_template_url ⇒ 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
-
.make_tmpdir_name(base) ⇒ String
Generate a name for a temporary directory.
-
.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_root ⇒ String?
Returns path to the root of the module being worked on.
- .package_cachedir ⇒ Object
- .package_install? ⇒ Boolean
- .pdk_package_basedir ⇒ Object
- .puppetlabs_template_ref ⇒ Object
- .puppetlabs_template_url ⇒ 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.
86 87 88 89 90 91 92 |
# File 'lib/pdk/util.rb', line 86 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
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.(path) end end |
.default_template_ref ⇒ Object
203 204 205 206 207 208 |
# File 'lib/pdk/util.rb', line 203 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_url ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/pdk/util.rb', line 175 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_exists?(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
61 62 63 |
# File 'lib/pdk/util.rb', line 61 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
124 125 126 |
# File 'lib/pdk/util.rb', line 124 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.
114 115 116 |
# File 'lib/pdk/util.rb', line 114 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.
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.(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.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pdk/util.rb', line 139 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
66 67 68 |
# File 'lib/pdk/util.rb', line 66 def gem_install? !(package_install? || development_mode?) end |
.make_tmpdir_name(base) ⇒ String
Generate a name for a temporary directory.
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_metadata ⇒ Object
TO-DO: Refactor replacement of lib/pdk/module/build.rb:metadata to use this function instead
221 222 223 |
# File 'lib/pdk/util.rb', line 221 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
227 228 229 |
# File 'lib/pdk/util.rb', line 227 def module_pdk_compatible? ['pdk-version', 'template-url'].any? { |key| .key?(key) } end |
.module_root ⇒ String?
Returns path to the root of the module being worked on.
99 100 101 102 103 104 105 106 |
# File 'lib/pdk/util.rb', line 99 def module_root = find_upwards('metadata.json') if File.dirname() else nil end end |
.package_cachedir ⇒ Object
78 79 80 |
# File 'lib/pdk/util.rb', line 78 def package_cachedir File.join(pdk_package_basedir, 'share', 'cache') end |
.package_install? ⇒ Boolean
56 57 58 |
# File 'lib/pdk/util.rb', line 56 def package_install? !PDK::Util::Version.version_file.nil? end |
.pdk_package_basedir ⇒ Object
71 72 73 74 75 |
# File 'lib/pdk/util.rb', line 71 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_ref ⇒ Object
211 212 213 214 215 216 217 |
# File 'lib/pdk/util.rb', line 211 def puppetlabs_template_ref if PDK::Util.development_mode? 'origin/master' else PDK::TEMPLATE_REF end end |
.puppetlabs_template_url ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/pdk/util.rb', line 194 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
164 165 166 167 168 169 170 171 172 |
# File 'lib/pdk/util.rb', line 164 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 |