pdk 1.14.1 → 1.18.1

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 +155 -2
  3. data/lib/pdk.rb +28 -19
  4. data/lib/pdk/answer_file.rb +2 -95
  5. data/lib/pdk/bolt.rb +19 -0
  6. data/lib/pdk/cli.rb +4 -5
  7. data/lib/pdk/cli/bundle.rb +5 -1
  8. data/lib/pdk/cli/config.rb +3 -1
  9. data/lib/pdk/cli/config/get.rb +3 -1
  10. data/lib/pdk/cli/console.rb +1 -1
  11. data/lib/pdk/cli/convert.rb +16 -10
  12. data/lib/pdk/cli/exec.rb +2 -1
  13. data/lib/pdk/cli/exec/command.rb +45 -4
  14. data/lib/pdk/cli/exec_group.rb +78 -43
  15. data/lib/pdk/cli/get.rb +20 -0
  16. data/lib/pdk/cli/get/config.rb +24 -0
  17. data/lib/pdk/cli/module/build.rb +1 -1
  18. data/lib/pdk/cli/module/generate.rb +1 -1
  19. data/lib/pdk/cli/new/class.rb +2 -2
  20. data/lib/pdk/cli/new/defined_type.rb +2 -2
  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.rb +192 -0
  26. data/lib/pdk/cli/release/prep.rb +39 -0
  27. data/lib/pdk/cli/release/publish.rb +40 -0
  28. data/lib/pdk/cli/remove.rb +20 -0
  29. data/lib/pdk/cli/remove/config.rb +80 -0
  30. data/lib/pdk/cli/set.rb +20 -0
  31. data/lib/pdk/cli/set/config.rb +119 -0
  32. data/lib/pdk/cli/update.rb +18 -8
  33. data/lib/pdk/cli/util.rb +7 -3
  34. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  35. data/lib/pdk/cli/validate.rb +26 -44
  36. data/lib/pdk/config.rb +265 -8
  37. data/lib/pdk/config/ini_file.rb +183 -0
  38. data/lib/pdk/config/ini_file_setting.rb +39 -0
  39. data/lib/pdk/config/namespace.rb +26 -6
  40. data/lib/pdk/config/setting.rb +3 -2
  41. data/lib/pdk/context.rb +99 -0
  42. data/lib/pdk/context/control_repo.rb +60 -0
  43. data/lib/pdk/context/module.rb +28 -0
  44. data/lib/pdk/context/none.rb +22 -0
  45. data/lib/pdk/control_repo.rb +90 -0
  46. data/lib/pdk/generate.rb +1 -0
  47. data/lib/pdk/generate/defined_type.rb +25 -32
  48. data/lib/pdk/generate/module.rb +42 -35
  49. data/lib/pdk/generate/provider.rb +16 -65
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -189
  52. data/lib/pdk/generate/resource_api_object.rb +55 -0
  53. data/lib/pdk/generate/task.rb +28 -46
  54. data/lib/pdk/generate/transport.rb +21 -75
  55. data/lib/pdk/module.rb +1 -1
  56. data/lib/pdk/module/build.rb +38 -25
  57. data/lib/pdk/module/convert.rb +61 -42
  58. data/lib/pdk/module/metadata.rb +1 -3
  59. data/lib/pdk/module/release.rb +254 -0
  60. data/lib/pdk/module/update.rb +24 -16
  61. data/lib/pdk/module/update_manager.rb +8 -1
  62. data/lib/pdk/report.rb +18 -12
  63. data/lib/pdk/report/event.rb +6 -3
  64. data/lib/pdk/template.rb +59 -0
  65. data/lib/pdk/template/fetcher.rb +98 -0
  66. data/lib/pdk/template/fetcher/git.rb +85 -0
  67. data/lib/pdk/template/fetcher/local.rb +28 -0
  68. data/lib/pdk/template/renderer.rb +96 -0
  69. data/lib/pdk/template/renderer/v1.rb +25 -0
  70. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  71. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  72. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  73. data/lib/pdk/template/template_dir.rb +67 -0
  74. data/lib/pdk/tests/unit.rb +5 -0
  75. data/lib/pdk/util.rb +55 -45
  76. data/lib/pdk/util/bundler.rb +9 -9
  77. data/lib/pdk/util/changelog_generator.rb +120 -0
  78. data/lib/pdk/util/env.rb +28 -11
  79. data/lib/pdk/util/filesystem.rb +62 -2
  80. data/lib/pdk/util/git.rb +60 -8
  81. data/lib/pdk/util/json_finder.rb +84 -0
  82. data/lib/pdk/util/puppet_strings.rb +3 -3
  83. data/lib/pdk/util/puppet_version.rb +4 -5
  84. data/lib/pdk/util/ruby_version.rb +9 -6
  85. data/lib/pdk/util/template_uri.rb +60 -48
  86. data/lib/pdk/util/version.rb +4 -4
  87. data/lib/pdk/validate.rb +79 -25
  88. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  89. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  90. data/lib/pdk/validate/external_command_validator.rb +208 -0
  91. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  92. data/lib/pdk/validate/invokable_validator.rb +215 -0
  93. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  94. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  95. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  96. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  97. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  98. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  99. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  100. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  101. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  102. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  103. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  104. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  105. data/lib/pdk/validate/validator.rb +118 -0
  106. data/lib/pdk/validate/validator_group.rb +104 -0
  107. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  108. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  109. data/lib/pdk/version.rb +1 -1
  110. data/locales/pdk.pot +755 -319
  111. metadata +66 -24
  112. data/lib/pdk/module/templatedir.rb +0 -391
  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 -137
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -137
  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.14.1
4
+ version: 1.18.1
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-11-01 00:00:00.000000000 Z
11
+ date: 2020-07-17 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
@@ -286,6 +292,7 @@ files:
286
292
  - lib/pdk/analytics/client/noop.rb
287
293
  - lib/pdk/analytics/util.rb
288
294
  - lib/pdk/answer_file.rb
295
+ - lib/pdk/bolt.rb
289
296
  - lib/pdk/cli.rb
290
297
  - lib/pdk/cli/build.rb
291
298
  - lib/pdk/cli/bundle.rb
@@ -298,6 +305,8 @@ files:
298
305
  - lib/pdk/cli/exec/command.rb
299
306
  - lib/pdk/cli/exec/interactive_command.rb
300
307
  - lib/pdk/cli/exec_group.rb
308
+ - lib/pdk/cli/get.rb
309
+ - lib/pdk/cli/get/config.rb
301
310
  - lib/pdk/cli/module.rb
302
311
  - lib/pdk/cli/module/build.rb
303
312
  - lib/pdk/cli/module/generate.rb
@@ -309,6 +318,13 @@ files:
309
318
  - lib/pdk/cli/new/task.rb
310
319
  - lib/pdk/cli/new/test.rb
311
320
  - lib/pdk/cli/new/transport.rb
321
+ - lib/pdk/cli/release.rb
322
+ - lib/pdk/cli/release/prep.rb
323
+ - lib/pdk/cli/release/publish.rb
324
+ - lib/pdk/cli/remove.rb
325
+ - lib/pdk/cli/remove/config.rb
326
+ - lib/pdk/cli/set.rb
327
+ - lib/pdk/cli/set/config.rb
312
328
  - lib/pdk/cli/test.rb
313
329
  - lib/pdk/cli/test/unit.rb
314
330
  - lib/pdk/cli/update.rb
@@ -318,10 +334,13 @@ files:
318
334
  - lib/pdk/cli/util/option_normalizer.rb
319
335
  - lib/pdk/cli/util/option_validator.rb
320
336
  - lib/pdk/cli/util/spinner.rb
337
+ - lib/pdk/cli/util/update_manager_printer.rb
321
338
  - lib/pdk/cli/validate.rb
322
339
  - lib/pdk/config.rb
323
340
  - lib/pdk/config/analytics_schema.json
324
341
  - lib/pdk/config/errors.rb
342
+ - lib/pdk/config/ini_file.rb
343
+ - lib/pdk/config/ini_file_setting.rb
325
344
  - lib/pdk/config/json.rb
326
345
  - lib/pdk/config/json_schema_namespace.rb
327
346
  - lib/pdk/config/json_schema_setting.rb
@@ -331,12 +350,18 @@ files:
331
350
  - lib/pdk/config/validator.rb
332
351
  - lib/pdk/config/yaml.rb
333
352
  - lib/pdk/config/yaml_with_schema.rb
353
+ - lib/pdk/context.rb
354
+ - lib/pdk/context/control_repo.rb
355
+ - lib/pdk/context/module.rb
356
+ - lib/pdk/context/none.rb
357
+ - lib/pdk/control_repo.rb
334
358
  - lib/pdk/generate.rb
335
359
  - lib/pdk/generate/defined_type.rb
336
360
  - lib/pdk/generate/module.rb
337
361
  - lib/pdk/generate/provider.rb
338
362
  - lib/pdk/generate/puppet_class.rb
339
363
  - lib/pdk/generate/puppet_object.rb
364
+ - lib/pdk/generate/resource_api_object.rb
340
365
  - lib/pdk/generate/task.rb
341
366
  - lib/pdk/generate/transport.rb
342
367
  - lib/pdk/i18n.rb
@@ -345,18 +370,29 @@ files:
345
370
  - lib/pdk/module/build.rb
346
371
  - lib/pdk/module/convert.rb
347
372
  - lib/pdk/module/metadata.rb
348
- - lib/pdk/module/templatedir.rb
373
+ - lib/pdk/module/release.rb
349
374
  - lib/pdk/module/update.rb
350
375
  - lib/pdk/module/update_manager.rb
351
376
  - lib/pdk/report.rb
352
377
  - lib/pdk/report/event.rb
353
- - lib/pdk/template_file.rb
378
+ - lib/pdk/template.rb
379
+ - lib/pdk/template/fetcher.rb
380
+ - lib/pdk/template/fetcher/git.rb
381
+ - lib/pdk/template/fetcher/local.rb
382
+ - lib/pdk/template/renderer.rb
383
+ - lib/pdk/template/renderer/v1.rb
384
+ - lib/pdk/template/renderer/v1/legacy_template_dir.rb
385
+ - lib/pdk/template/renderer/v1/renderer.rb
386
+ - lib/pdk/template/renderer/v1/template_file.rb
387
+ - lib/pdk/template/template_dir.rb
354
388
  - lib/pdk/tests/unit.rb
355
389
  - lib/pdk/util.rb
356
390
  - lib/pdk/util/bundler.rb
391
+ - lib/pdk/util/changelog_generator.rb
357
392
  - lib/pdk/util/env.rb
358
393
  - lib/pdk/util/filesystem.rb
359
394
  - lib/pdk/util/git.rb
395
+ - lib/pdk/util/json_finder.rb
360
396
  - lib/pdk/util/puppet_strings.rb
361
397
  - lib/pdk/util/puppet_version.rb
362
398
  - lib/pdk/util/ruby_version.rb
@@ -369,21 +405,27 @@ files:
369
405
  - lib/pdk/util/windows/process.rb
370
406
  - lib/pdk/util/windows/string.rb
371
407
  - lib/pdk/validate.rb
372
- - lib/pdk/validate/base_validator.rb
373
- - lib/pdk/validate/metadata/metadata_json_lint.rb
374
- - lib/pdk/validate/metadata/metadata_syntax.rb
375
- - lib/pdk/validate/metadata_validator.rb
376
- - lib/pdk/validate/puppet/puppet_epp.rb
377
- - lib/pdk/validate/puppet/puppet_lint.rb
378
- - lib/pdk/validate/puppet/puppet_syntax.rb
379
- - lib/pdk/validate/puppet_validator.rb
380
- - lib/pdk/validate/ruby/rubocop.rb
381
- - lib/pdk/validate/ruby_validator.rb
382
- - lib/pdk/validate/tasks/metadata_lint.rb
383
- - lib/pdk/validate/tasks/name.rb
384
- - lib/pdk/validate/tasks_validator.rb
385
- - lib/pdk/validate/yaml/syntax.rb
386
- - lib/pdk/validate/yaml_validator.rb
408
+ - lib/pdk/validate/control_repo/control_repo_validator_group.rb
409
+ - lib/pdk/validate/control_repo/environment_conf_validator.rb
410
+ - lib/pdk/validate/external_command_validator.rb
411
+ - lib/pdk/validate/internal_ruby_validator.rb
412
+ - lib/pdk/validate/invokable_validator.rb
413
+ - lib/pdk/validate/metadata/metadata_json_lint_validator.rb
414
+ - lib/pdk/validate/metadata/metadata_syntax_validator.rb
415
+ - lib/pdk/validate/metadata/metadata_validator_group.rb
416
+ - lib/pdk/validate/puppet/puppet_epp_validator.rb
417
+ - lib/pdk/validate/puppet/puppet_lint_validator.rb
418
+ - lib/pdk/validate/puppet/puppet_syntax_validator.rb
419
+ - lib/pdk/validate/puppet/puppet_validator_group.rb
420
+ - lib/pdk/validate/ruby/ruby_rubocop_validator.rb
421
+ - lib/pdk/validate/ruby/ruby_validator_group.rb
422
+ - lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb
423
+ - lib/pdk/validate/tasks/tasks_name_validator.rb
424
+ - lib/pdk/validate/tasks/tasks_validator_group.rb
425
+ - lib/pdk/validate/validator.rb
426
+ - lib/pdk/validate/validator_group.rb
427
+ - lib/pdk/validate/yaml/yaml_syntax_validator.rb
428
+ - lib/pdk/validate/yaml/yaml_validator_group.rb
387
429
  - lib/pdk/version.rb
388
430
  - locales/config.yaml
389
431
  - locales/pdk.pot
@@ -405,7 +447,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
405
447
  - !ruby/object:Gem::Version
406
448
  version: '0'
407
449
  requirements: []
408
- rubygems_version: 3.0.6
450
+ rubygems_version: 3.0.3
409
451
  signing_key:
410
452
  specification_version: 4
411
453
  summary: A key part of the Puppet Development Kit, the shortest path to better modules
@@ -1,391 +0,0 @@
1
- require 'pdk'
2
-
3
- module PDK
4
- module Module
5
- class TemplateDir
6
- attr_accessor :module_metadata
7
- attr_reader :uri
8
-
9
- # Initialises the TemplateDir object with the path or URL to the template
10
- # and the block of code to run to be run while the template is available.
11
- #
12
- # The template directory is only guaranteed to be available on disk
13
- # within the scope of the block passed to this method.
14
- #
15
- # @param uri [PDK::Util::TemplateURI] The path to a directory to use as the
16
- # template or a URI to a git repository.
17
- # @param module_metadata [Hash] A Hash containing the module metadata.
18
- # Defaults to an empty Hash.
19
- # @yieldparam self [PDK::Module::TemplateDir] The initialised object with
20
- # the template available on disk.
21
- #
22
- # @example Using a git repository as a template
23
- # PDK::Module::TemplateDir.new('https://github.com/puppetlabs/pdk-templates') do |t|
24
- # t.render do |filename, content|
25
- # File.open(filename, 'w') do |file|
26
- # file.write(content)
27
- # end
28
- # end
29
- # end
30
- #
31
- # @raise [ArgumentError] If no block is given to this method.
32
- # @raise [PDK::CLI::FatalError] (see #clone_repo)
33
- # @raise [ArgumentError] (see #validate_module_template!)
34
- #
35
- # @api public
36
- def initialize(uri, module_metadata = {}, init = false)
37
- require 'pdk/analytics'
38
- require 'pdk/util/template_uri'
39
- require 'pdk/util/git'
40
-
41
- unless block_given?
42
- raise ArgumentError, _('%{class_name} must be initialized with a block.') % { class_name: self.class.name }
43
- end
44
- unless uri.is_a? PDK::Util::TemplateURI
45
- raise ArgumentError, _('PDK::Module::TemplateDir.new must be initialized with a PDK::Util::TemplateURI, got a %{uri_type}') % { uri_type: uri.class }
46
- end
47
-
48
- if PDK::Util::Git.repo?(uri.git_remote)
49
- # This is either a bare local repo or a remote. either way it needs cloning.
50
- @path = clone_template_repo(uri)
51
- temp_dir_clone = true
52
- else
53
- # if it is a local path & non-bare repo then we can use it directly.
54
- # Still have to check the branch.
55
- @path = uri.shell_path
56
- # We don't do a checkout of local-path repos. There are lots of edge
57
- # cases or user un-expectations.
58
- if PDK::Util::Git.work_tree?(@path)
59
- PDK.logger.warn _("Repository '%{repo}' has a work-tree; skipping git reset.") % {
60
- repo: @path,
61
- }
62
- end
63
- end
64
- @uri = uri
65
-
66
- @init = init
67
- @moduleroot_dir = File.join(@path, 'moduleroot')
68
- @moduleroot_init = File.join(@path, 'moduleroot_init')
69
- @dirs = [@moduleroot_dir]
70
- @dirs << @moduleroot_init if @init
71
- @object_dir = File.join(@path, 'object_templates')
72
-
73
- validate_module_template!
74
-
75
- @module_metadata = module_metadata
76
-
77
- template_type = uri.default? ? 'default' : 'custom'
78
- PDK.analytics.event('TemplateDir', 'initialize', label: template_type)
79
-
80
- yield self
81
- ensure
82
- # If we cloned a git repo to get the template, remove the clone once
83
- # we're done with it.
84
- if temp_dir_clone
85
- require 'fileutils'
86
- FileUtils.remove_dir(@path)
87
- end
88
- end
89
-
90
- # Retrieve identifying metadata for the template.
91
- #
92
- # For git repositories, this will return the URL to the repository and
93
- # a reference to the HEAD.
94
- #
95
- # @return [Hash{String => String}] A hash of identifying metadata.
96
- #
97
- # @api public
98
- def metadata
99
- require 'pdk/util/version'
100
-
101
- {
102
- 'pdk-version' => PDK::Util::Version.version_string,
103
- 'template-url' => uri.metadata_format,
104
- 'template-ref' => cache_template_ref(@path),
105
- }
106
- end
107
-
108
- # Loop through the files in the template, yielding each rendered file to
109
- # the supplied block.
110
- #
111
- # @yieldparam dest_path [String] The path of the destination file,
112
- # relative to the root of the module.
113
- # @yieldparam dest_content [String] The rendered content of the
114
- # destination file.
115
- #
116
- # @raise [PDK::CLI::FatalError] If the template fails to render.
117
- #
118
- # @return [void]
119
- #
120
- # @api public
121
- def render
122
- require 'pdk/template_file'
123
-
124
- PDK::Module::TemplateDir.files_in_template(@dirs).each do |template_file, template_loc|
125
- template_file = template_file.to_s
126
- PDK.logger.debug(_("Rendering '%{template}'...") % { template: template_file })
127
- dest_path = template_file.sub(%r{\.erb\Z}, '')
128
- config = config_for(dest_path)
129
-
130
- dest_status = if template_loc.start_with?(@moduleroot_init)
131
- :init
132
- else
133
- :manage
134
- end
135
-
136
- if config['unmanaged']
137
- dest_status = :unmanage
138
- elsif config['delete']
139
- dest_status = :delete
140
- else
141
- begin
142
- dest_content = PDK::TemplateFile.new(File.join(template_loc, template_file), configs: config, template_dir: self).render
143
- rescue => error
144
- error_msg = _(
145
- "Failed to render template '%{template}'\n" \
146
- '%{exception}: %{message}',
147
- ) % { template: template_file, exception: error.class, message: error.message }
148
- raise PDK::CLI::FatalError, error_msg
149
- end
150
- end
151
-
152
- yield dest_path, dest_content, dest_status
153
- end
154
- end
155
-
156
- # Searches the template directory for template files that can be used to
157
- # render files for the specified object type.
158
- #
159
- # @param object_type [Symbol] The object type, e.g. (`:class`,
160
- # `:defined_type`, `:fact`, etc).
161
- #
162
- # @return [Hash{Symbol => String}] if the templates are available in the
163
- # template dir, otherwise `nil`. The returned hash can contain two keys,
164
- # :object contains the path on disk to the template for the object, :spec
165
- # contains the path on disk to the template for the object's spec file
166
- # (if available).
167
- #
168
- # @api public
169
- def object_template_for(object_type)
170
- object_path = File.join(@object_dir, "#{object_type}.erb")
171
- type_path = File.join(@object_dir, "#{object_type}_type.erb")
172
- device_path = File.join(@object_dir, "#{object_type}_device.erb")
173
- spec_path = File.join(@object_dir, "#{object_type}_spec.erb")
174
- type_spec_path = File.join(@object_dir, "#{object_type}_type_spec.erb")
175
-
176
- if File.file?(object_path) && File.readable?(object_path)
177
- result = { object: object_path }
178
- result[:type] = type_path if File.file?(type_path) && File.readable?(type_path)
179
- result[:spec] = spec_path if File.file?(spec_path) && File.readable?(spec_path)
180
- result[:device] = device_path if File.file?(device_path) && File.readable?(device_path)
181
- result[:type_spec] = type_spec_path if File.file?(type_spec_path) && File.readable?(type_spec_path)
182
- result
183
- else
184
- nil
185
- end
186
- end
187
-
188
- # Generate a hash of data to be used when rendering object templates.
189
- #
190
- # Read `config_defaults.yml` from the root of the template directory (if
191
- # it exists) build a hash of values from the value of the `:global`
192
- # key.
193
- #
194
- # @return [Hash] The data that will be available to the template via the
195
- # `@configs` instance variable.
196
- #
197
- # @api private
198
- def object_config
199
- config_for(nil)
200
- end
201
-
202
- # Validate the content of the template directory.
203
- #
204
- # @raise [ArgumentError] If the specified path is not a directory.
205
- # @raise [ArgumentError] If the template directory does not contain
206
- # a directory called 'moduleroot'.
207
- #
208
- # @return [void]
209
- #
210
- # @api private
211
- def validate_module_template!
212
- # rubocop:disable Style/GuardClause
213
- unless File.directory?(@path)
214
- require 'pdk/util'
215
-
216
- if PDK::Util.package_install? && File.fnmatch?(File.join(PDK::Util.package_cachedir, '*'), @path)
217
- raise ArgumentError, _('The built-in template has substantially changed. Please run "pdk convert" on your module to continue.')
218
- else
219
- raise ArgumentError, _("The specified template '%{path}' is not a directory.") % { path: @path }
220
- end
221
- end
222
-
223
- unless File.directory?(@moduleroot_dir)
224
- raise ArgumentError, _("The template at '%{path}' does not contain a 'moduleroot/' directory.") % { path: @path }
225
- end
226
-
227
- unless File.directory?(@moduleroot_init)
228
- # rubocop:disable Metrics/LineLength
229
- 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: @path }
230
- # rubocop:enable Metrics/LineLength Style/GuardClause
231
- end
232
- # rubocop:enable Style/GuardClause
233
- end
234
-
235
- # Get a list of template files in the template directory.
236
- #
237
- # @return [Hash{String=>String}] A hash of key file names and
238
- # value locations.
239
- #
240
- # @api public
241
- def self.files_in_template(dirs)
242
- temp_paths = []
243
- dirlocs = []
244
- dirs.each do |dir|
245
- raise ArgumentError, _("The directory '%{dir}' doesn't exist") % { dir: dir } unless Dir.exist?(dir)
246
- temp_paths += Dir.glob(File.join(dir, '**', '*'), File::FNM_DOTMATCH).select do |template_path|
247
- if File.file?(template_path) && !File.symlink?(template_path)
248
- dirlocs << dir
249
- end
250
- end
251
- temp_paths.map do |template_path|
252
- template_path.sub!(%r{\A#{Regexp.escape(dir)}#{Regexp.escape(File::SEPARATOR)}}, '')
253
- end
254
- end
255
- Hash[temp_paths.zip dirlocs]
256
- end
257
-
258
- # Generate a hash of data to be used when rendering the specified
259
- # template.
260
- #
261
- # @param dest_path [String] The destination path of the file that the
262
- # data is for, relative to the root of the module.
263
- #
264
- # @return [Hash] The data that will be available to the template via the
265
- # `@configs` instance variable.
266
- #
267
- # @api private
268
- def config_for(dest_path, sync_config_path = nil)
269
- require 'pdk/util'
270
- require 'pdk/analytics'
271
-
272
- module_root = PDK::Util.module_root
273
- sync_config_path ||= File.join(module_root, '.sync.yml') unless module_root.nil?
274
- config_path = File.join(@path, 'config_defaults.yml')
275
-
276
- if @config.nil?
277
- require 'deep_merge'
278
- conf_defaults = read_config(config_path)
279
- @sync_config = read_config(sync_config_path) unless sync_config_path.nil?
280
- @config = conf_defaults
281
- @config.deep_merge!(@sync_config, knockout_prefix: '---') unless @sync_config.nil?
282
- end
283
- file_config = @config.fetch(:global, {})
284
- file_config['module_metadata'] = @module_metadata
285
- file_config.merge!(@config.fetch(dest_path, {})) unless dest_path.nil?
286
- file_config.merge!(@config).tap do |c|
287
- if uri.default?
288
- file_value = if c['unmanaged']
289
- 'unmanaged'
290
- elsif c['delete']
291
- 'deleted'
292
- elsif @sync_config && @sync_config.key?(dest_path)
293
- 'customized'
294
- else
295
- 'default'
296
- end
297
-
298
- PDK.analytics.event('TemplateDir', 'file', label: dest_path, value: file_value)
299
- end
300
- end
301
- end
302
-
303
- # Generates a hash of data from a given yaml file location.
304
- #
305
- # @param loc [String] The path of the yaml config file.
306
- #
307
- # @warn If the specified path is not a valid yaml file. Returns an empty Hash
308
- # if so.
309
- #
310
- # @return [Hash] The data that has been read in from the given yaml file.
311
- #
312
- # @api private
313
- def read_config(loc)
314
- if File.file?(loc) && File.readable?(loc)
315
- require 'yaml'
316
-
317
- begin
318
- YAML.safe_load(File.read(loc), [], [], true)
319
- rescue Psych::SyntaxError => e
320
- PDK.logger.warn _("'%{file}' is not a valid YAML file: %{problem} %{context} at line %{line} column %{column}") % {
321
- file: loc,
322
- problem: e.problem,
323
- context: e.context,
324
- line: e.line,
325
- column: e.column,
326
- }
327
- {}
328
- end
329
- else
330
- {}
331
- end
332
- end
333
-
334
- # @return [String] Path to working directory into which template repo has been cloned and reset
335
- #
336
- # @raise [PDK::CLI::FatalError] If unable to clone the given origin_repo into a tempdir.
337
- # @raise [PDK::CLI::FatalError] If reset HEAD of the cloned repo to desired ref.
338
- #
339
- # @api private
340
- def clone_template_repo(uri)
341
- # @todo When switching this over to using rugged, cache the cloned
342
- # template repo in `%AppData%` or `$XDG_CACHE_DIR` and update before
343
- # use.
344
- require 'pdk/util'
345
- require 'pdk/util/git'
346
-
347
- temp_dir = PDK::Util.make_tmpdir_name('pdk-templates')
348
- origin_repo = uri.git_remote
349
- git_ref = uri.git_ref
350
-
351
- clone_result = PDK::Util::Git.git('clone', origin_repo, temp_dir)
352
-
353
- if clone_result[:exit_code].zero?
354
- checkout_template_ref(temp_dir, git_ref)
355
- else
356
- PDK.logger.error clone_result[:stdout]
357
- PDK.logger.error clone_result[:stderr]
358
- raise PDK::CLI::FatalError, _("Unable to clone git repository at '%{repo}' into '%{dest}'.") % { repo: origin_repo, dest: temp_dir }
359
- end
360
-
361
- PDK::Util.canonical_path(temp_dir)
362
- end
363
-
364
- # @api private
365
- def checkout_template_ref(path, ref)
366
- require 'pdk/util/git'
367
-
368
- if PDK::Util::Git.work_dir_clean?(path)
369
- Dir.chdir(path) do
370
- full_ref = PDK::Util::Git.ls_remote(path, ref)
371
- cache_template_ref(path, full_ref)
372
- reset_result = PDK::Util::Git.git('reset', '--hard', full_ref)
373
- return if reset_result[:exit_code].zero?
374
-
375
- PDK.logger.error reset_result[:stdout]
376
- PDK.logger.error reset_result[:stderr]
377
- raise PDK::CLI::FatalError, _("Unable to checkout '%{ref}' of git repository at '%{path}'.") % { ref: ref, path: path }
378
- end
379
- else
380
- PDK.logger.warn _("Uncommitted changes found when attempting to checkout '%{ref}' of git repository at '%{path}'; skipping git reset.") % { ref: ref, path: path }
381
- end
382
- end
383
-
384
- def cache_template_ref(path, ref = nil)
385
- require 'pdk/util/git'
386
-
387
- @template_ref ||= PDK::Util::Git.describe(File.join(path, '.git'), ref)
388
- end
389
- end
390
- end
391
- end