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
@@ -3,46 +3,40 @@ require 'pdk'
3
3
  module PDK
4
4
  module Generate
5
5
  class PuppetClass < PuppetObject
6
- OBJECT_TYPE = :class
7
6
  PUPPET_STRINGS_TYPE = 'puppet_classes'.freeze
8
7
 
9
- # Prepares the data needed to render the new Puppet class template.
10
- #
11
- # @return [Hash{Symbol => Object}] a hash of information that will be
12
- # provided to the class and class spec templates during rendering.
13
- def template_data
14
- data = { name: object_name }
8
+ def initialize(*_args)
9
+ super
10
+ object_name_parts = @object_name.split('::')
15
11
 
16
- data
12
+ @object_name = if object_name_parts.first == module_name
13
+ object_name
14
+ else
15
+ [module_name, object_name].join('::')
16
+ end
17
17
  end
18
18
 
19
- # Calculates the path to the .pp file that the new class will be written
20
- # to.
21
- #
22
- # @return [String] the path where the new class will be written.
23
- def target_object_path
24
- @target_pp_path ||= begin
25
- class_name_parts = object_name.split('::')[1..-1]
26
- class_name_parts << 'init' if class_name_parts.empty?
27
-
28
- "#{File.join(module_dir, 'manifests', *class_name_parts)}.pp"
29
- end
19
+ def friendly_name
20
+ 'Puppet Class'.freeze
30
21
  end
31
22
 
32
- # Calculates the path to the file where the tests for the new class will
33
- # be written.
34
- #
35
- # @return [String] the path where the tests for the new class will be
36
- # written.
37
- def target_spec_path
38
- @target_spec_path ||= begin
39
- class_name_parts = object_name.split('::')
23
+ def template_files
24
+ # Calculate the class tests name
25
+ class_name_parts = object_name.split('::')
26
+ # Drop the module name if the object name contains multiple parts
27
+ class_name_parts.delete_at(0) if class_name_parts.length > 1
28
+ files = { 'class_spec.erb' => File.join('spec', 'classes', *class_name_parts) + '_spec.rb' }
29
+ return files if spec_only?
30
+
31
+ class_name_parts = object_name.split('::')[1..-1]
32
+ class_name_parts << 'init' if class_name_parts.empty?
33
+ files['class.erb'] = File.join('manifests', *class_name_parts) + '.pp'
40
34
 
41
- # drop the module name if the object name contains multiple parts
42
- class_name_parts.delete_at(0) if class_name_parts.length > 1
35
+ files
36
+ end
43
37
 
44
- "#{File.join(module_dir, 'spec', 'classes', *class_name_parts)}_spec.rb"
45
- end
38
+ def template_data
39
+ { name: object_name }
46
40
  end
47
41
  end
48
42
  end
@@ -3,7 +3,7 @@ require 'pdk'
3
3
  module PDK
4
4
  module Generate
5
5
  class PuppetObject
6
- attr_reader :module_dir
6
+ attr_reader :context
7
7
  attr_reader :object_name
8
8
  attr_reader :options
9
9
 
@@ -16,232 +16,128 @@ module PDK
16
16
  # OBJECT_TYPE constant and implement the {#template_data},
17
17
  # {#target_object_path} and {#target_spec_path} methods.
18
18
  #
19
- # @param module_dir [String] The path to the module directory that the
19
+ # @param module_dir [String] The path to the root of module that the
20
20
  # will contain the object.
21
21
  # @param object_name [String] The name of the object.
22
22
  # @param options [Hash{Symbol => Object}]
23
- #
24
- # @api public
25
- def initialize(module_dir, object_name, options)
26
- @module_dir = module_dir
23
+ def initialize(context, object_name, options)
24
+ raise ArgumentError, _('Expected PDK::Context::AbstractContext but got \'%{klass}\' for context') % { klass: context.class } unless context.is_a?(PDK::Context::AbstractContext)
25
+ @context = context
27
26
  @options = options
28
27
  @object_name = object_name
29
-
30
- if [:class, :defined_type].include?(object_type) # rubocop:disable Style/GuardClause
31
- object_name_parts = object_name.split('::')
32
-
33
- @object_name = if object_name_parts.first == module_name
34
- object_name
35
- else
36
- [module_name, object_name].join('::')
37
- end
38
- end
39
28
  end
40
29
 
30
+ # Whether the generator should only return test (spec) files
31
+ # @return [Boolean]
41
32
  def spec_only?
42
33
  @options[:spec_only]
43
34
  end
44
35
 
45
- # @abstract Subclass and implement {#template_data} to provide data to
46
- # the templates during rendering. Implementations of this method should
47
- # return a Hash!{Symbol => Object}.
48
- def template_data
36
+ # Subclass and implement {#friendly_name} to provide a nice name to show users in CLI
37
+ # @abstract
38
+ # @return String
39
+ def friendly_name
49
40
  raise NotImplementedError
50
41
  end
51
42
 
52
- # @abstract Subclass and implement {#target_object_path}. Implementations
53
- # of this method should return a String containing the destination path
54
- # of the object being generated.
55
- def target_object_path
43
+ # Subclass and implement {#template_files} to provide the template files to
44
+ # render. Implementations of this method should return a Hash!{String => String}.
45
+ # @abstract
46
+ # @return Hash{String => String} Hash key is the source template file and the Hash value is
47
+ # the relative destination path
48
+ def template_files
56
49
  raise NotImplementedError
57
50
  end
58
51
 
59
- # @abstract Subclass and implement {#target_type_path}. Implementations
60
- # of this method should return a String containing the destination path
61
- # of the additional object file being generated.
62
- # @return [String] returns nil if there is no additional object file
63
- def target_type_path
64
- nil
65
- end
66
-
67
- # @abstract Subclass and implement {#target_spec_path}. Implementations
68
- # of this method should return a String containing the destination path
69
- # of the tests for the object being generated.
70
- def target_spec_path
52
+ # Subclass and implement {#template_data} to provide data to the templates during rendering.
53
+ # @abstract
54
+ # @return Hash{Symbol => Object}
55
+ def template_data
71
56
  raise NotImplementedError
72
57
  end
73
58
 
74
- # @abstract Subclass and implement {#target_type_spec_path}. Implementations
75
- # of this method should return a String containing the destination path
76
- # of the tests for the object being generated.
77
- def target_type_spec_path
78
- nil
79
- end
80
-
81
- # @abstract Subclass and implement {#target_device_path}. Implementations
82
- # of this method should return a String containing the destination path
83
- # of the device class being generated.
84
- def target_device_path
85
- nil
86
- end
87
-
88
- # Retrieves the type of the object being generated, e.g. :class,
89
- # :defined_type, etc. This is specified in the subclass' OBJECT_TYPE
90
- # constant.
59
+ # Raises an error if any pre-conditions are not met
91
60
  #
92
- # @return [Symbol] the type of the object being generated.
93
- #
94
- # @api private
95
- def object_type
96
- self.class::OBJECT_TYPE
61
+ # @return [void]
62
+ # @abstract
63
+ def check_preconditions
64
+ raise ArgumentError, _('Expected a module context but got %{context_name}') % { context_name: context.display_name } unless context.is_a?(PDK::Context::Module)
97
65
  end
98
66
 
99
- # Retrieves the type of the object being generated as represented in
100
- # the JSON output of puppet-strings.
67
+ # Check the preconditions of this template group, behaving as a predicate rather than raising an exception.
101
68
  #
102
- # @return [String] the type of the object being generated or nil if
103
- # there is no mapping.
104
- #
105
- # @api private
106
- def self.puppet_strings_type
107
- return nil unless const_defined?(:PUPPET_STRINGS_TYPE)
108
-
109
- self::PUPPET_STRINGS_TYPE
69
+ # @return [Boolean] true if the generator is safe to run, otherwise false.
70
+ def can_run?
71
+ check_preconditions
72
+ true
73
+ rescue StandardError
74
+ false
110
75
  end
111
76
 
112
- # Returns an array of possible target path strings.
113
- def targets
114
- targets = [
115
- target_spec_path,
116
- target_type_spec_path,
117
- ]
118
-
119
- unless spec_only?
120
- targets += [
121
- target_object_path,
122
- target_type_path,
123
- target_device_path,
124
- ]
125
- end
126
-
127
- targets.compact
77
+ # Creates an instance of an update manager
78
+ # @api private
79
+ def update_manager_instance
80
+ require 'pdk/module/update_manager'
81
+ PDK::Module::UpdateManager.new
128
82
  end
129
83
 
130
- # Check preconditions of this template group. By default this only makes sure that the target files do not
131
- # already exist. Override this (and call super) to add your own preconditions.
132
- #
133
- # @raise [PDK::CLI::ExitWithError] if the target files already exist.
84
+ # Stages and then executes the changes for the templates to be rendereed.
85
+ # This is the main entry point for the class.
134
86
  #
87
+ # @see #stage_changes
88
+ # @return [PDK::Module::UpdateManager] The update manager which implemented the changes
135
89
  # @api public
136
- def check_preconditions
137
- require 'pdk/util/filesystem'
138
-
139
- targets.each do |target_file|
140
- next unless PDK::Util::Filesystem.exist?(target_file)
141
-
142
- raise PDK::CLI::ExitWithError, _("Unable to generate %{object_type}; '%{file}' already exists.") % {
143
- file: target_file,
144
- object_type: spec_only? ? 'unit test' : object_type,
145
- }
146
- end
147
- end
148
-
149
- # Check the preconditions of this template group, behaving as a
150
- # predicate rather than raising an exception.
151
- #
152
- # @return [Boolean] true if the generator is safe to run, otherwise
153
- # false.
154
- def can_run?
155
- check_preconditions
156
- true
157
- rescue PDK::CLI::ExitWithError
158
- false
90
+ def run(update_manager = update_manager_instance)
91
+ stage_changes(update_manager).sync_changes!
92
+ update_manager
159
93
  end
160
94
 
161
95
  # Check that the templates can be rendered. Find an appropriate template
162
- # and create the target files from the template. This is the main entry
96
+ # and stages the target files from the template. This is the main entry
163
97
  # point for the class.
164
98
  #
165
99
  # @raise [PDK::CLI::ExitWithError] if the target files already exist.
166
100
  # @raise [PDK::CLI::FatalError] (see #render_file)
167
- #
101
+ # @return [PDK::Module::UpdateManager] The update manager with the staged changes
168
102
  # @api public
169
- def run
103
+ def stage_changes(update_manager)
170
104
  check_preconditions
171
105
 
172
- with_templates do |template_path, config_hash|
173
- data = template_data.merge(configs: config_hash)
106
+ with_templates do |template_dir|
107
+ template_files.each do |source_file, relative_dest_path|
108
+ new_content = template_dir.render_single_item(source_file, template_data)
109
+ next if new_content.nil?
174
110
 
175
- render_file(target_object_path, template_path[:object], data) unless spec_only?
176
- render_file(target_type_path, template_path[:type], data) if template_path[:type]
177
- render_file(target_device_path, template_path[:device], data) if template_path[:device]
178
- render_file(target_spec_path, template_path[:spec], data) if template_path[:spec]
179
- render_file(target_type_spec_path, template_path[:type_spec], data) if template_path[:type_spec]
111
+ stage_change(relative_dest_path, new_content, update_manager)
112
+ end
180
113
  end
114
+ non_template_files.each { |relative_dest_path, content| stage_change(relative_dest_path, content, update_manager) }
115
+
116
+ update_manager
181
117
  end
182
118
 
183
- # Render a file using the provided template and write it to disk.
184
- #
185
- # @param dest_path [String] The path that the rendered file should be
186
- # written to. Any necessary directories will be automatically created.
187
- # @param template_path [String] The path on disk to the file containing
188
- # the template.
189
- # @param data [Hash{Object => Object}] The data to be provided to the
190
- # template when rendering.
191
- #
192
- # @raise [PDK::CLI::FatalError] if the parent directories to `dest_path`
193
- # do not exist and could not be created.
194
- # @raise [PDK::CLI::FatalError] if the rendered file could not be written
195
- # to `dest_path`.
196
- #
119
+ # Stages a single file into the Update Manager.
197
120
  # @return [void]
198
- #
199
121
  # @api private
200
- def render_file(dest_path, template_path, data)
201
- require 'pdk/template_file'
202
-
203
- write_file(dest_path) do
204
- PDK::TemplateFile.new(template_path, data).render
122
+ def stage_change(relative_dest_path, content, update_manager)
123
+ absolute_file_path = File.join(context.root_path, relative_dest_path)
124
+ if PDK::Util::Filesystem.exist?(absolute_file_path)
125
+ raise PDK::CLI::ExitWithError, _("Unable to generate %{object_type}; '%{file}' already exists.") % {
126
+ file: absolute_file_path,
127
+ object_type: spec_only? ? 'unit test' : friendly_name,
128
+ }
205
129
  end
130
+ update_manager.add_file(absolute_file_path, content)
206
131
  end
207
132
 
208
- # Write the result of the block to disk.
209
- #
210
- # @param dest_path [String] The path that the rendered file should be
211
- # written to. Any necessary directories will be automatically created.
212
- # @param &block [String] The content to be written
213
- #
214
- # @raise [PDK::CLI::FatalError] if the parent directories to `dest_path`
215
- # do not exist and could not be created.
216
- # @raise [PDK::CLI::FatalError] if the rendered file could not be written
217
- # to `dest_path`.
218
- #
219
- # @return [void]
133
+ # A subclass may wish to stage files into the Update Manager, but the content is not templated. Subclasses
134
+ # can override this method to stage arbitrary files
220
135
  #
221
136
  # @api private
222
- def write_file(dest_path)
223
- require 'pdk/logger'
224
- require 'pdk/util/filesystem'
225
-
226
- PDK.logger.info(_("Creating '%{file}' from template.") % { file: dest_path })
227
-
228
- file_content = yield
229
-
230
- begin
231
- PDK::Util::Filesystem.mkdir_p(File.dirname(dest_path))
232
- rescue SystemCallError => e
233
- raise PDK::CLI::FatalError, _("Unable to create directory '%{path}': %{message}") % {
234
- path: File.dirname(dest_path),
235
- message: e.message,
236
- }
237
- end
238
-
239
- PDK::Util::Filesystem.write_file(dest_path, file_content)
240
- rescue SystemCallError => e
241
- raise PDK::CLI::FatalError, _("Unable to write to file '%{path}': %{message}") % {
242
- path: dest_path,
243
- message: e.message,
244
- }
137
+ # @return [Hash{String => String}] A Hash with the relative file path as the key and the new file content as the value.
138
+ # @abstract
139
+ def non_template_files
140
+ {}
245
141
  end
246
142
 
247
143
  # Search the possible template directories in order of preference to find
@@ -268,18 +164,15 @@ module PDK
268
164
  next
269
165
  end
270
166
 
271
- PDK::Module::TemplateDir.with(PDK::Util::TemplateURI.new(template[:uri])) do |template_dir|
272
- template_paths = template_dir.object_template_for(object_type)
273
-
274
- if template_paths
275
- config_hash = template_dir.object_config
276
- yield template_paths, config_hash
167
+ PDK::Template.with(PDK::Util::TemplateURI.new(template[:uri]), context) do |template_dir|
168
+ if template_files.any? { |source_file, _| template_dir.has_single_item?(source_file) }
169
+ yield template_dir
277
170
  # TODO: refactor to a search-and-execute form instead
278
171
  return # work is done # rubocop:disable Lint/NonLocalExitFromIterator
279
172
  elsif template[:allow_fallback]
280
- PDK.logger.debug(_('Unable to find a %{type} template in %{url}; trying next template directory.') % { type: object_type, url: template[:uri] })
173
+ PDK.logger.debug(_('Unable to find a %{type} template in %{url}; trying next template directory.') % { type: friendly_name, url: template[:uri] })
281
174
  else
282
- raise PDK::CLI::FatalError, _('Unable to find the %{type} template in %{url}.') % { type: object_type, url: template[:uri] }
175
+ raise PDK::CLI::FatalError, _('Unable to find the %{type} template in %{url}.') % { type: friendly_name, url: template[:uri] }
283
176
  end
284
177
  end
285
178
  end
@@ -320,15 +213,18 @@ module PDK
320
213
  #
321
214
  # @api private
322
215
  def module_name
323
- require 'pdk/util'
216
+ return nil unless context.is_a?(PDK::Context::Module)
324
217
 
325
- @module_name ||= PDK::Util.module_metadata['name'].rpartition('-').last
218
+ require 'pdk/util'
219
+ @module_name ||= PDK::Util.module_metadata(context.root_path)['name'].rpartition('-').last
326
220
  rescue ArgumentError => e
327
221
  raise PDK::CLI::FatalError, e
328
222
  end
329
223
 
330
- # transform a object name into a ruby class name
331
- def self.class_name_from_object_name(object_name)
224
+ private
225
+
226
+ # Transform an object name into a ruby class name
227
+ def class_name_from_object_name(object_name)
332
228
  object_name.to_s.split('_').map(&:capitalize).join
333
229
  end
334
230
  end
@@ -3,44 +3,21 @@ require 'pdk'
3
3
  module PDK
4
4
  module Generate
5
5
  class Task < PuppetObject
6
- OBJECT_TYPE = :task
6
+ def friendly_name
7
+ 'Task'
8
+ end
7
9
 
8
- # Prepares the data needed to render the new task template.
9
- #
10
- # @return [Hash{Symbol => Object}] a hash of information that will be
11
- # provided to the task template during rendering. Additionally, this hash
12
- # (with the :name key removed) makes up the task metadata.
13
- def template_data
10
+ def template_files
11
+ return {} if spec_only?
14
12
  {
15
- name: object_name,
16
- puppet_task_version: 1,
17
- supports_noop: false,
18
- description: options.fetch(:description, 'A short description of this task'),
19
- parameters: {},
13
+ 'task.erb' => File.join('tasks', task_name + '.sh'),
20
14
  }
21
15
  end
22
16
 
23
- # Calculates the path to the file where the new task will be written.
24
- #
25
- # @return [String] the path to the task file.
26
- def target_object_path
27
- @target_object_path ||= File.join(module_dir, 'tasks', "#{task_name}.sh")
28
- end
29
-
30
- # Calculates the path to the file where the tests for the new task will
31
- # be written.
32
- #
33
- # @return [nil] as there is currently no test framework for Tasks.
34
- def target_spec_path
35
- nil
36
- end
37
-
38
- def run
39
- check_if_task_already_exists
40
-
41
- super
42
-
43
- write_task_metadata
17
+ def template_data
18
+ {
19
+ name: object_name,
20
+ }
44
21
  end
45
22
 
46
23
  # Checks that the task has not already been defined with a different
@@ -48,30 +25,26 @@ module PDK
48
25
  #
49
26
  # @raise [PDK::CLI::ExitWithError] if files with the same name as the
50
27
  # task exist in the <module>/tasks/ directory
51
- #
52
- # @api private
53
- def check_if_task_already_exists
28
+ def check_preconditions
29
+ super
30
+
54
31
  error = _("A task named '%{name}' already exists in this module; defined in %{file}")
55
32
  allowed_extensions = %w[.md .conf]
56
33
 
57
- PDK::Util::Filesystem.glob(File.join(module_dir, 'tasks', "#{task_name}.*")).each do |file|
34
+ PDK::Util::Filesystem.glob(File.join(context.root_path, 'tasks', task_name + '.*')).each do |file|
58
35
  next if allowed_extensions.include?(File.extname(file))
59
36
 
60
37
  raise PDK::CLI::ExitWithError, error % { name: task_name, file: file }
61
38
  end
62
39
  end
63
40
 
64
- # Writes the <module>/tasks/<task_name>.json metadata file for the task.
65
- #
66
- # @api private
67
- def write_task_metadata
68
- write_file(File.join(module_dir, 'tasks', "#{task_name}.json")) do
69
- task_metadata = template_data.dup
70
- task_metadata.delete(:name)
71
- JSON.pretty_generate(task_metadata)
72
- end
41
+ def non_template_files
42
+ task_metadata_file = File.join('tasks', task_name + '.json')
43
+ { task_metadata_file => JSON.pretty_generate(task_metadata) }
73
44
  end
74
45
 
46
+ private
47
+
75
48
  # Calculates the file name of the task files ('init' if the task has the
76
49
  # same name as the module, otherwise use the specified task name).
77
50
  #
@@ -81,6 +54,15 @@ module PDK
81
54
  def task_name
82
55
  (object_name == module_name) ? 'init' : object_name
83
56
  end
57
+
58
+ def task_metadata
59
+ {
60
+ puppet_task_version: 1,
61
+ supports_noop: false,
62
+ description: options.fetch(:description, 'A short description of this task'),
63
+ parameters: {},
64
+ }
65
+ end
84
66
  end
85
67
  end
86
68
  end