pdk 2.7.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +2 -48
  4. data/lib/pdk/analytics/client/google_analytics.rb +22 -26
  5. data/lib/pdk/analytics/util.rb +0 -1
  6. data/lib/pdk/analytics.rb +1 -1
  7. data/lib/pdk/bolt.rb +1 -0
  8. data/lib/pdk/cli/build.rb +53 -56
  9. data/lib/pdk/cli/bundle.rb +34 -33
  10. data/lib/pdk/cli/console.rb +136 -134
  11. data/lib/pdk/cli/convert.rb +39 -41
  12. data/lib/pdk/cli/env.rb +49 -47
  13. data/lib/pdk/cli/errors.rb +1 -2
  14. data/lib/pdk/cli/exec/command.rb +23 -29
  15. data/lib/pdk/cli/exec/interactive_command.rb +7 -12
  16. data/lib/pdk/cli/exec.rb +4 -11
  17. data/lib/pdk/cli/exec_group.rb +3 -2
  18. data/lib/pdk/cli/get/config.rb +21 -19
  19. data/lib/pdk/cli/get.rb +15 -13
  20. data/lib/pdk/cli/new/class.rb +22 -22
  21. data/lib/pdk/cli/new/defined_type.rb +22 -22
  22. data/lib/pdk/cli/new/fact.rb +19 -19
  23. data/lib/pdk/cli/new/function.rb +20 -20
  24. data/lib/pdk/cli/new/module.rb +40 -38
  25. data/lib/pdk/cli/new/provider.rb +19 -19
  26. data/lib/pdk/cli/new/task.rb +23 -23
  27. data/lib/pdk/cli/new/test.rb +50 -48
  28. data/lib/pdk/cli/new/transport.rb +18 -18
  29. data/lib/pdk/cli/new.rb +11 -9
  30. data/lib/pdk/cli/release/prep.rb +27 -25
  31. data/lib/pdk/cli/release/publish.rb +39 -37
  32. data/lib/pdk/cli/release.rb +152 -149
  33. data/lib/pdk/cli/remove/config.rb +63 -60
  34. data/lib/pdk/cli/remove.rb +15 -13
  35. data/lib/pdk/cli/set/config.rb +91 -89
  36. data/lib/pdk/cli/set.rb +15 -13
  37. data/lib/pdk/cli/test/unit.rb +71 -69
  38. data/lib/pdk/cli/test.rb +9 -7
  39. data/lib/pdk/cli/update.rb +33 -38
  40. data/lib/pdk/cli/util/command_redirector.rb +10 -1
  41. data/lib/pdk/cli/util/interview.rb +11 -4
  42. data/lib/pdk/cli/util/option_normalizer.rb +2 -4
  43. data/lib/pdk/cli/util/option_validator.rb +7 -9
  44. data/lib/pdk/cli/util/update_manager_printer.rb +4 -4
  45. data/lib/pdk/cli/util.rb +32 -48
  46. data/lib/pdk/cli/validate.rb +98 -96
  47. data/lib/pdk/cli.rb +124 -120
  48. data/lib/pdk/config/ini_file.rb +4 -3
  49. data/lib/pdk/config/ini_file_setting.rb +6 -10
  50. data/lib/pdk/config/json.rb +1 -0
  51. data/lib/pdk/config/json_schema_namespace.rb +5 -10
  52. data/lib/pdk/config/json_schema_setting.rb +3 -5
  53. data/lib/pdk/config/json_with_schema.rb +2 -4
  54. data/lib/pdk/config/namespace.rb +19 -13
  55. data/lib/pdk/config/setting.rb +5 -6
  56. data/lib/pdk/config/task_schema.json +116 -0
  57. data/lib/pdk/config/validator.rb +4 -4
  58. data/lib/pdk/config/yaml.rb +3 -8
  59. data/lib/pdk/config/yaml_with_schema.rb +4 -12
  60. data/lib/pdk/config.rb +47 -51
  61. data/lib/pdk/context/control_repo.rb +3 -2
  62. data/lib/pdk/context/module.rb +2 -2
  63. data/lib/pdk/context/none.rb +2 -2
  64. data/lib/pdk/context.rb +4 -5
  65. data/lib/pdk/control_repo.rb +3 -4
  66. data/lib/pdk/generate/defined_type.rb +3 -3
  67. data/lib/pdk/generate/fact.rb +3 -2
  68. data/lib/pdk/generate/function.rb +5 -4
  69. data/lib/pdk/generate/module.rb +91 -106
  70. data/lib/pdk/generate/provider.rb +5 -4
  71. data/lib/pdk/generate/puppet_class.rb +3 -3
  72. data/lib/pdk/generate/puppet_object.rb +9 -12
  73. data/lib/pdk/generate/task.rb +11 -10
  74. data/lib/pdk/generate/transport.rb +8 -7
  75. data/lib/pdk/generate.rb +1 -1
  76. data/lib/pdk/logger.rb +3 -2
  77. data/lib/pdk/module/build.rb +34 -49
  78. data/lib/pdk/module/convert.rb +13 -22
  79. data/lib/pdk/module/metadata.rb +53 -61
  80. data/lib/pdk/module/release.rb +19 -25
  81. data/lib/pdk/module/update.rb +4 -13
  82. data/lib/pdk/module/update_manager.rb +18 -25
  83. data/lib/pdk/module.rb +1 -1
  84. data/lib/pdk/monkey_patches.rb +268 -0
  85. data/lib/pdk/report/event.rb +12 -37
  86. data/lib/pdk/report.rb +4 -4
  87. data/lib/pdk/template/fetcher/git.rb +7 -8
  88. data/lib/pdk/template/fetcher/local.rb +1 -0
  89. data/lib/pdk/template/fetcher.rb +4 -2
  90. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +4 -9
  91. data/lib/pdk/template/renderer/v1/renderer.rb +19 -20
  92. data/lib/pdk/template/renderer/v1/template_file.rb +4 -8
  93. data/lib/pdk/template/renderer/v1.rb +1 -1
  94. data/lib/pdk/template/renderer.rb +1 -0
  95. data/lib/pdk/template/template_dir.rb +3 -3
  96. data/lib/pdk/template.rb +2 -6
  97. data/lib/pdk/tests/unit.rb +36 -25
  98. data/lib/pdk/util/bundler.rb +10 -14
  99. data/lib/pdk/util/changelog_generator.rb +15 -12
  100. data/lib/pdk/util/env.rb +1 -0
  101. data/lib/pdk/util/filesystem.rb +18 -17
  102. data/lib/pdk/util/git.rb +16 -21
  103. data/lib/pdk/util/json_finder.rb +7 -6
  104. data/lib/pdk/util/puppet_strings.rb +1 -1
  105. data/lib/pdk/util/puppet_version.rb +16 -67
  106. data/lib/pdk/util/ruby_version.rb +9 -13
  107. data/lib/pdk/util/template_uri.rb +17 -29
  108. data/lib/pdk/util/vendored_file.rb +5 -18
  109. data/lib/pdk/util/windows/api_types.rb +70 -64
  110. data/lib/pdk/util/windows/file.rb +31 -27
  111. data/lib/pdk/util/windows/process.rb +59 -61
  112. data/lib/pdk/util/windows/string.rb +19 -12
  113. data/lib/pdk/util.rb +12 -20
  114. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +1 -1
  115. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +25 -25
  116. data/lib/pdk/validate/external_command_validator.rb +6 -1
  117. data/lib/pdk/validate/internal_ruby_validator.rb +5 -4
  118. data/lib/pdk/validate/invokable_validator.rb +30 -20
  119. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +12 -14
  120. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +13 -15
  121. data/lib/pdk/validate/metadata/metadata_validator_group.rb +1 -1
  122. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +16 -18
  123. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +14 -14
  124. data/lib/pdk/validate/puppet/puppet_plan_syntax_validator.rb +1 -1
  125. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +16 -18
  126. data/lib/pdk/validate/puppet/puppet_validator_group.rb +1 -1
  127. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +10 -11
  128. data/lib/pdk/validate/ruby/ruby_validator_group.rb +1 -1
  129. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +19 -24
  130. data/lib/pdk/validate/tasks/tasks_name_validator.rb +11 -13
  131. data/lib/pdk/validate/tasks/tasks_validator_group.rb +1 -1
  132. data/lib/pdk/validate/validator.rb +4 -2
  133. data/lib/pdk/validate/validator_group.rb +6 -3
  134. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +27 -36
  135. data/lib/pdk/validate/yaml/yaml_validator_group.rb +1 -1
  136. data/lib/pdk/validate.rb +6 -6
  137. data/lib/pdk/version.rb +2 -2
  138. data/lib/pdk.rb +12 -12
  139. metadata +27 -35
  140. data/lib/pdk/cli/config/get.rb +0 -26
  141. data/lib/pdk/cli/config.rb +0 -22
  142. data/lib/pdk/cli/module/build.rb +0 -12
  143. data/lib/pdk/cli/module/generate.rb +0 -47
  144. data/lib/pdk/cli/module.rb +0 -14
data/lib/pdk/util/git.rb CHANGED
@@ -3,10 +3,7 @@ require 'pdk'
3
3
  module PDK
4
4
  module Util
5
5
  class GitError < StandardError
6
- attr_reader :stdout
7
- attr_reader :stderr
8
- attr_reader :exit_code
9
- attr_reader :args
6
+ attr_reader :stdout, :stderr, :exit_code, :args
10
7
 
11
8
  def initialze(args, result)
12
9
  @args = args
@@ -14,12 +11,12 @@ module PDK
14
11
  @stderr = result[:stderr]
15
12
  @exit_code = result[:exit_code]
16
13
 
17
- super('Git command failed: git %{args}' % { args: args.join(' ') })
14
+ super(format('Git command failed: git %{args}', args: args.join(' ')))
18
15
  end
19
16
  end
20
17
 
21
18
  module Git
22
- GIT_QUERY_CACHE_TTL ||= 10
19
+ GIT_QUERY_CACHE_TTL = 10
23
20
 
24
21
  def self.git_bindir
25
22
  @git_dir ||= File.join('private', 'git', Gem.win_platform? ? 'cmd' : 'bin')
@@ -67,6 +64,7 @@ module PDK
67
64
  def self.repo?(maybe_repo)
68
65
  result = cached_git_query(maybe_repo, :repo?)
69
66
  return result unless result.nil?
67
+
70
68
  result = if PDK::Util::Filesystem.directory?(maybe_repo)
71
69
  # Use boolean shortcircuiting here. The mostly likely type of git repo
72
70
  # is a "normal" repo with a working tree. Bare repos do not have work tree
@@ -90,6 +88,7 @@ module PDK
90
88
 
91
89
  def self.work_tree?(path)
92
90
  return false unless PDK::Util::Filesystem.directory?(path)
91
+
93
92
  result = cached_git_query(path, :work_tree?)
94
93
  return result unless result.nil?
95
94
 
@@ -100,30 +99,27 @@ module PDK
100
99
  end
101
100
 
102
101
  def self.work_dir_clean?(repo)
103
- raise PDK::CLI::ExitWithError, 'Unable to locate git work dir "%{workdir}"' % { workdir: repo } unless PDK::Util::Filesystem.directory?(repo)
104
- raise PDK::CLI::ExitWithError, 'Unable to locate git dir "%{gitdir}"' % { gitdir: repo } unless PDK::Util::Filesystem.directory?(File.join(repo, '.git'))
102
+ raise PDK::CLI::ExitWithError, format('Unable to locate git work dir "%{workdir}"', workdir: repo) unless PDK::Util::Filesystem.directory?(repo)
103
+ raise PDK::CLI::ExitWithError, format('Unable to locate git dir "%{gitdir}"', gitdir: repo) unless PDK::Util::Filesystem.directory?(File.join(repo, '.git'))
105
104
 
106
105
  git('--work-tree', repo, '--git-dir', File.join(repo, '.git'), 'status', '--untracked-files=no', '--porcelain', repo)[:stdout].empty?
107
106
  end
108
107
 
109
108
  def self.ls_remote(repo, ref)
110
- if PDK::Util::Filesystem.directory?(repo)
111
- repo = 'file://' + repo
112
- end
109
+ repo = "file://#{repo}" if PDK::Util::Filesystem.directory?(repo)
113
110
 
114
111
  output = git('ls-remote', '--refs', repo, ref)
115
112
 
116
113
  unless output[:exit_code].zero?
117
114
  PDK.logger.error output[:stdout]
118
115
  PDK.logger.error output[:stderr]
119
- raise PDK::CLI::ExitWithError, 'Unable to access the template repository "%{repository}"' % {
120
- repository: repo,
121
- }
116
+ raise PDK::CLI::ExitWithError, format('Unable to access the template repository "%{repository}"', repository: repo)
122
117
  end
123
118
 
124
- matching_refs = output[:stdout].split(%r{\r?\n}).map { |r| r.split("\t") }
125
- matching_ref = matching_refs.find { |_sha, remote_ref| remote_ref == "refs/tags/#{ref}" || remote_ref == "refs/remotes/origin/#{ref}" || remote_ref == "refs/heads/#{ref}" }
126
- raise PDK::CLI::ExitWithError, 'Unable to find a branch or tag named "%{ref}" in %{repo}' % { ref: ref, repo: repo } if matching_ref.nil?
119
+ matching_refs = output[:stdout].split(/\r?\n/).map { |r| r.split("\t") }
120
+ matching_ref = matching_refs.find { |_sha, remote_ref| ["refs/tags/#{ref}", "refs/remotes/origin/#{ref}", "refs/heads/#{ref}"].include?(remote_ref) }
121
+ raise PDK::CLI::ExitWithError, format('Unable to find a branch or tag named "%{ref}" in %{repo}', ref: ref, repo: repo) if matching_ref.nil?
122
+
127
123
  matching_ref.first
128
124
  end
129
125
 
@@ -131,6 +127,7 @@ module PDK
131
127
  args = ['--git-dir', path, 'describe', '--all', '--long', '--always', ref].compact
132
128
  result = git(*args)
133
129
  raise PDK::Util::GitError, args, result unless result[:exit_code].zero?
130
+
134
131
  result[:stdout].strip
135
132
  end
136
133
 
@@ -148,14 +145,12 @@ module PDK
148
145
 
149
146
  def self.cached_git_query(repo, query)
150
147
  # TODO: Not thread safe
151
- if @git_repo_expire_cache.nil?
148
+ if @git_repo_expire_cache.nil? || Time.now > @git_repo_expire_cache
152
149
  @git_repo_expire_cache = Time.now + GIT_QUERY_CACHE_TTL # Expire the cache every GIT_QUERY_CACHE_TTL seconds
153
150
  @git_repo_cache = {}
154
- elsif Time.now > @git_repo_expire_cache
155
- @git_repo_expire_cache = Time.now + GIT_QUERY_CACHE_TTL
156
- @git_repo_cache = {}
157
151
  end
158
152
  return nil if @git_repo_cache[repo].nil?
153
+
159
154
  @git_repo_cache[repo][query]
160
155
  end
161
156
  private_class_method :cached_git_query
@@ -33,13 +33,14 @@ module PDK
33
33
  @scanner.getch until @scanner.peek(1) == '{' || @scanner.eos?
34
34
 
35
35
  (@objects ||= []) << begin
36
- JSON.parse(read_object(true) || '')
37
- rescue JSON::ParserError
38
- nil
39
- end
36
+ JSON.parse(read_object(true) || '')
37
+ rescue JSON::ParserError
38
+ nil
39
+ end
40
40
  end
41
41
 
42
42
  return [] if @objects.nil?
43
+
43
44
  @objects = @objects.compact
44
45
  end
45
46
 
@@ -56,7 +57,7 @@ module PDK
56
57
  matched_text = new_object ? @scanner.getch : ''
57
58
 
58
59
  until @scanner.eos?
59
- text = @scanner.scan_until(%r{(?:(?<!\\)"|\{|\})})
60
+ text = @scanner.scan_until(/(?:(?<!\\)"|\{|\})/)
60
61
  unless text
61
62
  @scanner.terminate
62
63
  return nil
@@ -67,7 +68,7 @@ module PDK
67
68
  when '}'
68
69
  break
69
70
  when '"'
70
- text = @scanner.scan_until(%r{(?<!\\)"})
71
+ text = @scanner.scan_until(/(?<!\\)"/)
71
72
  unless text
72
73
  @scanner.terminate
73
74
  return nil
@@ -117,7 +117,7 @@ module PDK
117
117
  require 'pdk/generate'
118
118
 
119
119
  PDK::Generate.generators.find do |gen|
120
- gen.const_defined?(:PUPPET_STRINGS_TYPE) && gen::PUPPET_STRINGS_TYPE == type
120
+ gen.const_defined?(:PUPPET_STRINGS_TYPE) && type == gen::PUPPET_STRINGS_TYPE
121
121
  end
122
122
  end
123
123
  end
@@ -25,8 +25,8 @@ module PDK
25
25
  require 'pdk/util/ruby_version'
26
26
 
27
27
  {
28
- gem_version: 'file://%{path}' % { path: puppet_dev_path },
29
- ruby_version: PDK::Util::RubyVersion.latest_ruby_version,
28
+ gem_version: format('file://%{path}', path: puppet_dev_path),
29
+ ruby_version: PDK::Util::RubyVersion.latest_ruby_version
30
30
  }
31
31
  end
32
32
 
@@ -39,9 +39,7 @@ module PDK
39
39
  def latest_available
40
40
  latest = find_gem(Gem::Requirement.create('>= 0'))
41
41
 
42
- if latest.nil?
43
- raise ArgumentError, 'Unable to find a Puppet gem in current Ruby environment or from Rubygems.org.'
44
- end
42
+ raise ArgumentError, 'Unable to find a Puppet gem in current Ruby environment or from Rubygems.org.' if latest.nil?
45
43
 
46
44
  latest
47
45
  end
@@ -69,7 +67,7 @@ module PDK
69
67
 
70
68
  PDK.logger.error clone_result[:stdout]
71
69
  PDK.logger.error clone_result[:stderr]
72
- raise PDK::CLI::FatalError, "Unable to clone git repository from '%{repo}'." % { repo: DEFAULT_PUPPET_DEV_URL }
70
+ raise PDK::CLI::FatalError, format("Unable to clone git repository from '%{repo}'.", repo: DEFAULT_PUPPET_DEV_URL)
73
71
  end
74
72
 
75
73
  # Fetch Updates from remote repository
@@ -78,7 +76,7 @@ module PDK
78
76
  unless fetch_result[:exit_code].zero?
79
77
  PDK.logger.error fetch_result[:stdout]
80
78
  PDK.logger.error fetch_result[:stderr]
81
- raise PDK::CLI::FatalError, "Unable to fetch from git remote at '%{repo}'." % { repo: DEFAULT_PUPPET_DEV_URL }
79
+ raise PDK::CLI::FatalError, format("Unable to fetch from git remote at '%{repo}'.", repo: DEFAULT_PUPPET_DEV_URL)
82
80
  end
83
81
 
84
82
  # Reset local repo to latest
@@ -89,7 +87,7 @@ module PDK
89
87
 
90
88
  PDK.logger.error reset_result[:stdout]
91
89
  PDK.logger.error reset_result[:stderr]
92
- raise PDK::CLI::FatalError, "Unable to update git repository at '%{cachedir}'." % { cachedir: puppet_dev_path }
90
+ raise PDK::CLI::FatalError, format("Unable to update git repository at '%{cachedir}'.", cachedir: puppet_dev_path)
93
91
  end
94
92
 
95
93
  def find_gem_for(version_str)
@@ -108,18 +106,12 @@ module PDK
108
106
  latest_requirement = Gem::Requirement.create(requirement_string)
109
107
  latest_available_gem = find_gem(latest_requirement)
110
108
 
111
- if latest_available_gem.nil?
112
- raise ArgumentError, 'Unable to find a Puppet gem matching %{requirement}.' % {
113
- requirement: latest_requirement,
114
- }
115
- end
109
+ raise ArgumentError, format('Unable to find a Puppet gem matching %{requirement}.', requirement: latest_requirement) if latest_available_gem.nil?
116
110
 
117
111
  # Only issue this warning if they requested an exact version that isn't available.
118
112
  if version.segments.length == 3
119
- PDK.logger.warn('Puppet %{requested_version} is not available, activating %{found_version} instead.' % {
120
- requested_version: version_str,
121
- found_version: latest_available_gem[:gem_version].version,
122
- })
113
+ PDK.logger.warn(format('Puppet %{requested_version} is not available, activating %{found_version} instead.', requested_version: version_str,
114
+ found_version: latest_available_gem[:gem_version].version))
123
115
  end
124
116
 
125
117
  latest_available_gem
@@ -134,20 +126,13 @@ module PDK
134
126
  safe_versions = {
135
127
  2023 => '7.23.0',
136
128
  2021 => '7.23.0',
137
- 2019 => '6.29.0',
129
+ 2019 => '6.29.0'
138
130
  }
139
131
 
140
132
  gem_version = safe_versions[version.segments[0]]
141
- if gem_version.nil?
142
- raise ArgumentError, 'Unable to map Puppet Enterprise version %{pe_version} to a Puppet version.' % {
143
- pe_version: version_str,
144
- }
145
- end
133
+ raise ArgumentError, format('Unable to map Puppet Enterprise version %{pe_version} to a Puppet version.', pe_version: version_str) if gem_version.nil?
146
134
 
147
- PDK.logger.info 'Puppet Enterprise %{pe_version} maps to Puppet %{puppet_version}.' % {
148
- pe_version: version_str,
149
- puppet_version: gem_version,
150
- }
135
+ PDK.logger.info format('Puppet Enterprise %{pe_version} maps to Puppet %{puppet_version}.', pe_version: version_str, puppet_version: gem_version)
151
136
 
152
137
  find_gem_for(gem_version)
153
138
  end
@@ -172,7 +157,7 @@ module PDK
172
157
 
173
158
  # Split combined requirements like ">= 4.7.0 < 6.0.0" into their
174
159
  # component requirements [">= 4.7.0", "< 6.0.0"]
175
- pattern = %r{#{Gem::Requirement::PATTERN_RAW}}
160
+ pattern = /#{Gem::Requirement::PATTERN_RAW}/o
176
161
  requirement_strings = metadata_requirement['version_requirement'].scan(pattern).map do |req|
177
162
  req.compact.join(' ')
178
163
  end
@@ -186,47 +171,11 @@ module PDK
186
171
  def parse_specified_version(version_str)
187
172
  Gem::Version.new(version_str)
188
173
  rescue ArgumentError
189
- raise ArgumentError, '%{version} is not a valid version number.' % {
190
- version: version_str,
191
- }
192
- end
193
-
194
- def pe_version_map
195
- @pe_version_map ||= fetch_pe_version_map.map { |version_map|
196
- maps = version_map['versions'].map do |pe_release|
197
- requirements = ["= #{pe_release['version']}"]
198
-
199
- # Some PE release have a .0 Z release, which causes problems when
200
- # the user specifies "X.Y" expecting to get the latest Z and
201
- # instead getting the oldest.
202
- requirements << "!= #{pe_release['version'].gsub(%r{\.\d+\Z}, '')}" if pe_release['version'].end_with?('.0')
203
- {
204
- requirement: Gem::Requirement.create(requirements),
205
- gem_version: pe_release['puppet'],
206
- }
207
- end
208
-
209
- maps << {
210
- requirement: requirement_from_forge_range(version_map['release']),
211
- gem_version: version_map['versions'].find { |r| r['version'] == version_map['latest'] }['puppet'],
212
- }
213
- }.flatten
214
- end
215
-
216
- def fetch_pe_version_map
217
- require 'pdk/util/vendored_file'
218
-
219
- map = PDK::Util::VendoredFile.new('pe_versions.json', PE_VERSIONS_URL).read
220
-
221
- JSON.parse(map)
222
- rescue PDK::Util::VendoredFile::DownloadError => e
223
- raise PDK::CLI::FatalError, e.message
224
- rescue JSON::ParserError
225
- raise PDK::CLI::FatalError, 'Failed to parse Puppet Enterprise version map file.'
174
+ raise ArgumentError, format('%{version} is not a valid version number.', version: version_str)
226
175
  end
227
176
 
228
177
  def requirement_from_forge_range(range_str)
229
- Gem::Requirement.create("~> #{range_str.gsub(%r{\.x\Z}, '.0')}")
178
+ Gem::Requirement.create("~> #{range_str.gsub(/\.x\Z/, '.0')}")
230
179
  end
231
180
 
232
181
  def rubygems_puppet_versions
@@ -236,7 +185,7 @@ module PDK
236
185
  spec_tuple.name == 'puppet' && Gem::Platform.match(spec_tuple.platform)
237
186
  end
238
187
  puppet_versions = puppet_tuples.map { |name, _| name.version }.uniq
239
- puppet_versions.sort { |a, b| b <=> a }
188
+ puppet_versions.sort.reverse
240
189
  end
241
190
  end
242
191
 
@@ -31,23 +31,19 @@ module PDK
31
31
  end
32
32
 
33
33
  def use(version)
34
- if versions.key?(version)
35
- @active_ruby_version = version
36
- else
37
- raise ArgumentError, 'Unknown Ruby version "%{ruby_version}"' % {
38
- ruby_version: version,
39
- }
40
- end
34
+ raise ArgumentError, format('Unknown Ruby version "%{ruby_version}"', ruby_version: version) unless versions.key?(version)
35
+
36
+ @active_ruby_version = version
41
37
  end
42
38
 
43
39
  def scan_for_packaged_rubies
44
40
  require 'pdk/util'
45
41
 
46
42
  ruby_basedir = File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', '*')
47
- PDK::Util::Filesystem.glob(ruby_basedir).sort.map { |ruby_dir|
43
+ PDK::Util::Filesystem.glob(ruby_basedir).sort.map do |ruby_dir|
48
44
  version = File.basename(ruby_dir)
49
- [version, version.split('.').take(2).concat(['0']).join('.')]
50
- }.reverse.to_h
45
+ [version, version.split('.').take(2).push('0').join('.')]
46
+ end.reverse.to_h
51
47
  end
52
48
 
53
49
  def default_ruby_version
@@ -71,7 +67,7 @@ module PDK
71
67
  end
72
68
 
73
69
  def latest_ruby_version
74
- versions.keys.sort { |a, b| Gem::Version.new(b) <=> Gem::Version.new(a) }.first
70
+ versions.keys.min { |a, b| Gem::Version.new(b) <=> Gem::Version.new(a) }
75
71
  end
76
72
 
77
73
  def versions
@@ -111,7 +107,7 @@ module PDK
111
107
  [
112
108
  File.join(PDK::Util.pdk_package_basedir, 'private', 'ruby', ruby_version, 'lib', 'ruby', 'gems', versions[ruby_version]),
113
109
  File.join(PDK::Util.package_cachedir, 'ruby', versions[ruby_version]),
114
- File.join(PDK::Util.pdk_package_basedir, 'private', 'puppet', 'ruby', versions[ruby_version]),
110
+ File.join(PDK::Util.pdk_package_basedir, 'private', 'puppet', 'ruby', versions[ruby_version])
115
111
  ]
116
112
  else
117
113
  # This allows the subprocess to find the 'bundler' gem, which isn't
@@ -155,7 +151,7 @@ module PDK
155
151
  puppet_specs << spec if spec.name == 'puppet'
156
152
  end
157
153
 
158
- @available_puppet_versions = puppet_specs.map(&:version).sort { |a, b| b <=> a }
154
+ @available_puppet_versions = puppet_specs.map(&:version).sort.reverse
159
155
  end
160
156
 
161
157
  private
@@ -3,7 +3,7 @@ require 'pdk'
3
3
  module PDK
4
4
  module Util
5
5
  class TemplateURI
6
- SCP_PATTERN = %r{\A(?!\w+://)(?:(?<user>.+?)@)?(?<host>[^:/]+):(?<path>.+)\z}
6
+ SCP_PATTERN = %r{\A(?!\w+://)(?:(?<user>.+?)@)?(?<host>[^:/]+):(?<path>.+)\z}.freeze
7
7
 
8
8
  PACKAGED_TEMPLATE_KEYWORD = 'pdk-default'.freeze
9
9
  DEPRECATED_TEMPLATE_URL = 'https://github.com/puppetlabs/pdk-module-template'.freeze
@@ -11,8 +11,8 @@ module PDK
11
11
 
12
12
  LEGACY_PACKAGED_TEMPLATE_PATHS = {
13
13
  'windows' => 'file:///C:/Program Files/Puppet Labs/DevelopmentKit/share/cache/pdk-templates.git',
14
- 'macos' => 'file:///opt/puppetlabs/pdk/share/cache/pdk-templates.git',
15
- 'linux' => 'file:///opt/puppetlabs/pdk/share/cache/pdk-templates.git',
14
+ 'macos' => 'file:///opt/puppetlabs/pdk/share/cache/pdk-templates.git',
15
+ 'linux' => 'file:///opt/puppetlabs/pdk/share/cache/pdk-templates.git'
16
16
  }.freeze
17
17
 
18
18
  # XXX Previously
@@ -37,9 +37,10 @@ module PDK
37
37
  def initialize(opts_or_uri)
38
38
  require 'addressable'
39
39
  # If a uri string is passed, skip the valid uri finding code.
40
- @uri = if opts_or_uri.is_a?(self.class)
40
+ @uri = case opts_or_uri
41
+ when self.class
41
42
  opts_or_uri.uri
42
- elsif opts_or_uri.is_a?(String)
43
+ when String
43
44
  begin
44
45
  uri, ref = opts_or_uri.split('#', 2)
45
46
  if PDK::Util::TemplateURI.packaged_template?(uri)
@@ -48,9 +49,9 @@ module PDK
48
49
  Addressable::URI.parse(opts_or_uri)
49
50
  end
50
51
  rescue Addressable::URI::InvalidURIError
51
- raise PDK::CLI::FatalError, 'PDK::Util::TemplateURI attempted initialization with a non-uri string: {string}' % { string: opts_or_uri }
52
+ raise PDK::CLI::FatalError, "PDK::Util::TemplateURI attempted initialization with a non-uri string: #{opts_or_uri}"
52
53
  end
53
- elsif opts_or_uri.is_a?(Addressable::URI)
54
+ when Addressable::URI
54
55
  opts_or_uri.dup
55
56
  else
56
57
  PDK::Util::TemplateURI.first_valid_uri(PDK::Util::TemplateURI.templates(opts_or_uri))
@@ -110,7 +111,7 @@ module PDK
110
111
  require 'addressable'
111
112
 
112
113
  if uri.is_a?(Addressable::URI) && uri.fragment
113
- human_readable(uri.to_s.chomp('#' + uri.fragment))
114
+ human_readable(uri.to_s.chomp("##{uri.fragment}"))
114
115
  else
115
116
  human_readable(uri.to_s)
116
117
  end
@@ -149,7 +150,7 @@ module PDK
149
150
  #
150
151
  # @returns String
151
152
  def self.uri_safe(string)
152
- url = (Gem.win_platform? && string =~ %r{^[a-zA-Z][\|:]}) ? "/#{string}" : string
153
+ url = Gem.win_platform? && string =~ /^[a-zA-Z][|:]/ ? "/#{string}" : string
153
154
  parse_scp_url(url)
154
155
  end
155
156
 
@@ -159,7 +160,7 @@ module PDK
159
160
  #
160
161
  # @returns String
161
162
  def self.human_readable(string)
162
- (Gem.win_platform? && string =~ %r{^\/[a-zA-Z][\|:]}) ? string[1..-1] : string
163
+ Gem.win_platform? && string =~ %r{^/[a-zA-Z][|:]} ? string[1..] : string
163
164
  end
164
165
 
165
166
  def self.parse_scp_url(url)
@@ -178,10 +179,7 @@ module PDK
178
179
  return url unless Pathname.new(url).relative? && scp_url
179
180
 
180
181
  uri = Addressable::URI.new(scheme: 'ssh', user: scp_url[:user], host: scp_url[:host], path: scp_url[:path])
181
- PDK.logger.warn '%{scp_uri} appears to be an SCP style URL; it will be converted to an RFC compliant URI: %{rfc_uri}' % {
182
- scp_uri: url,
183
- rfc_uri: uri.to_s,
184
- }
182
+ PDK.logger.warn format('%{scp_uri} appears to be an SCP style URL; it will be converted to an RFC compliant URI: %{rfc_uri}', scp_uri: url, rfc_uri: uri.to_s)
185
183
 
186
184
  uri.to_s
187
185
  end
@@ -208,22 +206,14 @@ module PDK
208
206
  else
209
207
  explicit_uri = nil
210
208
  end
211
- metadata_uri = if PDK::Util.module_root && PDK::Util::Filesystem.file?(File.join(PDK::Util.module_root, 'metadata.json'))
212
- if PDK::Util.module_metadata['template-url']
213
- new(uri_safe(PDK::Util.module_metadata['template-url'])).uri
214
- else
215
- nil
216
- end
217
- else
218
- nil
209
+ metadata_uri = if PDK::Util.module_root && PDK::Util::Filesystem.file?(File.join(PDK::Util.module_root, 'metadata.json')) && PDK::Util.module_metadata['template-url']
210
+ new(uri_safe(PDK::Util.module_metadata['template-url'])).uri
219
211
  end
220
212
  default_template_url = PDK.config.get_within_scopes('module_defaults.template-url')
221
213
  answers_uri = if [PACKAGED_TEMPLATE_KEYWORD, DEPRECATED_TEMPLATE_URL].include?(default_template_url)
222
214
  Addressable::URI.parse(default_template_uri)
223
215
  elsif default_template_url
224
216
  new(uri_safe(default_template_url)).uri
225
- else
226
- nil
227
217
  end
228
218
  default_uri = default_template_uri.uri
229
219
  default_uri.fragment = default_template_ref(default_template_uri)
@@ -257,6 +247,7 @@ module PDK
257
247
  found_template = templates_array.find { |t| valid_template?(t) }
258
248
 
259
249
  raise PDK::CLI::FatalError, 'Unable to find a valid module template to use.' if found_template.nil?
250
+
260
251
  found_template[:uri]
261
252
  end
262
253
 
@@ -267,6 +258,7 @@ module PDK
267
258
  return false if template[:uri].nil? || !template[:uri].is_a?(Addressable::URI)
268
259
 
269
260
  return true if PDK::Util::Git.repo?(bare_uri(template[:uri]))
261
+
270
262
  path = human_readable(template[:uri].path)
271
263
  if PDK::Util::Filesystem.directory?(path)
272
264
  # We know that it's not a git repository, but it's a valid path on disk
@@ -278,11 +270,7 @@ module PDK
278
270
  end
279
271
  end
280
272
 
281
- unless template[:allow_fallback]
282
- raise PDK::CLI::FatalError, 'Unable to find a valid template at %{uri}' % {
283
- uri: template[:uri].to_s,
284
- }
285
- end
273
+ raise PDK::CLI::FatalError, format('Unable to find a valid template at %{uri}', uri: template[:uri].to_s) unless template[:allow_fallback]
286
274
 
287
275
  false
288
276
  end
@@ -5,8 +5,7 @@ module PDK
5
5
  class VendoredFile
6
6
  class DownloadError < StandardError; end
7
7
 
8
- attr_reader :file_name
9
- attr_reader :url
8
+ attr_reader :file_name, :url
10
9
 
11
10
  def initialize(file_name, url)
12
11
  @file_name = file_name
@@ -44,13 +43,10 @@ module PDK
44
43
  Net::HTTPBadResponse,
45
44
  Net::HTTPHeaderSyntaxError,
46
45
  Net::ProtocolError,
47
- Timeout::Error,
46
+ Timeout::Error
48
47
  ]
49
48
 
50
- PDK.logger.debug '%{file_name} was not found in the cache, downloading it from %{url}.' % {
51
- file_name: file_name,
52
- url: url,
53
- }
49
+ PDK.logger.debug format('%{file_name} was not found in the cache, downloading it from %{url}.', file_name: file_name, url: url)
54
50
 
55
51
  uri = URI.parse(url)
56
52
  http = Net::HTTP.new(uri.host, uri.port)
@@ -60,20 +56,11 @@ module PDK
60
56
  request = Net::HTTP::Get.new(uri.request_uri)
61
57
  response = http.request(request)
62
58
 
63
- unless response.code == '200'
64
- raise DownloadError, 'Unable to download %{url}. %{code}: %{message}.' % {
65
- url: url,
66
- code: response.code,
67
- message: response.message,
68
- }
69
- end
59
+ raise DownloadError, format('Unable to download %{url}. %{code}: %{message}.', url: url, code: response.code, message: response.message) unless response.code == '200'
70
60
 
71
61
  response.body
72
62
  rescue *http_errors => e
73
- raise DownloadError, 'Unable to download %{url}. Check internet connectivity and try again. %{error}' % {
74
- url: url,
75
- error: e,
76
- }
63
+ raise DownloadError, format('Unable to download %{url}. Check internet connectivity and try again. %{error}', url: url, error: e)
77
64
  end
78
65
 
79
66
  def package_vendored_path