pdk 1.17.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +148 -11
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +1 -1
  5. data/lib/pdk/cli.rb +7 -1
  6. data/lib/pdk/cli/convert.rb +7 -9
  7. data/lib/pdk/cli/env.rb +52 -0
  8. data/lib/pdk/cli/exec/command.rb +11 -1
  9. data/lib/pdk/cli/new.rb +2 -0
  10. data/lib/pdk/cli/new/class.rb +2 -1
  11. data/lib/pdk/cli/new/defined_type.rb +2 -1
  12. data/lib/pdk/cli/new/fact.rb +29 -0
  13. data/lib/pdk/cli/new/function.rb +29 -0
  14. data/lib/pdk/cli/new/provider.rb +2 -1
  15. data/lib/pdk/cli/new/task.rb +2 -1
  16. data/lib/pdk/cli/new/test.rb +2 -1
  17. data/lib/pdk/cli/new/transport.rb +2 -1
  18. data/lib/pdk/cli/release.rb +1 -1
  19. data/lib/pdk/cli/release/publish.rb +11 -1
  20. data/lib/pdk/cli/remove.rb +20 -0
  21. data/lib/pdk/cli/remove/config.rb +80 -0
  22. data/lib/pdk/cli/set.rb +20 -0
  23. data/lib/pdk/cli/set/config.rb +119 -0
  24. data/lib/pdk/cli/update.rb +6 -8
  25. data/lib/pdk/cli/util.rb +1 -0
  26. data/lib/pdk/cli/util/option_validator.rb +6 -0
  27. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  28. data/lib/pdk/config.rb +96 -13
  29. data/lib/pdk/context.rb +8 -5
  30. data/lib/pdk/generate/defined_type.rb +25 -32
  31. data/lib/pdk/generate/fact.rb +25 -0
  32. data/lib/pdk/generate/function.rb +48 -0
  33. data/lib/pdk/generate/module.rb +11 -10
  34. data/lib/pdk/generate/provider.rb +15 -64
  35. data/lib/pdk/generate/puppet_class.rb +25 -31
  36. data/lib/pdk/generate/puppet_object.rb +83 -187
  37. data/lib/pdk/generate/task.rb +28 -46
  38. data/lib/pdk/generate/transport.rb +20 -74
  39. data/lib/pdk/module.rb +1 -1
  40. data/lib/pdk/module/convert.rb +43 -23
  41. data/lib/pdk/module/metadata.rb +6 -2
  42. data/lib/pdk/module/release.rb +8 -2
  43. data/lib/pdk/module/update.rb +7 -11
  44. data/lib/pdk/module/update_manager.rb +7 -0
  45. data/lib/pdk/report.rb +3 -3
  46. data/lib/pdk/report/event.rb +8 -2
  47. data/lib/pdk/template.rb +59 -0
  48. data/lib/pdk/template/fetcher.rb +98 -0
  49. data/lib/pdk/template/fetcher/git.rb +85 -0
  50. data/lib/pdk/template/fetcher/local.rb +28 -0
  51. data/lib/pdk/template/renderer.rb +96 -0
  52. data/lib/pdk/template/renderer/v1.rb +25 -0
  53. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  54. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  55. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  56. data/lib/pdk/template/template_dir.rb +67 -0
  57. data/lib/pdk/tests/unit.rb +8 -1
  58. data/lib/pdk/util.rb +4 -35
  59. data/lib/pdk/util/bundler.rb +1 -1
  60. data/lib/pdk/util/changelog_generator.rb +20 -3
  61. data/lib/pdk/util/json_finder.rb +85 -0
  62. data/lib/pdk/util/puppet_strings.rb +3 -3
  63. data/lib/pdk/util/puppet_version.rb +2 -2
  64. data/lib/pdk/util/ruby_version.rb +5 -1
  65. data/lib/pdk/util/template_uri.rb +9 -11
  66. data/lib/pdk/util/vendored_file.rb +1 -2
  67. data/lib/pdk/validate.rb +17 -10
  68. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  69. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  70. data/lib/pdk/validate/invokable_validator.rb +8 -4
  71. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +1 -1
  72. data/lib/pdk/validate/validator.rb +7 -0
  73. data/lib/pdk/validate/validator_group.rb +1 -0
  74. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +2 -2
  75. data/lib/pdk/version.rb +1 -1
  76. data/locales/pdk.pot +356 -228
  77. metadata +65 -28
  78. data/lib/pdk/module/template_dir.rb +0 -115
  79. data/lib/pdk/module/template_dir/base.rb +0 -268
  80. data/lib/pdk/module/template_dir/git.rb +0 -91
  81. data/lib/pdk/module/template_dir/local.rb +0 -21
  82. data/lib/pdk/template_file.rb +0 -96
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.17.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-27 00:00:00.000000000 Z
11
+ date: 2021-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -36,14 +36,14 @@ dependencies:
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: 0.7.1
39
+ version: 4.0.0
40
40
  type: :runtime
41
41
  prerelease: false
42
42
  version_requirements: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - "~>"
45
45
  - !ruby/object:Gem::Version
46
- version: 0.7.1
46
+ version: 4.0.0
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: cri
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -62,30 +62,42 @@ dependencies:
62
62
  name: diff-lcs
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - '='
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 1.4.4
68
+ - - "<"
66
69
  - !ruby/object:Gem::Version
67
- version: '1.3'
70
+ version: '1.5'
68
71
  type: :runtime
69
72
  prerelease: false
70
73
  version_requirements: !ruby/object:Gem::Requirement
71
74
  requirements:
72
- - - '='
75
+ - - ">="
73
76
  - !ruby/object:Gem::Version
74
- version: '1.3'
77
+ version: 1.4.4
78
+ - - "<"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.5'
75
81
  - !ruby/object:Gem::Dependency
76
82
  name: ffi
77
83
  requirement: !ruby/object:Gem::Requirement
78
84
  requirements:
79
- - - "~>"
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: 1.9.25
88
+ - - "<"
80
89
  - !ruby/object:Gem::Version
81
- version: 1.9.0
90
+ version: 2.0.0
82
91
  type: :runtime
83
92
  prerelease: false
84
93
  version_requirements: !ruby/object:Gem::Requirement
85
94
  requirements:
86
- - - "~>"
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 1.9.25
98
+ - - "<"
87
99
  - !ruby/object:Gem::Version
88
- version: 1.9.0
100
+ version: 2.0.0
89
101
  - !ruby/object:Gem::Dependency
90
102
  name: gettext-setup
91
103
  requirement: !ruby/object:Gem::Requirement
@@ -106,14 +118,14 @@ dependencies:
106
118
  requirements:
107
119
  - - '='
108
120
  - !ruby/object:Gem::Version
109
- version: 1.3.0
121
+ version: 2.0.0
110
122
  type: :runtime
111
123
  prerelease: false
112
124
  version_requirements: !ruby/object:Gem::Requirement
113
125
  requirements:
114
126
  - - '='
115
127
  - !ruby/object:Gem::Version
116
- version: 1.3.0
128
+ version: 2.0.0
117
129
  - !ruby/object:Gem::Dependency
118
130
  name: json-schema
119
131
  requirement: !ruby/object:Gem::Requirement
@@ -176,14 +188,14 @@ dependencies:
176
188
  requirements:
177
189
  - - "~>"
178
190
  - !ruby/object:Gem::Version
179
- version: '0.13'
191
+ version: '0.22'
180
192
  type: :runtime
181
193
  prerelease: false
182
194
  version_requirements: !ruby/object:Gem::Requirement
183
195
  requirements:
184
196
  - - "~>"
185
197
  - !ruby/object:Gem::Version
186
- version: '0.13'
198
+ version: '0.22'
187
199
  - !ruby/object:Gem::Dependency
188
200
  name: tty-spinner
189
201
  requirement: !ruby/object:Gem::Requirement
@@ -230,16 +242,22 @@ dependencies:
230
242
  name: facter
231
243
  requirement: !ruby/object:Gem::Requirement
232
244
  requirements:
233
- - - "~>"
245
+ - - ">="
234
246
  - !ruby/object:Gem::Version
235
247
  version: 2.5.1
248
+ - - "<"
249
+ - !ruby/object:Gem::Version
250
+ version: 5.0.0
236
251
  type: :runtime
237
252
  prerelease: false
238
253
  version_requirements: !ruby/object:Gem::Requirement
239
254
  requirements:
240
- - - "~>"
255
+ - - ">="
241
256
  - !ruby/object:Gem::Version
242
257
  version: 2.5.1
258
+ - - "<"
259
+ - !ruby/object:Gem::Version
260
+ version: 5.0.0
243
261
  - !ruby/object:Gem::Dependency
244
262
  name: httpclient
245
263
  requirement: !ruby/object:Gem::Requirement
@@ -294,6 +312,7 @@ files:
294
312
  - lib/pdk/cli/config/get.rb
295
313
  - lib/pdk/cli/console.rb
296
314
  - lib/pdk/cli/convert.rb
315
+ - lib/pdk/cli/env.rb
297
316
  - lib/pdk/cli/errors.rb
298
317
  - lib/pdk/cli/exec.rb
299
318
  - lib/pdk/cli/exec/command.rb
@@ -307,6 +326,8 @@ files:
307
326
  - lib/pdk/cli/new.rb
308
327
  - lib/pdk/cli/new/class.rb
309
328
  - lib/pdk/cli/new/defined_type.rb
329
+ - lib/pdk/cli/new/fact.rb
330
+ - lib/pdk/cli/new/function.rb
310
331
  - lib/pdk/cli/new/module.rb
311
332
  - lib/pdk/cli/new/provider.rb
312
333
  - lib/pdk/cli/new/task.rb
@@ -315,6 +336,10 @@ files:
315
336
  - lib/pdk/cli/release.rb
316
337
  - lib/pdk/cli/release/prep.rb
317
338
  - lib/pdk/cli/release/publish.rb
339
+ - lib/pdk/cli/remove.rb
340
+ - lib/pdk/cli/remove/config.rb
341
+ - lib/pdk/cli/set.rb
342
+ - lib/pdk/cli/set/config.rb
318
343
  - lib/pdk/cli/test.rb
319
344
  - lib/pdk/cli/test/unit.rb
320
345
  - lib/pdk/cli/update.rb
@@ -324,6 +349,7 @@ files:
324
349
  - lib/pdk/cli/util/option_normalizer.rb
325
350
  - lib/pdk/cli/util/option_validator.rb
326
351
  - lib/pdk/cli/util/spinner.rb
352
+ - lib/pdk/cli/util/update_manager_printer.rb
327
353
  - lib/pdk/cli/validate.rb
328
354
  - lib/pdk/config.rb
329
355
  - lib/pdk/config/analytics_schema.json
@@ -346,6 +372,8 @@ files:
346
372
  - lib/pdk/control_repo.rb
347
373
  - lib/pdk/generate.rb
348
374
  - lib/pdk/generate/defined_type.rb
375
+ - lib/pdk/generate/fact.rb
376
+ - lib/pdk/generate/function.rb
349
377
  - lib/pdk/generate/module.rb
350
378
  - lib/pdk/generate/provider.rb
351
379
  - lib/pdk/generate/puppet_class.rb
@@ -359,15 +387,20 @@ files:
359
387
  - lib/pdk/module/convert.rb
360
388
  - lib/pdk/module/metadata.rb
361
389
  - lib/pdk/module/release.rb
362
- - lib/pdk/module/template_dir.rb
363
- - lib/pdk/module/template_dir/base.rb
364
- - lib/pdk/module/template_dir/git.rb
365
- - lib/pdk/module/template_dir/local.rb
366
390
  - lib/pdk/module/update.rb
367
391
  - lib/pdk/module/update_manager.rb
368
392
  - lib/pdk/report.rb
369
393
  - lib/pdk/report/event.rb
370
- - lib/pdk/template_file.rb
394
+ - lib/pdk/template.rb
395
+ - lib/pdk/template/fetcher.rb
396
+ - lib/pdk/template/fetcher/git.rb
397
+ - lib/pdk/template/fetcher/local.rb
398
+ - lib/pdk/template/renderer.rb
399
+ - lib/pdk/template/renderer/v1.rb
400
+ - lib/pdk/template/renderer/v1/legacy_template_dir.rb
401
+ - lib/pdk/template/renderer/v1/renderer.rb
402
+ - lib/pdk/template/renderer/v1/template_file.rb
403
+ - lib/pdk/template/template_dir.rb
371
404
  - lib/pdk/tests/unit.rb
372
405
  - lib/pdk/util.rb
373
406
  - lib/pdk/util/bundler.rb
@@ -375,6 +408,7 @@ files:
375
408
  - lib/pdk/util/env.rb
376
409
  - lib/pdk/util/filesystem.rb
377
410
  - lib/pdk/util/git.rb
411
+ - lib/pdk/util/json_finder.rb
378
412
  - lib/pdk/util/puppet_strings.rb
379
413
  - lib/pdk/util/puppet_version.rb
380
414
  - lib/pdk/util/ruby_version.rb
@@ -387,6 +421,8 @@ files:
387
421
  - lib/pdk/util/windows/process.rb
388
422
  - lib/pdk/util/windows/string.rb
389
423
  - lib/pdk/validate.rb
424
+ - lib/pdk/validate/control_repo/control_repo_validator_group.rb
425
+ - lib/pdk/validate/control_repo/environment_conf_validator.rb
390
426
  - lib/pdk/validate/external_command_validator.rb
391
427
  - lib/pdk/validate/internal_ruby_validator.rb
392
428
  - lib/pdk/validate/invokable_validator.rb
@@ -412,7 +448,7 @@ files:
412
448
  homepage: https://github.com/puppetlabs/pdk
413
449
  licenses: []
414
450
  metadata: {}
415
- post_install_message:
451
+ post_install_message:
416
452
  rdoc_options: []
417
453
  require_paths:
418
454
  - lib
@@ -420,15 +456,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
420
456
  requirements:
421
457
  - - ">="
422
458
  - !ruby/object:Gem::Version
423
- version: 2.1.9
459
+ version: 2.4.0
424
460
  required_rubygems_version: !ruby/object:Gem::Requirement
425
461
  requirements:
426
462
  - - ">="
427
463
  - !ruby/object:Gem::Version
428
464
  version: '0'
429
465
  requirements: []
430
- rubygems_version: 3.0.6
431
- signing_key:
466
+ rubyforge_project:
467
+ rubygems_version: 2.7.6.2
468
+ signing_key:
432
469
  specification_version: 4
433
470
  summary: A key part of the Puppet Development Kit, the shortest path to better modules
434
471
  test_files: []
@@ -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