pdk 1.15.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +168 -10
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +28 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/bolt.rb +19 -0
  7. data/lib/pdk/cli.rb +7 -6
  8. data/lib/pdk/cli/bundle.rb +5 -1
  9. data/lib/pdk/cli/config.rb +3 -1
  10. data/lib/pdk/cli/config/get.rb +3 -1
  11. data/lib/pdk/cli/convert.rb +7 -9
  12. data/lib/pdk/cli/exec/command.rb +45 -4
  13. data/lib/pdk/cli/exec_group.rb +78 -43
  14. data/lib/pdk/cli/get.rb +20 -0
  15. data/lib/pdk/cli/get/config.rb +24 -0
  16. data/lib/pdk/cli/new.rb +2 -0
  17. data/lib/pdk/cli/new/class.rb +2 -2
  18. data/lib/pdk/cli/new/defined_type.rb +2 -2
  19. data/lib/pdk/cli/new/fact.rb +29 -0
  20. data/lib/pdk/cli/new/function.rb +29 -0
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release/publish.rb +11 -1
  26. data/lib/pdk/cli/remove.rb +20 -0
  27. data/lib/pdk/cli/remove/config.rb +80 -0
  28. data/lib/pdk/cli/set.rb +20 -0
  29. data/lib/pdk/cli/set/config.rb +119 -0
  30. data/lib/pdk/cli/update.rb +6 -8
  31. data/lib/pdk/cli/util.rb +7 -3
  32. data/lib/pdk/cli/util/option_validator.rb +6 -0
  33. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  34. data/lib/pdk/cli/validate.rb +26 -44
  35. data/lib/pdk/config.rb +264 -7
  36. data/lib/pdk/config/ini_file.rb +183 -0
  37. data/lib/pdk/config/ini_file_setting.rb +39 -0
  38. data/lib/pdk/config/namespace.rb +25 -5
  39. data/lib/pdk/config/setting.rb +3 -2
  40. data/lib/pdk/context.rb +99 -0
  41. data/lib/pdk/context/control_repo.rb +60 -0
  42. data/lib/pdk/context/module.rb +28 -0
  43. data/lib/pdk/context/none.rb +22 -0
  44. data/lib/pdk/control_repo.rb +90 -0
  45. data/lib/pdk/generate/defined_type.rb +25 -32
  46. data/lib/pdk/generate/fact.rb +25 -0
  47. data/lib/pdk/generate/function.rb +48 -0
  48. data/lib/pdk/generate/module.rb +33 -20
  49. data/lib/pdk/generate/provider.rb +15 -64
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -188
  52. data/lib/pdk/generate/task.rb +28 -46
  53. data/lib/pdk/generate/transport.rb +20 -74
  54. data/lib/pdk/module.rb +1 -1
  55. data/lib/pdk/module/build.rb +23 -0
  56. data/lib/pdk/module/convert.rb +58 -34
  57. data/lib/pdk/module/metadata.rb +6 -2
  58. data/lib/pdk/module/release.rb +3 -9
  59. data/lib/pdk/module/update.rb +8 -12
  60. data/lib/pdk/module/update_manager.rb +7 -0
  61. data/lib/pdk/report.rb +3 -3
  62. data/lib/pdk/report/event.rb +8 -2
  63. data/lib/pdk/template.rb +59 -0
  64. data/lib/pdk/template/fetcher.rb +98 -0
  65. data/lib/pdk/template/fetcher/git.rb +85 -0
  66. data/lib/pdk/template/fetcher/local.rb +28 -0
  67. data/lib/pdk/template/renderer.rb +96 -0
  68. data/lib/pdk/template/renderer/v1.rb +25 -0
  69. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  70. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  71. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  72. data/lib/pdk/template/template_dir.rb +67 -0
  73. data/lib/pdk/tests/unit.rb +8 -1
  74. data/lib/pdk/util.rb +38 -39
  75. data/lib/pdk/util/bundler.rb +2 -1
  76. data/lib/pdk/util/changelog_generator.rb +11 -2
  77. data/lib/pdk/util/env.rb +28 -11
  78. data/lib/pdk/util/json_finder.rb +84 -0
  79. data/lib/pdk/util/puppet_strings.rb +3 -3
  80. data/lib/pdk/util/puppet_version.rb +2 -2
  81. data/lib/pdk/util/ruby_version.rb +11 -4
  82. data/lib/pdk/util/template_uri.rb +17 -14
  83. data/lib/pdk/util/vendored_file.rb +1 -2
  84. data/lib/pdk/validate.rb +79 -25
  85. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  86. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  87. data/lib/pdk/validate/external_command_validator.rb +208 -0
  88. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  89. data/lib/pdk/validate/invokable_validator.rb +220 -0
  90. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  91. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  92. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  93. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  94. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  95. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  96. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  97. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  98. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  99. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  100. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  101. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  102. data/lib/pdk/validate/validator.rb +118 -0
  103. data/lib/pdk/validate/validator_group.rb +104 -0
  104. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  105. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  106. data/lib/pdk/version.rb +1 -1
  107. data/locales/pdk.pot +513 -329
  108. metadata +76 -32
  109. data/lib/pdk/module/template_dir.rb +0 -115
  110. data/lib/pdk/module/template_dir/base.rb +0 -268
  111. data/lib/pdk/module/template_dir/git.rb +0 -91
  112. data/lib/pdk/module/template_dir/local.rb +0 -21
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.15.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-13 00:00:00.000000000 Z
11
+ date: 2021-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,16 +76,22 @@ dependencies:
76
76
  name: ffi
77
77
  requirement: !ruby/object:Gem::Requirement
78
78
  requirements:
79
- - - "~>"
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: 1.9.25
82
+ - - "<"
80
83
  - !ruby/object:Gem::Version
81
- version: 1.9.0
84
+ version: 2.0.0
82
85
  type: :runtime
83
86
  prerelease: false
84
87
  version_requirements: !ruby/object:Gem::Requirement
85
88
  requirements:
86
- - - "~>"
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: 1.9.25
92
+ - - "<"
87
93
  - !ruby/object:Gem::Version
88
- version: 1.9.0
94
+ version: 2.0.0
89
95
  - !ruby/object:Gem::Dependency
90
96
  name: gettext-setup
91
97
  requirement: !ruby/object:Gem::Requirement
@@ -176,14 +182,14 @@ dependencies:
176
182
  requirements:
177
183
  - - "~>"
178
184
  - !ruby/object:Gem::Version
179
- version: '0.13'
185
+ version: '0.22'
180
186
  type: :runtime
181
187
  prerelease: false
182
188
  version_requirements: !ruby/object:Gem::Requirement
183
189
  requirements:
184
190
  - - "~>"
185
191
  - !ruby/object:Gem::Version
186
- version: '0.13'
192
+ version: '0.22'
187
193
  - !ruby/object:Gem::Dependency
188
194
  name: tty-spinner
189
195
  requirement: !ruby/object:Gem::Requirement
@@ -230,16 +236,22 @@ dependencies:
230
236
  name: facter
231
237
  requirement: !ruby/object:Gem::Requirement
232
238
  requirements:
233
- - - "~>"
239
+ - - ">="
234
240
  - !ruby/object:Gem::Version
235
241
  version: 2.5.1
242
+ - - "<"
243
+ - !ruby/object:Gem::Version
244
+ version: 5.0.0
236
245
  type: :runtime
237
246
  prerelease: false
238
247
  version_requirements: !ruby/object:Gem::Requirement
239
248
  requirements:
240
- - - "~>"
249
+ - - ">="
241
250
  - !ruby/object:Gem::Version
242
251
  version: 2.5.1
252
+ - - "<"
253
+ - !ruby/object:Gem::Version
254
+ version: 5.0.0
243
255
  - !ruby/object:Gem::Dependency
244
256
  name: httpclient
245
257
  requirement: !ruby/object:Gem::Requirement
@@ -286,6 +298,7 @@ files:
286
298
  - lib/pdk/analytics/client/noop.rb
287
299
  - lib/pdk/analytics/util.rb
288
300
  - lib/pdk/answer_file.rb
301
+ - lib/pdk/bolt.rb
289
302
  - lib/pdk/cli.rb
290
303
  - lib/pdk/cli/build.rb
291
304
  - lib/pdk/cli/bundle.rb
@@ -298,12 +311,16 @@ files:
298
311
  - lib/pdk/cli/exec/command.rb
299
312
  - lib/pdk/cli/exec/interactive_command.rb
300
313
  - lib/pdk/cli/exec_group.rb
314
+ - lib/pdk/cli/get.rb
315
+ - lib/pdk/cli/get/config.rb
301
316
  - lib/pdk/cli/module.rb
302
317
  - lib/pdk/cli/module/build.rb
303
318
  - lib/pdk/cli/module/generate.rb
304
319
  - lib/pdk/cli/new.rb
305
320
  - lib/pdk/cli/new/class.rb
306
321
  - lib/pdk/cli/new/defined_type.rb
322
+ - lib/pdk/cli/new/fact.rb
323
+ - lib/pdk/cli/new/function.rb
307
324
  - lib/pdk/cli/new/module.rb
308
325
  - lib/pdk/cli/new/provider.rb
309
326
  - lib/pdk/cli/new/task.rb
@@ -312,6 +329,10 @@ files:
312
329
  - lib/pdk/cli/release.rb
313
330
  - lib/pdk/cli/release/prep.rb
314
331
  - lib/pdk/cli/release/publish.rb
332
+ - lib/pdk/cli/remove.rb
333
+ - lib/pdk/cli/remove/config.rb
334
+ - lib/pdk/cli/set.rb
335
+ - lib/pdk/cli/set/config.rb
315
336
  - lib/pdk/cli/test.rb
316
337
  - lib/pdk/cli/test/unit.rb
317
338
  - lib/pdk/cli/update.rb
@@ -321,10 +342,13 @@ files:
321
342
  - lib/pdk/cli/util/option_normalizer.rb
322
343
  - lib/pdk/cli/util/option_validator.rb
323
344
  - lib/pdk/cli/util/spinner.rb
345
+ - lib/pdk/cli/util/update_manager_printer.rb
324
346
  - lib/pdk/cli/validate.rb
325
347
  - lib/pdk/config.rb
326
348
  - lib/pdk/config/analytics_schema.json
327
349
  - lib/pdk/config/errors.rb
350
+ - lib/pdk/config/ini_file.rb
351
+ - lib/pdk/config/ini_file_setting.rb
328
352
  - lib/pdk/config/json.rb
329
353
  - lib/pdk/config/json_schema_namespace.rb
330
354
  - lib/pdk/config/json_schema_setting.rb
@@ -334,8 +358,15 @@ files:
334
358
  - lib/pdk/config/validator.rb
335
359
  - lib/pdk/config/yaml.rb
336
360
  - lib/pdk/config/yaml_with_schema.rb
361
+ - lib/pdk/context.rb
362
+ - lib/pdk/context/control_repo.rb
363
+ - lib/pdk/context/module.rb
364
+ - lib/pdk/context/none.rb
365
+ - lib/pdk/control_repo.rb
337
366
  - lib/pdk/generate.rb
338
367
  - lib/pdk/generate/defined_type.rb
368
+ - lib/pdk/generate/fact.rb
369
+ - lib/pdk/generate/function.rb
339
370
  - lib/pdk/generate/module.rb
340
371
  - lib/pdk/generate/provider.rb
341
372
  - lib/pdk/generate/puppet_class.rb
@@ -349,15 +380,20 @@ files:
349
380
  - lib/pdk/module/convert.rb
350
381
  - lib/pdk/module/metadata.rb
351
382
  - lib/pdk/module/release.rb
352
- - lib/pdk/module/template_dir.rb
353
- - lib/pdk/module/template_dir/base.rb
354
- - lib/pdk/module/template_dir/git.rb
355
- - lib/pdk/module/template_dir/local.rb
356
383
  - lib/pdk/module/update.rb
357
384
  - lib/pdk/module/update_manager.rb
358
385
  - lib/pdk/report.rb
359
386
  - lib/pdk/report/event.rb
360
- - lib/pdk/template_file.rb
387
+ - lib/pdk/template.rb
388
+ - lib/pdk/template/fetcher.rb
389
+ - lib/pdk/template/fetcher/git.rb
390
+ - lib/pdk/template/fetcher/local.rb
391
+ - lib/pdk/template/renderer.rb
392
+ - lib/pdk/template/renderer/v1.rb
393
+ - lib/pdk/template/renderer/v1/legacy_template_dir.rb
394
+ - lib/pdk/template/renderer/v1/renderer.rb
395
+ - lib/pdk/template/renderer/v1/template_file.rb
396
+ - lib/pdk/template/template_dir.rb
361
397
  - lib/pdk/tests/unit.rb
362
398
  - lib/pdk/util.rb
363
399
  - lib/pdk/util/bundler.rb
@@ -365,6 +401,7 @@ files:
365
401
  - lib/pdk/util/env.rb
366
402
  - lib/pdk/util/filesystem.rb
367
403
  - lib/pdk/util/git.rb
404
+ - lib/pdk/util/json_finder.rb
368
405
  - lib/pdk/util/puppet_strings.rb
369
406
  - lib/pdk/util/puppet_version.rb
370
407
  - lib/pdk/util/ruby_version.rb
@@ -377,21 +414,27 @@ files:
377
414
  - lib/pdk/util/windows/process.rb
378
415
  - lib/pdk/util/windows/string.rb
379
416
  - lib/pdk/validate.rb
380
- - lib/pdk/validate/base_validator.rb
381
- - lib/pdk/validate/metadata/metadata_json_lint.rb
382
- - lib/pdk/validate/metadata/metadata_syntax.rb
383
- - lib/pdk/validate/metadata_validator.rb
384
- - lib/pdk/validate/puppet/puppet_epp.rb
385
- - lib/pdk/validate/puppet/puppet_lint.rb
386
- - lib/pdk/validate/puppet/puppet_syntax.rb
387
- - lib/pdk/validate/puppet_validator.rb
388
- - lib/pdk/validate/ruby/rubocop.rb
389
- - lib/pdk/validate/ruby_validator.rb
390
- - lib/pdk/validate/tasks/metadata_lint.rb
391
- - lib/pdk/validate/tasks/name.rb
392
- - lib/pdk/validate/tasks_validator.rb
393
- - lib/pdk/validate/yaml/syntax.rb
394
- - lib/pdk/validate/yaml_validator.rb
417
+ - lib/pdk/validate/control_repo/control_repo_validator_group.rb
418
+ - lib/pdk/validate/control_repo/environment_conf_validator.rb
419
+ - lib/pdk/validate/external_command_validator.rb
420
+ - lib/pdk/validate/internal_ruby_validator.rb
421
+ - lib/pdk/validate/invokable_validator.rb
422
+ - lib/pdk/validate/metadata/metadata_json_lint_validator.rb
423
+ - lib/pdk/validate/metadata/metadata_syntax_validator.rb
424
+ - lib/pdk/validate/metadata/metadata_validator_group.rb
425
+ - lib/pdk/validate/puppet/puppet_epp_validator.rb
426
+ - lib/pdk/validate/puppet/puppet_lint_validator.rb
427
+ - lib/pdk/validate/puppet/puppet_syntax_validator.rb
428
+ - lib/pdk/validate/puppet/puppet_validator_group.rb
429
+ - lib/pdk/validate/ruby/ruby_rubocop_validator.rb
430
+ - lib/pdk/validate/ruby/ruby_validator_group.rb
431
+ - lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb
432
+ - lib/pdk/validate/tasks/tasks_name_validator.rb
433
+ - lib/pdk/validate/tasks/tasks_validator_group.rb
434
+ - lib/pdk/validate/validator.rb
435
+ - lib/pdk/validate/validator_group.rb
436
+ - lib/pdk/validate/yaml/yaml_syntax_validator.rb
437
+ - lib/pdk/validate/yaml/yaml_validator_group.rb
395
438
  - lib/pdk/version.rb
396
439
  - locales/config.yaml
397
440
  - locales/pdk.pot
@@ -406,14 +449,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
406
449
  requirements:
407
450
  - - ">="
408
451
  - !ruby/object:Gem::Version
409
- version: 2.1.9
452
+ version: 2.4.0
410
453
  required_rubygems_version: !ruby/object:Gem::Requirement
411
454
  requirements:
412
455
  - - ">="
413
456
  - !ruby/object:Gem::Version
414
457
  version: '0'
415
458
  requirements: []
416
- rubygems_version: 3.0.6
459
+ rubyforge_project:
460
+ rubygems_version: 2.7.6.2
417
461
  signing_key:
418
462
  specification_version: 4
419
463
  summary: A key part of the Puppet Development Kit, the shortest path to better modules
@@ -1,115 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Module
5
- module TemplateDir
6
- # Creates a TemplateDir object with the path or URL to the template
7
- # and the block of code to run to be run while the template is available.
8
- #
9
- # The template directory is only guaranteed to be available on disk
10
- # within the scope of the block passed to this method.
11
- #
12
- # @param uri [PDK::Util::TemplateURI] The path to a directory to use as the
13
- # template or a URI to a git repository.
14
- # @param module_metadata [Hash] A Hash containing the module metadata.
15
- # Defaults to an empty Hash.
16
- # @yieldparam self [PDK::Module::TemplateDir] The initialised object with
17
- # the template available on disk.
18
- #
19
- # @example Using a git repository as a template
20
- # PDK::Module::TemplateDir.with('https://github.com/puppetlabs/pdk-templates') do |t|
21
- # t.render do |filename, content|
22
- # File.open(filename, 'w') do |file|
23
- # file.write(content)
24
- # end
25
- # end
26
- # end
27
- #
28
- # @raise [ArgumentError] If no block is given to this method.
29
- # @raise [PDK::CLI::FatalError] (see #clone_repo)
30
- # @raise [ArgumentError] (see #validate_module_template!)
31
- #
32
- # @api public
33
- def self.with(uri, module_metadata = {}, init = false)
34
- unless block_given?
35
- raise ArgumentError, _('%{class_name}.with must be passed a block.') % { class_name: name }
36
- end
37
- unless uri.is_a? PDK::Util::TemplateURI
38
- raise ArgumentError, _('%{class_name}.with must be passed a PDK::Util::TemplateURI, got a %{uri_type}') % { uri_type: uri.class, class_name: name }
39
- end
40
-
41
- if PDK::Util::Git.repo?(uri.bare_uri)
42
- require 'pdk/module/template_dir/git'
43
- PDK::Module::TemplateDir::Git.new(uri, module_metadata, init) { |value| yield value }
44
- else
45
- require 'pdk/module/template_dir/local'
46
- PDK::Module::TemplateDir::Local.new(uri, module_metadata, init) { |value| yield value }
47
- end
48
- end
49
-
50
- def self.moduleroot_dir(template_root_dir)
51
- File.join(template_root_dir, 'moduleroot')
52
- end
53
-
54
- def self.moduleroot_init(template_root_dir)
55
- File.join(template_root_dir, 'moduleroot_init')
56
- end
57
-
58
- # Validate the content of the template directory.
59
- #
60
- # @raise [ArgumentError] If the specified path is not a directory.
61
- # @raise [ArgumentError] If the template directory does not contain
62
- # a directory called 'moduleroot'.
63
- #
64
- # @return [void]
65
- #
66
- # @api public
67
- def self.validate_module_template!(template_root_dir)
68
- # rubocop:disable Style/GuardClause
69
- unless PDK::Util::Filesystem.directory?(template_root_dir)
70
- require 'pdk/util'
71
-
72
- if PDK::Util.package_install? && PDK::Util::Filesystem.fnmatch?(File.join(PDK::Util.package_cachedir, '*'), template_root_dir)
73
- raise ArgumentError, _('The built-in template has substantially changed. Please run "pdk convert" on your module to continue.')
74
- else
75
- raise ArgumentError, _("The specified template '%{path}' is not a directory.") % { path: template_root_dir }
76
- end
77
- end
78
-
79
- unless PDK::Util::Filesystem.directory?(moduleroot_dir(template_root_dir))
80
- raise ArgumentError, _("The template at '%{path}' does not contain a 'moduleroot/' directory.") % { path: template_root_dir }
81
- end
82
-
83
- unless PDK::Util::Filesystem.directory?(moduleroot_init(template_root_dir))
84
- # rubocop:disable Metrics/LineLength
85
- raise ArgumentError, _("The template at '%{path}' does not contain a 'moduleroot_init/' directory, which indicates you are using an older style of template. Before continuing please use the --template-url flag when running the pdk new commands to pass a new style template.") % { path: template_root_dir }
86
- # rubocop:enable Metrics/LineLength
87
- end
88
- # rubocop:enable Style/GuardClause
89
- end
90
-
91
- # Get a list of template files in the template directory.
92
- #
93
- # @return [Hash{String=>String}] A hash of key file names and
94
- # value locations.
95
- #
96
- # @api public
97
- def self.files_in_template(dirs)
98
- temp_paths = []
99
- dirlocs = []
100
- dirs.each do |dir|
101
- raise ArgumentError, _("The directory '%{dir}' doesn't exist") % { dir: dir } unless PDK::Util::Filesystem.directory?(dir)
102
- temp_paths += PDK::Util::Filesystem.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).select do |template_path|
103
- if PDK::Util::Filesystem.file?(template_path) && !PDK::Util::Filesystem.symlink?(template_path)
104
- dirlocs << dir
105
- end
106
- end
107
- temp_paths.map do |template_path|
108
- template_path.sub!(%r{\A#{Regexp.escape(dir)}#{Regexp.escape(File::SEPARATOR)}}, '')
109
- end
110
- end
111
- Hash[temp_paths.zip dirlocs]
112
- end
113
- end
114
- end
115
- end
@@ -1,268 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Module
5
- module TemplateDir
6
- class Base
7
- attr_accessor :module_metadata
8
- attr_reader :uri
9
-
10
- # Initialises the TemplateDir object with the path or URL to the template
11
- # and the block of code to run to be run while the template is available.
12
- #
13
- # The template directory is only guaranteed to be available on disk
14
- # within the scope of the block passed to this method.
15
- #
16
- # @param uri [PDK::Util::TemplateURI] The path to a directory to use as the
17
- # template or a URI to a git repository.
18
- # @param module_metadata [Hash] A Hash containing the module metadata.
19
- # Defaults to an empty Hash.
20
- # @yieldparam self [PDK::Module::TemplateDir] The initialised object with
21
- # the template available on disk.
22
- #
23
- # @example Using a git repository as a template
24
- # PDK::Module::TemplateDir::Base.new('https://github.com/puppetlabs/pdk-templates') do |t|
25
- # t.render do |filename, content|
26
- # File.open(filename, 'w') do |file|
27
- # file.write(content)
28
- # end
29
- # end
30
- # end
31
- #
32
- # @raise [ArgumentError] If no block is given to this method.
33
- # @raise [PDK::CLI::FatalError] (see #clone_repo)
34
- # @raise [ArgumentError] (see #validate_module_template!)
35
- #
36
- # @api public
37
- def initialize(uri, module_metadata = {}, init = false)
38
- unless block_given?
39
- raise ArgumentError, _('%{class_name} must be initialized with a block.') % { class_name: self.class.name }
40
- end
41
- unless uri.is_a? PDK::Util::TemplateURI
42
- raise ArgumentError, _('%{class_name} must be initialized with a PDK::Util::TemplateURI, got a %{uri_type}') % { uri_type: uri.class, class_name: self.class.name }
43
- end
44
-
45
- @path, @is_temporary_path = template_path(uri)
46
- @uri = uri
47
-
48
- @init = init
49
- @moduleroot_dir = PDK::Module::TemplateDir.moduleroot_dir(@path)
50
- @moduleroot_init = PDK::Module::TemplateDir.moduleroot_init(@path)
51
- @dirs = [@moduleroot_dir]
52
- @dirs << @moduleroot_init if @init
53
- @object_dir = File.join(@path, 'object_templates')
54
-
55
- PDK::Module::TemplateDir.validate_module_template!(@path)
56
-
57
- @module_metadata = module_metadata
58
-
59
- template_type = uri.default? ? 'default' : 'custom'
60
- PDK.analytics.event('TemplateDir', 'initialize', label: template_type)
61
-
62
- yield self
63
- ensure
64
- # If the the path is temporary, clean it up
65
- if @is_temporary_path
66
- PDK::Util::Filesystem.rm_rf(@path)
67
- end
68
- end
69
-
70
- # Retrieve identifying metadata for the template.
71
- #
72
- # For git repositories, this will return the URL to the repository and
73
- # a reference to the HEAD.
74
- #
75
- # For plain fileystem directories, this will return the URL to the repository only.
76
- #
77
- # @return [Hash{String => String}] A hash of identifying metadata.
78
- #
79
- # @api public
80
- # @abstract
81
- def metadata
82
- {
83
- 'pdk-version' => PDK::Util::Version.version_string,
84
- 'template-url' => nil,
85
- 'template-ref' => nil,
86
- }
87
- end
88
-
89
- # Loop through the files in the template, yielding each rendered file to
90
- # the supplied block.
91
- #
92
- # @yieldparam dest_path [String] The path of the destination file,
93
- # relative to the root of the module.
94
- # @yieldparam dest_content [String] The rendered content of the
95
- # destination file.
96
- #
97
- # @raise [PDK::CLI::FatalError] If the template fails to render.
98
- #
99
- # @return [void]
100
- #
101
- # @api public
102
- def render
103
- require 'pdk/template_file'
104
-
105
- PDK::Module::TemplateDir.files_in_template(@dirs).each do |template_file, template_loc|
106
- template_file = template_file.to_s
107
- PDK.logger.debug(_("Rendering '%{template}'...") % { template: template_file })
108
- dest_path = template_file.sub(%r{\.erb\Z}, '')
109
- config = config_for(dest_path)
110
-
111
- dest_status = if template_loc.start_with?(@moduleroot_init)
112
- :init
113
- else
114
- :manage
115
- end
116
-
117
- if config['unmanaged']
118
- dest_status = :unmanage
119
- elsif config['delete']
120
- dest_status = :delete
121
- else
122
- begin
123
- dest_content = PDK::TemplateFile.new(File.join(template_loc, template_file), configs: config, template_dir: self).render
124
- rescue => error
125
- error_msg = _(
126
- "Failed to render template '%{template}'\n" \
127
- '%{exception}: %{message}',
128
- ) % { template: template_file, exception: error.class, message: error.message }
129
- raise PDK::CLI::FatalError, error_msg
130
- end
131
- end
132
-
133
- yield dest_path, dest_content, dest_status
134
- end
135
- end
136
-
137
- # Searches the template directory for template files that can be used to
138
- # render files for the specified object type.
139
- #
140
- # @param object_type [Symbol] The object type, e.g. (`:class`,
141
- # `:defined_type`, `:fact`, etc).
142
- #
143
- # @return [Hash{Symbol => String}] if the templates are available in the
144
- # template dir, otherwise `nil`. The returned hash can contain two keys,
145
- # :object contains the path on disk to the template for the object, :spec
146
- # contains the path on disk to the template for the object's spec file
147
- # (if available).
148
- #
149
- # @api public
150
- def object_template_for(object_type)
151
- object_path = File.join(@object_dir, "#{object_type}.erb")
152
- type_path = File.join(@object_dir, "#{object_type}_type.erb")
153
- device_path = File.join(@object_dir, "#{object_type}_device.erb")
154
- spec_path = File.join(@object_dir, "#{object_type}_spec.erb")
155
- type_spec_path = File.join(@object_dir, "#{object_type}_type_spec.erb")
156
-
157
- if PDK::Util::Filesystem.file?(object_path) && PDK::Util::Filesystem.readable?(object_path)
158
- result = { object: object_path }
159
- result[:type] = type_path if PDK::Util::Filesystem.file?(type_path) && PDK::Util::Filesystem.readable?(type_path)
160
- result[:spec] = spec_path if PDK::Util::Filesystem.file?(spec_path) && PDK::Util::Filesystem.readable?(spec_path)
161
- result[:device] = device_path if PDK::Util::Filesystem.file?(device_path) && PDK::Util::Filesystem.readable?(device_path)
162
- result[:type_spec] = type_spec_path if PDK::Util::Filesystem.file?(type_spec_path) && PDK::Util::Filesystem.readable?(type_spec_path)
163
- result
164
- else
165
- nil
166
- end
167
- end
168
-
169
- # Generate a hash of data to be used when rendering object templates.
170
- #
171
- # Read `config_defaults.yml` from the root of the template directory (if
172
- # it exists) build a hash of values from the value of the `:global`
173
- # key.
174
- #
175
- # @return [Hash] The data that will be available to the template via the
176
- # `@configs` instance variable.
177
- #
178
- # @api private
179
- def object_config
180
- config_for(nil)
181
- end
182
-
183
- # Generate a hash of data to be used when rendering the specified
184
- # template.
185
- #
186
- # @param dest_path [String] The destination path of the file that the
187
- # data is for, relative to the root of the module.
188
- #
189
- # @return [Hash] The data that will be available to the template via the
190
- # `@configs` instance variable.
191
- #
192
- # @api private
193
- def config_for(dest_path, sync_config_path = nil)
194
- require 'pdk/util'
195
- require 'pdk/analytics'
196
-
197
- module_root = PDK::Util.module_root
198
- sync_config_path ||= File.join(module_root, '.sync.yml') unless module_root.nil?
199
- config_path = File.join(@path, 'config_defaults.yml')
200
-
201
- if @config.nil?
202
- require 'deep_merge'
203
- conf_defaults = read_config(config_path)
204
- @sync_config = read_config(sync_config_path) unless sync_config_path.nil?
205
- @config = conf_defaults
206
- @config.deep_merge!(@sync_config, knockout_prefix: '---') unless @sync_config.nil?
207
- end
208
- file_config = @config.fetch(:global, {})
209
- file_config['module_metadata'] = @module_metadata
210
- file_config.merge!(@config.fetch(dest_path, {})) unless dest_path.nil?
211
- file_config.merge!(@config).tap do |c|
212
- if uri.default?
213
- file_value = if c['unmanaged']
214
- 'unmanaged'
215
- elsif c['delete']
216
- 'deleted'
217
- elsif @sync_config && @sync_config.key?(dest_path)
218
- 'customized'
219
- else
220
- 'default'
221
- end
222
-
223
- PDK.analytics.event('TemplateDir', 'file', label: dest_path, value: file_value)
224
- end
225
- end
226
- end
227
-
228
- # Generates a hash of data from a given yaml file location.
229
- #
230
- # @param loc [String] The path of the yaml config file.
231
- #
232
- # @warn If the specified path is not a valid yaml file. Returns an empty Hash
233
- # if so.
234
- #
235
- # @return [Hash] The data that has been read in from the given yaml file.
236
- #
237
- # @api private
238
- def read_config(loc)
239
- if PDK::Util::Filesystem.file?(loc) && PDK::Util::Filesystem.readable?(loc)
240
- require 'yaml'
241
-
242
- begin
243
- YAML.safe_load(PDK::Util::Filesystem.read_file(loc), [], [], true)
244
- rescue Psych::SyntaxError => e
245
- PDK.logger.warn _("'%{file}' is not a valid YAML file: %{problem} %{context} at line %{line} column %{column}") % {
246
- file: loc,
247
- problem: e.problem,
248
- context: e.context,
249
- line: e.line,
250
- column: e.column,
251
- }
252
- {}
253
- end
254
- else
255
- {}
256
- end
257
- end
258
-
259
- # @return [Path, Boolean] The path to the Template and whether this path is temporary. Temporary paths
260
- # are deleted once the object has yielded
261
- # @api private
262
- def template_path(uri)
263
- [uri.shell_path, false]
264
- end
265
- end
266
- end
267
- end
268
- end