puppetlabs_spec_helper 2.7.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Initialize puppet for testing by loading the
2
4
  # 'puppetlabs_spec_helper/puppet_spec_helper' library
3
5
  require 'puppetlabs_spec_helper/puppet_spec_helper'
@@ -8,9 +10,9 @@ module PuppetlabsSpec
8
10
  # instance suitable for placing in a test harness with the intent of
9
11
  # testing parser functions from modules.
10
12
  def scope(parts = {})
11
- RSpec.deprecate('scope', :replacement => 'rspec-puppet 2.2.0 provides a scope property')
13
+ RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')
12
14
 
13
- if Puppet.version =~ /^2\.[67]/
15
+ if %r{^2\.[67]}.match?(Puppet.version)
14
16
  # loadall should only be necessary prior to 3.x
15
17
  # Please note, loadall needs to happen first when creating a scope, otherwise
16
18
  # you might receive undefined method `function_*' errors
@@ -19,15 +21,15 @@ module PuppetlabsSpec
19
21
 
20
22
  scope_compiler = parts[:compiler] || compiler
21
23
  scope_parent = parts[:parent] || scope_compiler.topscope
22
- scope_resource = parts[:resource] || resource(:type => :node, :title => scope_compiler.node.name)
24
+ scope_resource = parts[:resource] || resource(type: :node, title: scope_compiler.node.name)
23
25
 
24
- if Puppet.version =~ /^2\.[67]/
25
- scope = Puppet::Parser::Scope.new(:compiler => scope_compiler)
26
- else
27
- scope = Puppet::Parser::Scope.new(scope_compiler)
28
- end
26
+ scope = if %r{^2\.[67]}.match?(Puppet.version)
27
+ Puppet::Parser::Scope.new(compiler: scope_compiler)
28
+ else
29
+ Puppet::Parser::Scope.new(scope_compiler)
30
+ end
29
31
 
30
- scope.source = Puppet::Resource::Type.new(:node, "foo")
32
+ scope.source = Puppet::Resource::Type.new(:node, 'foo')
31
33
  scope.parent = scope_parent
32
34
  scope
33
35
  end
@@ -35,13 +37,13 @@ module PuppetlabsSpec
35
37
 
36
38
  def resource(parts = {})
37
39
  resource_type = parts[:type] || :hostclass
38
- resource_name = parts[:name] || "testing"
40
+ resource_name = parts[:name] || 'testing'
39
41
  Puppet::Resource::Type.new(resource_type, resource_name)
40
42
  end
41
43
  module_function :resource
42
44
 
43
45
  def compiler(parts = {})
44
- compiler_node = parts[:node] || node()
46
+ compiler_node = parts[:node] || node
45
47
  Puppet::Parser::Compiler.new(compiler_node)
46
48
  end
47
49
  module_function :compiler
@@ -49,12 +51,12 @@ module PuppetlabsSpec
49
51
  def node(parts = {})
50
52
  node_name = parts[:name] || 'testinghost'
51
53
  options = parts[:options] || {}
52
- if Puppet.version.to_f >= 4.0
53
- node_environment = Puppet::Node::Environment.create(parts[:environment] || 'test', [])
54
- else
55
- node_environment = Puppet::Node::Environment.new(parts[:environment] || 'test')
56
- end
57
- options.merge!({:environment => node_environment})
54
+ node_environment = if Puppet.version.to_f >= 4.0
55
+ Puppet::Node::Environment.create(parts[:environment] || 'test', [])
56
+ else
57
+ Puppet::Node::Environment.new(parts[:environment] || 'test')
58
+ end
59
+ options[:environment] = node_environment
58
60
  Puppet::Node.new(node_name, options)
59
61
  end
60
62
  module_function :node
@@ -67,6 +69,7 @@ module PuppetlabsSpec
67
69
  # exists. This is a hack, but at least it's a hidden hack and not an
68
70
  # exposed hack.
69
71
  return nil unless Puppet::Parser::Functions.function(name)
72
+
70
73
  scope.method("function_#{name}".intern)
71
74
  end
72
75
  module_function :function_method
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Define the main module namespace for use by the helper modules
2
4
  module PuppetlabsSpec
3
5
  # FIXTURE_DIR represents the standard locations of all fixture data. Normally
4
6
  # this represents <project>/spec/fixtures. This will be used by the fixtures
5
7
  # library to find relative fixture data.
6
- FIXTURE_DIR = File.join("spec", "fixtures") unless defined?(FIXTURE_DIR)
8
+ FIXTURE_DIR = File.join('spec', 'fixtures') unless defined?(FIXTURE_DIR)
7
9
  end
8
10
 
9
11
  # Require all necessary helper libraries so they can be used later
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'fileutils'
2
4
  require 'rake'
3
5
  require 'rspec/core/rake_task'
@@ -6,6 +8,8 @@ require 'pathname'
6
8
  require 'puppetlabs_spec_helper/version'
7
9
  require 'puppetlabs_spec_helper/tasks/beaker'
8
10
  require 'puppetlabs_spec_helper/tasks/fixtures'
11
+ require 'puppetlabs_spec_helper/tasks/check_symlinks'
12
+ require 'English'
9
13
 
10
14
  # optional gems
11
15
  begin
@@ -14,6 +18,24 @@ rescue LoadError
14
18
  # ignore
15
19
  end
16
20
 
21
+ begin
22
+ require 'puppet_blacksmith/rake_tasks'
23
+ rescue LoadError
24
+ # ignore
25
+ end
26
+
27
+ begin
28
+ require 'github_changelog_generator/task'
29
+ rescue LoadError
30
+ # ignore
31
+ end
32
+
33
+ begin
34
+ require 'puppet-strings/tasks'
35
+ rescue LoadError
36
+ # ignore
37
+ end
38
+
17
39
  parallel_tests_loaded = false
18
40
  begin
19
41
  require 'parallel_tests'
@@ -22,121 +44,177 @@ rescue LoadError
22
44
  # ignore
23
45
  end
24
46
 
25
- task :default => [:help]
47
+ task default: [:help]
26
48
 
27
- pattern = 'spec/{aliases,classes,defines,unit,functions,hosts,integration,plans,type_aliases,types}/**/*_spec.rb'
49
+ pattern = 'spec/{aliases,classes,defines,functions,hosts,integration,plans,tasks,type_aliases,types,unit}/**/*_spec.rb'
28
50
 
29
51
  RSpec::Core::RakeTask.new(:spec_standalone) do |t, args|
30
- t.rspec_opts = ['--color']
52
+ t.rspec_opts = []
31
53
  t.rspec_opts << ENV['CI_SPEC_OPTIONS'] unless ENV['CI_SPEC_OPTIONS'].nil?
32
54
  if ENV['CI_NODE_TOTAL'] && ENV['CI_NODE_INDEX']
33
55
  ci_total = ENV['CI_NODE_TOTAL'].to_i
34
56
  ci_index = ENV['CI_NODE_INDEX'].to_i
35
57
  raise "CI_NODE_INDEX must be between 1-#{ci_total}" unless ci_index >= 1 && ci_index <= ci_total
58
+
36
59
  files = Rake::FileList[pattern].to_a
37
60
  per_node = (files.size / ci_total.to_f).ceil
38
61
  t.pattern = if args.extras.nil? || args.extras.empty?
39
62
  files.each_slice(per_node).to_a[ci_index - 1] || files.first
40
63
  else
41
- args.extras.join(",")
64
+ args.extras.join(',')
42
65
  end
43
66
  else
44
- if args.extras.nil? || args.extras.empty?
45
- t.pattern = pattern
46
- else
47
- t.pattern = args.extras.join(",")
48
- end
67
+ t.pattern = if args.extras.nil? || args.extras.empty?
68
+ pattern
69
+ else
70
+ args.extras.join(',')
71
+ end
49
72
  end
50
73
  end
51
74
 
52
- desc "List spec tests in a JSON document"
75
+ desc 'List spec tests in a JSON document'
53
76
  RSpec::Core::RakeTask.new(:spec_list_json) do |t|
54
77
  t.rspec_opts = ['--dry-run', '--format', 'json']
55
78
  t.pattern = pattern
56
79
  end
57
80
 
58
- desc "Run spec tests and clean the fixtures directory if successful"
59
- task :spec do |t, args|
81
+ desc 'Run spec tests and clean the fixtures directory if successful'
82
+ task :spec do |_t, args|
60
83
  begin
61
84
  Rake::Task[:spec_prep].invoke
62
85
  Rake::Task[:spec_standalone].invoke(*args.extras)
86
+ Rake::Task[:spec_clean].invoke
63
87
  ensure
88
+ Rake::Task[:spec_clean_symlinks].invoke
89
+ end
90
+ end
91
+
92
+ desc 'Run spec tests with ruby simplecov code coverage'
93
+ namespace :spec do
94
+ task :simplecov do
95
+ ENV['SIMPLECOV'] = 'yes'
96
+ Rake::Task['spec'].execute
97
+ end
98
+ end
99
+
100
+ desc 'Run spec tests in parallel and clean the fixtures directory if successful'
101
+ task :parallel_spec do |_t, args|
102
+ begin
103
+ Rake::Task[:spec_prep].invoke
104
+ Rake::Task[:parallel_spec_standalone].invoke(*args.extras)
64
105
  Rake::Task[:spec_clean].invoke
106
+ ensure
107
+ Rake::Task[:spec_clean_symlinks].invoke
65
108
  end
66
109
  end
67
110
 
68
- desc "Parallel spec tests"
69
- task :parallel_spec do
111
+ desc 'Parallel spec tests'
112
+ task :parallel_spec_standalone do |_t, args|
70
113
  raise 'Add the parallel_tests gem to Gemfile to enable this task' unless parallel_tests_loaded
114
+
71
115
  if Rake::FileList[pattern].to_a.empty?
72
- warn "No files for parallel_spec to run against"
116
+ warn 'No files for parallel_spec to run against'
73
117
  else
74
- begin
75
- args = ['-t', 'rspec']
76
- args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
77
- args.concat(Rake::FileList[pattern].to_a)
78
-
79
- Rake::Task[:spec_prep].invoke
80
- ParallelTests::CLI.new.run(args)
81
- ensure
82
- Rake::Task[:spec_clean].invoke
83
- end
118
+
119
+ args = ['-t', 'rspec']
120
+ args.push('--').concat(ENV['CI_SPEC_OPTIONS'].strip.split(' ')).push('--') unless ENV['CI_SPEC_OPTIONS'].nil? || ENV['CI_SPEC_OPTIONS'].strip.empty?
121
+ args.concat(Rake::FileList[pattern].to_a)
122
+
123
+ ParallelTests::CLI.new.run(args)
124
+
84
125
  end
85
126
  end
86
127
 
87
- desc "Build puppet module package"
128
+ desc 'Build puppet module package'
88
129
  task :build do
89
- # This will be deprecated once puppet-module is a face.
90
- begin
91
- Gem::Specification.find_by_name('puppet-module')
92
- rescue Gem::LoadError, NoMethodError
130
+ if Gem::Specification.find_by_name('puppet').version < Gem::Version.new('6.0.0')
131
+ Rake::Task['build:pmt'].invoke
132
+ else
133
+ Rake::Task['build:pdk'].invoke
134
+ end
135
+ end
136
+
137
+ namespace :build do
138
+ desc 'Build Puppet module package with PMT (Puppet < 6.0.0 only)'
139
+ task :pmt do
93
140
  require 'puppet/face'
141
+
94
142
  pmod = Puppet::Face['module', :current]
95
143
  pmod.build('./')
96
144
  end
145
+
146
+ desc 'Build Puppet module with PDK'
147
+ task :pdk do
148
+ begin
149
+ require 'pdk/util'
150
+ require 'pdk/module/build'
151
+
152
+ path = PDK::Module::Build.invoke(force: true, 'target-dir': File.join(Dir.pwd, 'pkg'))
153
+ puts "Module built: #{path}"
154
+ rescue LoadError
155
+ _ = `pdk --version`
156
+ unless $CHILD_STATUS.success?
157
+ warn 'Unable to build module. Please install PDK or add the `pdk` gem to your Gemfile.'
158
+ abort
159
+ end
160
+
161
+ system('pdk build --force')
162
+ end
163
+ end
97
164
  end
98
165
 
99
- desc "Clean a built module package"
166
+ desc 'Clean a built module package'
100
167
  task :clean do
101
- FileUtils.rm_rf("pkg/")
168
+ FileUtils.rm_rf('pkg/')
102
169
  end
103
170
 
104
171
  require 'puppet-lint/tasks/puppet-lint'
105
172
  # Must clear as it will not override the existing puppet-lint rake task since we require to import for
106
173
  # the PuppetLint::RakeTask
107
174
  Rake::Task[:lint].clear
108
- # Relative is not able to be set within the context of PuppetLint::RakeTask
175
+ # Utilize PuppetLint global configuration so that these settings can be tweaked by
176
+ # spec_helper.rb in an individual module
109
177
  PuppetLint.configuration.relative = true
110
- PuppetLint::RakeTask.new(:lint) do |config|
111
- config.fail_on_warnings = true
112
- config.disable_checks = [
113
- '80chars',
114
- '140chars',
115
- 'class_inherits_from_params_class',
116
- 'class_parameter_defaults',
117
- 'documentation',
118
- 'single_quote_string_with_variables']
119
- config.ignore_paths = [
120
- "bundle/**/*.pp",
121
- "pkg/**/*.pp",
122
- "spec/**/*.pp",
123
- "tests/**/*.pp",
124
- "types/**/*.pp",
125
- "vendor/**/*.pp",
126
- ]
178
+ PuppetLint.configuration.ignore_paths ||= []
179
+ PuppetLint.configuration.ignore_paths << '.vendor/**/*.pp'
180
+ PuppetLint.configuration.ignore_paths << 'bundle/**/*.pp'
181
+ PuppetLint.configuration.ignore_paths << 'pkg/**/*.pp'
182
+ PuppetLint.configuration.ignore_paths << 'spec/**/*.pp'
183
+ PuppetLint.configuration.ignore_paths << 'tests/**/*.pp'
184
+ PuppetLint.configuration.ignore_paths << 'types/**/*.pp'
185
+ PuppetLint.configuration.ignore_paths << 'vendor/**/*.pp'
186
+ puppet_lint_disable_checks = %w[
187
+ 80chars
188
+ 140chars
189
+ class_inherits_from_params_class
190
+ class_parameter_defaults
191
+ disable_autoloader_layout
192
+ documentation
193
+ single_quote_string_with_variables
194
+ ]
195
+ puppet_lint_disable_checks.each do |check|
196
+ PuppetLint.configuration.send("disable_#{check}")
197
+ end
198
+ PuppetLint::RakeTask.new(:lint)
199
+
200
+ desc 'Run puppet-lint and fix issues automatically'
201
+ PuppetLint::RakeTask.new(:lint_fix) do |config|
202
+ config.fix = true
127
203
  end
128
204
 
129
205
  require 'puppet-syntax/tasks/puppet-syntax'
130
206
  PuppetSyntax.exclude_paths ||= []
131
- PuppetSyntax.exclude_paths << "spec/fixtures/**/*"
132
- PuppetSyntax.exclude_paths << "pkg/**/*"
133
- PuppetSyntax.exclude_paths << "vendor/**/*"
207
+ PuppetSyntax.exclude_paths << 'spec/fixtures/**/*'
208
+ PuppetSyntax.exclude_paths << 'pkg/**/*'
209
+ PuppetSyntax.exclude_paths << 'vendor/**/*'
210
+ PuppetSyntax.exclude_paths << '.vendor/**/*'
211
+ PuppetSyntax.exclude_paths << 'plans/**/*'
134
212
  if Puppet.version.to_f < 4.0
135
- PuppetSyntax.exclude_paths << "types/**/*"
213
+ PuppetSyntax.exclude_paths << 'types/**/*'
136
214
  end
137
215
  PuppetSyntax.future_parser = true if ENV['FUTURE_PARSER'] == 'yes'
138
216
 
139
- desc "Check syntax of Ruby files and call :syntax and :metadata_lint"
217
+ desc 'Check syntax of Ruby files and call :syntax and :metadata_lint'
140
218
  task :validate do
141
219
  Dir['lib/**/*.rb'].each do |lib_file|
142
220
  sh "ruby -c #{lib_file}"
@@ -147,7 +225,7 @@ task :validate do
147
225
  if Rake::Task.task_defined?(:metadata_lint)
148
226
  Rake::Task[:metadata_lint].invoke
149
227
  else
150
- warn "Skipping metadata validation; the metadata-json-lint gem was not found"
228
+ warn 'Skipping metadata validation; the metadata-json-lint gem was not found'
151
229
  end
152
230
  end
153
231
  end
@@ -157,23 +235,23 @@ task :metadata do
157
235
  if Rake::Task.task_defined?(:metadata_lint)
158
236
  Rake::Task[:metadata_lint].invoke
159
237
  else
160
- warn "Skipping metadata validation; the metadata-json-lint gem was not found"
238
+ warn 'Skipping metadata validation; the metadata-json-lint gem was not found'
161
239
  end
162
240
  end
163
241
 
164
- desc "Print development version of module"
242
+ desc 'Print development version of module'
165
243
  task :compute_dev_version do
166
244
  version = ''
167
- if File.exists?( 'metadata.json' )
245
+ if File.exist?('metadata.json')
168
246
  require 'json'
169
247
 
170
- modinfo = JSON.parse(File.read( 'metadata.json' ))
248
+ modinfo = JSON.parse(File.read('metadata.json'))
171
249
  version = modinfo['version']
172
- elsif File.exists?( 'Modulefile' )
250
+ elsif File.exist?('Modulefile')
173
251
  modfile = File.read('Modulefile')
174
- version = modfile.match(/\nversion[ ]+['"](.*)['"]/)[1]
252
+ version = modfile.match(%r{\nversion +['"](.*)['"]})[1]
175
253
  else
176
- fail "Could not find a metadata.json or Modulefile! Cannot compute dev version without one or the other!"
254
+ raise 'Could not find a metadata.json or Modulefile! Cannot compute dev version without one or the other!'
177
255
  end
178
256
 
179
257
  sha = `git rev-parse HEAD`[0..7]
@@ -183,20 +261,20 @@ task :compute_dev_version do
183
261
  # If the branch is a release branch we append an 'r' into the new_version,
184
262
  # this is due to the release branch buildID conflicting with master branch when trying to push to the staging forge.
185
263
  # More info can be found at https://tickets.puppetlabs.com/browse/FM-6170
186
- if build = ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER']
187
- if branch.eql? "release"
188
- new_version = sprintf('%s-%s%04d-%s', version, "r", build, sha)
189
- else
190
- new_version = sprintf('%s-%04d-%s', version, build, sha)
191
- end
192
- else
193
- new_version = "#{version}-#{sha}"
194
- end
264
+ new_version = if build = (ENV['BUILD_NUMBER'] || ENV['TRAVIS_BUILD_NUMBER'])
265
+ if branch.eql? 'release'
266
+ '%s-%s%04d-%s' % [version, 'r', build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
267
+ else
268
+ '%s-%04d-%s' % [version, build, sha] # legacy support code # rubocop:disable Style/FormatStringToken
269
+ end
270
+ else
271
+ "#{version}-#{sha}"
272
+ end
195
273
 
196
274
  print new_version
197
275
  end
198
276
 
199
- desc "Runs all necessary checks on a module in preparation for a release"
277
+ desc 'Runs all necessary checks on a module in preparation for a release'
200
278
  task :release_checks do
201
279
  Rake::Task[:lint].invoke
202
280
  Rake::Task[:validate].invoke
@@ -205,56 +283,53 @@ task :release_checks do
205
283
  else
206
284
  Rake::Task[:spec].invoke
207
285
  end
208
- Rake::Task["check:symlinks"].invoke
209
- Rake::Task["check:test_file"].invoke
210
- Rake::Task["check:dot_underscore"].invoke
211
- Rake::Task["check:git_ignore"].invoke
286
+ Rake::Task[:check].invoke
212
287
  end
213
288
 
214
289
  namespace :check do
215
- desc "Fails if symlinks are present in directory"
290
+ desc 'Fails if symlinks are present in directory'
216
291
  task :symlinks do
217
- symlinks = check_directory_for_symlinks
292
+ symlinks = PuppetlabsSpecHelper::Tasks::CheckSymlinks.new.check
218
293
  unless symlinks.empty?
219
- symlinks.each { |r| puts "Symlink found: #{r.to_s} => #{r.readlink}" }
220
- fail "Symlink(s) exist within this directory"
294
+ symlinks.each { |r| puts "Symlink found: #{r} => #{r.readlink}" }
295
+ raise 'Symlink(s) exist within this directory'
221
296
  end
222
297
  end
223
298
 
224
- desc "Fails if .pp files present in tests folder"
299
+ desc 'Fails if .pp files present in tests folder'
225
300
  task :test_file do
226
- if Dir.exist?("tests")
227
- Dir.chdir("tests")
228
- ppfiles = Dir["*.pp"]
229
- unless ppfiles.empty?
230
- puts ppfiles
231
- fail ".pp files present in tests folder; Move them to an examples folder following the new convention"
232
- end
301
+ ppfiles = Dir[File.join('tests', '**', '*.pp')]
302
+ unless ppfiles.empty?
303
+ puts ppfiles
304
+ raise '.pp files present in tests folder; Move them to an examples folder following the new convention'
233
305
  end
234
306
  end
235
307
 
236
- desc "Fails if any ._ files are present in directory"
308
+ desc 'Fails if any ._ files are present in directory'
237
309
  task :dot_underscore do
238
- dirs = Dir["._*"]
310
+ dirs = Dir['._*']
239
311
  unless dirs.empty?
240
312
  puts dirs
241
- fail "._ files are present in the directory"
313
+ raise '._ files are present in the directory'
242
314
  end
243
315
  end
244
316
 
245
- desc "Fails if directories contain the files specified in .gitignore"
317
+ desc 'Fails if directories contain the files specified in .gitignore'
246
318
  task :git_ignore do
247
319
  matched = `git ls-files --ignored --exclude-standard`
248
- unless matched == ""
320
+ unless matched == ''
249
321
  puts matched
250
- fail "File specified in .gitignore has been committed"
322
+ raise 'File specified in .gitignore has been committed'
251
323
  end
252
324
  end
253
325
  end
254
326
 
255
- desc "Display the list of available rake tasks"
327
+ desc 'Run static pre release checks'
328
+ task check: ['check:symlinks', 'check:test_file', 'check:dot_underscore', 'check:git_ignore']
329
+
330
+ desc 'Display the list of available rake tasks'
256
331
  task :help do
257
- system("rake -T")
332
+ system('rake -T')
258
333
  end
259
334
 
260
335
  begin
@@ -264,31 +339,89 @@ begin
264
339
  task.options = ['-D', '-S', '-E']
265
340
  end
266
341
  rescue LoadError
267
- desc "rubocop is not available in this installation"
342
+ desc 'rubocop is not available in this installation'
268
343
  task :rubocop do
269
- fail "rubocop is not available in this installation"
344
+ raise 'rubocop is not available in this installation'
270
345
  end
271
346
  end
272
347
 
273
- module_dir = Dir.pwd
274
- locales_dir = File.absolute_path('locales', module_dir )
275
- # if the task is allowed to run when the module does not have a locales directory,
276
- # the task is run in the puppet gem instead and creates a POT there.
277
- puts "gettext-setup tasks will only be loaded if the locales/ directory is present" if Rake.verbose == true
278
- if File.exist? locales_dir
279
- begin
280
- spec = Gem::Specification.find_by_name 'gettext-setup'
281
- load "#{spec.gem_dir}/lib/tasks/gettext.rake"
282
- # Initialization requires a valid locales directory
283
- GettextSetup.initialize_config(locales_dir)
284
- namespace :module do
285
- desc "Runs all tasks to build a modules POT file for internationalization"
286
- task :pot_gen do
287
- Rake::Task["gettext:pot"].invoke()
288
- Rake::Task["gettext:metadata_pot"].invoke("#{module_dir}/metadata.json")
348
+ def create_gch_task(changelog_user = nil, changelog_project = nil, changelog_since_tag = nil, changelog_tag_pattern = 'v%s')
349
+ if Bundler.rubygems.find_name('github_changelog_generator').any?
350
+ # needed a place to hide these methods
351
+ # rubocop:disable Lint/NestedMethodDefinition
352
+ def changelog_user_from_metadata
353
+ result = JSON.parse(File.read('metadata.json'))['author']
354
+ raise 'unable to find the changelog_user in .sync.yml, or the author in metadata.json' if result.nil?
355
+
356
+ puts "GitHubChangelogGenerator user:#{result}"
357
+ result
358
+ end
359
+
360
+ def changelog_project_from_metadata
361
+ result = JSON.parse(File.read('metadata.json'))['name']
362
+ raise 'unable to find the changelog_project in .sync.yml or the name in metadata.json' if result.nil?
363
+
364
+ puts "GitHubChangelogGenerator project:#{result}"
365
+ result
366
+ end
367
+
368
+ def changelog_future_release
369
+ return unless Rake.application.top_level_tasks.include? 'changelog'
370
+
371
+ result = JSON.parse(File.read('metadata.json'))['version']
372
+ raise 'unable to find the future_release (version) in metadata.json' if result.nil?
373
+
374
+ puts "GitHubChangelogGenerator future_release:#{result}"
375
+ result
376
+ end
377
+ # rubocop:enable Lint/NestedMethodDefinition
378
+
379
+ GitHubChangelogGenerator::RakeTask.new :changelog do |config|
380
+ if ENV['CHANGELOG_GITHUB_TOKEN'].nil?
381
+ raise "Set CHANGELOG_GITHUB_TOKEN environment variable eg 'export CHANGELOG_GITHUB_TOKEN=valid_token_here'"
289
382
  end
383
+
384
+ config.user = changelog_user || changelog_user_from_metadata
385
+ config.project = changelog_project || changelog_project_from_metadata
386
+ config.since_tag = changelog_since_tag if changelog_since_tag
387
+ config.future_release = changelog_tag_pattern % changelog_future_release.to_s
388
+ config.exclude_labels = ['maintenance']
389
+ config.header = "# Change log\n\nAll notable changes to this project will be documented in this file. " \
390
+ 'The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres ' \
391
+ 'to [Semantic Versioning](https://semver.org).'
392
+ config.add_pr_wo_labels = true
393
+ config.issues = false
394
+ config.merge_prefix = '### UNCATEGORIZED PRS; GO LABEL THEM'
395
+ config.configure_sections = {
396
+ 'Changed' => {
397
+ 'prefix' => '### Changed',
398
+ 'labels' => ['backwards-incompatible'],
399
+ },
400
+ 'Added' => {
401
+ 'prefix' => '### Added',
402
+ 'labels' => %w[feature enhancement],
403
+ },
404
+ 'Fixed' => {
405
+ 'prefix' => '### Fixed',
406
+ 'labels' => ['bugfix'],
407
+ },
408
+ }
409
+ end
410
+ else
411
+ desc 'Generate a Changelog from GitHub'
412
+ task :changelog do
413
+ raise <<~MESSAGE
414
+ The changelog tasks depends on unreleased features of the github_changelog_generator gem.
415
+ Please manually add it to your .sync.yml for now, and run `pdk update`:
416
+ ---
417
+ Gemfile:
418
+ optional:
419
+ ':development':
420
+ - gem: 'github_changelog_generator'
421
+ git: 'https://github.com/skywinder/github-changelog-generator'
422
+ ref: '20ee04ba1234e9e83eb2ffb5056e23d641c7a018'
423
+ condition: "Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.2.2')"
424
+ MESSAGE
290
425
  end
291
- rescue Gem::LoadError
292
- puts "No gettext-setup gem found, skipping GettextSetup config initialization" if Rake.verbose == true
293
426
  end
294
427
  end