pdk 1.17.0 → 1.18.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.
Files changed (63) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +71 -2
  3. data/lib/pdk/cli/convert.rb +7 -9
  4. data/lib/pdk/cli/new/class.rb +2 -1
  5. data/lib/pdk/cli/new/defined_type.rb +2 -1
  6. data/lib/pdk/cli/new/provider.rb +2 -1
  7. data/lib/pdk/cli/new/task.rb +2 -1
  8. data/lib/pdk/cli/new/test.rb +2 -1
  9. data/lib/pdk/cli/new/transport.rb +2 -1
  10. data/lib/pdk/cli/remove/config.rb +80 -0
  11. data/lib/pdk/cli/remove.rb +20 -0
  12. data/lib/pdk/cli/set/config.rb +119 -0
  13. data/lib/pdk/cli/set.rb +20 -0
  14. data/lib/pdk/cli/update.rb +6 -8
  15. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  16. data/lib/pdk/cli/util.rb +1 -0
  17. data/lib/pdk/cli.rb +2 -0
  18. data/lib/pdk/config.rb +96 -13
  19. data/lib/pdk/context.rb +8 -5
  20. data/lib/pdk/generate/defined_type.rb +25 -32
  21. data/lib/pdk/generate/module.rb +11 -10
  22. data/lib/pdk/generate/provider.rb +16 -65
  23. data/lib/pdk/generate/puppet_class.rb +25 -31
  24. data/lib/pdk/generate/puppet_object.rb +83 -187
  25. data/lib/pdk/generate/resource_api_object.rb +55 -0
  26. data/lib/pdk/generate/task.rb +28 -46
  27. data/lib/pdk/generate/transport.rb +21 -75
  28. data/lib/pdk/generate.rb +1 -0
  29. data/lib/pdk/module/convert.rb +41 -23
  30. data/lib/pdk/module/release.rb +1 -1
  31. data/lib/pdk/module/update.rb +6 -10
  32. data/lib/pdk/module/update_manager.rb +7 -0
  33. data/lib/pdk/module.rb +0 -1
  34. data/lib/pdk/template/fetcher/git.rb +85 -0
  35. data/lib/pdk/template/fetcher/local.rb +28 -0
  36. data/lib/pdk/template/fetcher.rb +98 -0
  37. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  38. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  39. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  40. data/lib/pdk/template/renderer/v1.rb +25 -0
  41. data/lib/pdk/template/renderer.rb +96 -0
  42. data/lib/pdk/template/template_dir.rb +67 -0
  43. data/lib/pdk/template.rb +59 -0
  44. data/lib/pdk/tests/unit.rb +5 -0
  45. data/lib/pdk/util/json_finder.rb +84 -0
  46. data/lib/pdk/util/puppet_strings.rb +3 -3
  47. data/lib/pdk/util/template_uri.rb +4 -6
  48. data/lib/pdk/util.rb +4 -35
  49. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  50. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  51. data/lib/pdk/validate/invokable_validator.rb +2 -3
  52. data/lib/pdk/validate/validator.rb +7 -0
  53. data/lib/pdk/validate/validator_group.rb +1 -0
  54. data/lib/pdk/validate.rb +17 -10
  55. data/lib/pdk/version.rb +1 -1
  56. data/lib/pdk.rb +1 -1
  57. data/locales/pdk.pot +356 -228
  58. metadata +33 -12
  59. data/lib/pdk/module/template_dir/base.rb +0 -268
  60. data/lib/pdk/module/template_dir/git.rb +0 -91
  61. data/lib/pdk/module/template_dir/local.rb +0 -21
  62. data/lib/pdk/module/template_dir.rb +0 -115
  63. 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: 1.18.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: 2020-02-27 00:00:00.000000000 Z
11
+ date: 2020-05-12 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
@@ -315,6 +321,10 @@ files:
315
321
  - lib/pdk/cli/release.rb
316
322
  - lib/pdk/cli/release/prep.rb
317
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
318
328
  - lib/pdk/cli/test.rb
319
329
  - lib/pdk/cli/test/unit.rb
320
330
  - lib/pdk/cli/update.rb
@@ -324,6 +334,7 @@ files:
324
334
  - lib/pdk/cli/util/option_normalizer.rb
325
335
  - lib/pdk/cli/util/option_validator.rb
326
336
  - lib/pdk/cli/util/spinner.rb
337
+ - lib/pdk/cli/util/update_manager_printer.rb
327
338
  - lib/pdk/cli/validate.rb
328
339
  - lib/pdk/config.rb
329
340
  - lib/pdk/config/analytics_schema.json
@@ -350,6 +361,7 @@ files:
350
361
  - lib/pdk/generate/provider.rb
351
362
  - lib/pdk/generate/puppet_class.rb
352
363
  - lib/pdk/generate/puppet_object.rb
364
+ - lib/pdk/generate/resource_api_object.rb
353
365
  - lib/pdk/generate/task.rb
354
366
  - lib/pdk/generate/transport.rb
355
367
  - lib/pdk/i18n.rb
@@ -359,15 +371,20 @@ files:
359
371
  - lib/pdk/module/convert.rb
360
372
  - lib/pdk/module/metadata.rb
361
373
  - 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
374
  - lib/pdk/module/update.rb
367
375
  - lib/pdk/module/update_manager.rb
368
376
  - lib/pdk/report.rb
369
377
  - lib/pdk/report/event.rb
370
- - 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
371
388
  - lib/pdk/tests/unit.rb
372
389
  - lib/pdk/util.rb
373
390
  - lib/pdk/util/bundler.rb
@@ -375,6 +392,7 @@ files:
375
392
  - lib/pdk/util/env.rb
376
393
  - lib/pdk/util/filesystem.rb
377
394
  - lib/pdk/util/git.rb
395
+ - lib/pdk/util/json_finder.rb
378
396
  - lib/pdk/util/puppet_strings.rb
379
397
  - lib/pdk/util/puppet_version.rb
380
398
  - lib/pdk/util/ruby_version.rb
@@ -387,6 +405,8 @@ files:
387
405
  - lib/pdk/util/windows/process.rb
388
406
  - lib/pdk/util/windows/string.rb
389
407
  - lib/pdk/validate.rb
408
+ - lib/pdk/validate/control_repo/control_repo_validator_group.rb
409
+ - lib/pdk/validate/control_repo/environment_conf_validator.rb
390
410
  - lib/pdk/validate/external_command_validator.rb
391
411
  - lib/pdk/validate/internal_ruby_validator.rb
392
412
  - lib/pdk/validate/invokable_validator.rb
@@ -427,7 +447,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
427
447
  - !ruby/object:Gem::Version
428
448
  version: '0'
429
449
  requirements: []
430
- rubygems_version: 3.0.6
450
+ rubyforge_project:
451
+ rubygems_version: 2.6.14.3
431
452
  signing_key:
432
453
  specification_version: 4
433
454
  summary: A key part of the Puppet Development Kit, the shortest path to better modules
@@ -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
@@ -1,91 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/module/template_dir/base'
3
-
4
- module PDK
5
- module Module
6
- module TemplateDir
7
- class Git < Base
8
- def template_path(uri)
9
- # We don't do a checkout of local-path repos. There are lots of edge
10
- # cases or user un-expectations.
11
- if PDK::Util::Git.work_tree?(uri.shell_path)
12
- PDK.logger.warn _("Repository '%{repo}' has a work-tree; skipping git reset.") % {
13
- repo: uri.shell_path,
14
- }
15
- [uri.shell_path, false]
16
- else
17
- # This is either a bare local repo or a remote. either way it needs cloning.
18
- # A "remote" can also be git repo on the local filsystem.
19
- [clone_template_repo(uri), true]
20
- end
21
- end
22
-
23
- # For git repositories, this will return the URL to the repository and
24
- # a reference to the HEAD.
25
- #
26
- # @return [Hash{String => String}] A hash of identifying metadata.
27
- def metadata
28
- super.merge('template-url' => uri.metadata_format, 'template-ref' => cache_template_ref(@path))
29
- end
30
-
31
- private
32
-
33
- def cache_template_ref(path, ref = nil)
34
- require 'pdk/util/git'
35
-
36
- @template_ref ||= PDK::Util::Git.describe(File.join(path, '.git'), ref)
37
- end
38
-
39
- # @return [String] Path to working directory into which template repo has been cloned and reset
40
- #
41
- # @raise [PDK::CLI::FatalError] If unable to clone the given origin_repo into a tempdir.
42
- # @raise [PDK::CLI::FatalError] If reset HEAD of the cloned repo to desired ref.
43
- #
44
- # @api private
45
- def clone_template_repo(uri)
46
- # @todo When switching this over to using rugged, cache the cloned
47
- # template repo in `%AppData%` or `$XDG_CACHE_DIR` and update before
48
- # use.
49
- require 'pdk/util'
50
- require 'pdk/util/git'
51
-
52
- temp_dir = PDK::Util.make_tmpdir_name('pdk-templates')
53
- origin_repo = uri.bare_uri
54
- git_ref = uri.uri_fragment
55
-
56
- clone_result = PDK::Util::Git.git('clone', origin_repo, temp_dir)
57
-
58
- if clone_result[:exit_code].zero?
59
- checkout_template_ref(temp_dir, git_ref)
60
- else
61
- PDK.logger.error clone_result[:stdout]
62
- PDK.logger.error clone_result[:stderr]
63
- raise PDK::CLI::FatalError, _("Unable to clone git repository at '%{repo}' into '%{dest}'.") % { repo: origin_repo, dest: temp_dir }
64
- end
65
-
66
- PDK::Util.canonical_path(temp_dir)
67
- end
68
-
69
- # @api private
70
- def checkout_template_ref(path, ref)
71
- require 'pdk/util/git'
72
-
73
- if PDK::Util::Git.work_dir_clean?(path)
74
- Dir.chdir(path) do
75
- full_ref = PDK::Util::Git.ls_remote(path, ref)
76
- cache_template_ref(path, full_ref)
77
- reset_result = PDK::Util::Git.git('reset', '--hard', full_ref)
78
- return if reset_result[:exit_code].zero?
79
-
80
- PDK.logger.error reset_result[:stdout]
81
- PDK.logger.error reset_result[:stderr]
82
- raise PDK::CLI::FatalError, _("Unable to checkout '%{ref}' of git repository at '%{path}'.") % { ref: ref, path: path }
83
- end
84
- else
85
- PDK.logger.warn _("Uncommitted changes found when attempting to checkout '%{ref}' of git repository at '%{path}'; skipping git reset.") % { ref: ref, path: path }
86
- end
87
- end
88
- end
89
- end
90
- end
91
- end
@@ -1,21 +0,0 @@
1
- require 'pdk'
2
- require 'pdk/module/template_dir/base'
3
-
4
- module PDK
5
- module Module
6
- module TemplateDir
7
- class Local < Base
8
- def template_path(uri)
9
- [uri.shell_path, false]
10
- end
11
-
12
- # For plain fileystem directories, this will return the URL to the repository only.
13
- #
14
- # @return [Hash{String => String}] A hash of identifying metadata.
15
- def metadata
16
- super.merge('template-url' => uri.bare_uri)
17
- end
18
- end
19
- end
20
- end
21
- end
@@ -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