pdk 1.16.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +167 -11
  3. data/README.md +1 -1
  4. data/lib/pdk.rb +26 -19
  5. data/lib/pdk/answer_file.rb +2 -93
  6. data/lib/pdk/cli.rb +8 -6
  7. data/lib/pdk/cli/config.rb +3 -1
  8. data/lib/pdk/cli/config/get.rb +3 -1
  9. data/lib/pdk/cli/convert.rb +7 -9
  10. data/lib/pdk/cli/env.rb +52 -0
  11. data/lib/pdk/cli/exec/command.rb +13 -0
  12. data/lib/pdk/cli/exec_group.rb +78 -43
  13. data/lib/pdk/cli/get.rb +20 -0
  14. data/lib/pdk/cli/get/config.rb +24 -0
  15. data/lib/pdk/cli/new.rb +2 -0
  16. data/lib/pdk/cli/new/class.rb +2 -1
  17. data/lib/pdk/cli/new/defined_type.rb +2 -1
  18. data/lib/pdk/cli/new/fact.rb +29 -0
  19. data/lib/pdk/cli/new/function.rb +29 -0
  20. data/lib/pdk/cli/new/provider.rb +2 -1
  21. data/lib/pdk/cli/new/task.rb +2 -1
  22. data/lib/pdk/cli/new/test.rb +2 -1
  23. data/lib/pdk/cli/new/transport.rb +2 -1
  24. data/lib/pdk/cli/release/publish.rb +11 -1
  25. data/lib/pdk/cli/remove.rb +20 -0
  26. data/lib/pdk/cli/remove/config.rb +80 -0
  27. data/lib/pdk/cli/set.rb +20 -0
  28. data/lib/pdk/cli/set/config.rb +119 -0
  29. data/lib/pdk/cli/update.rb +6 -8
  30. data/lib/pdk/cli/util.rb +7 -3
  31. data/lib/pdk/cli/util/option_validator.rb +6 -0
  32. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  33. data/lib/pdk/cli/validate.rb +26 -44
  34. data/lib/pdk/config.rb +264 -7
  35. data/lib/pdk/config/ini_file.rb +183 -0
  36. data/lib/pdk/config/ini_file_setting.rb +39 -0
  37. data/lib/pdk/config/namespace.rb +25 -5
  38. data/lib/pdk/config/setting.rb +3 -2
  39. data/lib/pdk/context.rb +99 -0
  40. data/lib/pdk/context/control_repo.rb +60 -0
  41. data/lib/pdk/context/module.rb +28 -0
  42. data/lib/pdk/context/none.rb +22 -0
  43. data/lib/pdk/control_repo.rb +40 -0
  44. data/lib/pdk/generate/defined_type.rb +25 -32
  45. data/lib/pdk/generate/fact.rb +25 -0
  46. data/lib/pdk/generate/function.rb +48 -0
  47. data/lib/pdk/generate/module.rb +14 -17
  48. data/lib/pdk/generate/provider.rb +15 -64
  49. data/lib/pdk/generate/puppet_class.rb +25 -31
  50. data/lib/pdk/generate/puppet_object.rb +83 -187
  51. data/lib/pdk/generate/task.rb +28 -46
  52. data/lib/pdk/generate/transport.rb +20 -74
  53. data/lib/pdk/module.rb +1 -1
  54. data/lib/pdk/module/convert.rb +43 -23
  55. data/lib/pdk/module/metadata.rb +6 -2
  56. data/lib/pdk/module/release.rb +3 -9
  57. data/lib/pdk/module/update.rb +7 -11
  58. data/lib/pdk/module/update_manager.rb +7 -0
  59. data/lib/pdk/report.rb +3 -3
  60. data/lib/pdk/report/event.rb +8 -2
  61. data/lib/pdk/template.rb +59 -0
  62. data/lib/pdk/template/fetcher.rb +98 -0
  63. data/lib/pdk/template/fetcher/git.rb +85 -0
  64. data/lib/pdk/template/fetcher/local.rb +28 -0
  65. data/lib/pdk/template/renderer.rb +96 -0
  66. data/lib/pdk/template/renderer/v1.rb +25 -0
  67. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  68. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  69. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  70. data/lib/pdk/template/template_dir.rb +67 -0
  71. data/lib/pdk/tests/unit.rb +8 -1
  72. data/lib/pdk/util.rb +38 -39
  73. data/lib/pdk/util/bundler.rb +2 -1
  74. data/lib/pdk/util/changelog_generator.rb +11 -2
  75. data/lib/pdk/util/json_finder.rb +84 -0
  76. data/lib/pdk/util/puppet_strings.rb +3 -3
  77. data/lib/pdk/util/puppet_version.rb +2 -2
  78. data/lib/pdk/util/ruby_version.rb +5 -1
  79. data/lib/pdk/util/template_uri.rb +13 -14
  80. data/lib/pdk/util/vendored_file.rb +1 -2
  81. data/lib/pdk/validate.rb +79 -25
  82. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  83. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  84. data/lib/pdk/validate/external_command_validator.rb +208 -0
  85. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  86. data/lib/pdk/validate/invokable_validator.rb +220 -0
  87. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  88. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  89. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  90. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  91. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  92. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  93. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  94. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  95. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  96. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  97. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  98. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  99. data/lib/pdk/validate/validator.rb +118 -0
  100. data/lib/pdk/validate/validator_group.rb +104 -0
  101. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  102. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  103. data/lib/pdk/version.rb +1 -1
  104. data/locales/pdk.pot +477 -313
  105. metadata +77 -35
  106. data/lib/pdk/module/template_dir.rb +0 -115
  107. data/lib/pdk/module/template_dir/base.rb +0 -268
  108. data/lib/pdk/module/template_dir/git.rb +0 -91
  109. data/lib/pdk/module/template_dir/local.rb +0 -21
  110. data/lib/pdk/template_file.rb +0 -96
  111. data/lib/pdk/validate/base_validator.rb +0 -215
  112. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  113. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  114. data/lib/pdk/validate/metadata_validator.rb +0 -26
  115. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -135
  116. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  117. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -135
  118. data/lib/pdk/validate/puppet_validator.rb +0 -26
  119. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  120. data/lib/pdk/validate/ruby_validator.rb +0 -26
  121. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  122. data/lib/pdk/validate/tasks/name.rb +0 -90
  123. data/lib/pdk/validate/tasks_validator.rb +0 -29
  124. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  125. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -0,0 +1,28 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Context
5
+ # Represents a context for a Puppet Module
6
+ class Module < PDK::Context::AbstractContext
7
+ # @param module_root [String] The root path for the module.
8
+ # @param context_path [String] The path where this context was created from e.g. Dir.pwd
9
+ # @see PDK::Context::AbstractContext
10
+ def initialize(module_root, context_path)
11
+ super(context_path)
12
+ @root_path = module_root
13
+ end
14
+
15
+ # @see PDK::Context::AbstractContext.pdk_compatible?
16
+ def pdk_compatible?
17
+ PDK::Util.module_pdk_compatible?(root_path)
18
+ end
19
+
20
+ #:nocov:
21
+ # @see PDK::Context::AbstractContext.display_name
22
+ def display_name
23
+ _('a Puppet Module context')
24
+ end
25
+ #:nocov:
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Context
5
+ # Represents a context which the PDK does not know. For example
6
+ # an empty directory
7
+ class None < PDK::Context::AbstractContext
8
+ #:nocov:
9
+ # @see PDK::Context::AbstractContext.display_name
10
+ def display_name
11
+ _('an unknown context')
12
+ end
13
+ #:nocov:
14
+
15
+ # @see PDK::Context::AbstractContext.parent_context
16
+ def parent_context
17
+ # An unknown context has no parent
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
@@ -4,6 +4,16 @@ module PDK
4
4
  module ControlRepo
5
5
  CONTROL_REPO_FILES = %w[environment.conf Puppetfile].freeze
6
6
 
7
+ DEFAULT_IGNORED = [
8
+ '/pkg/',
9
+ '~*',
10
+ '/coverage',
11
+ # Strictly speaking this isn't default but if people have tricked older PDK into thinking that a
12
+ # Control Repo is a module, they may have recursive symlinks in spec/fixtures/modules
13
+ '/spec/fixtures/modules/',
14
+ '/vendor/',
15
+ ].freeze
16
+
7
17
  # Returns path to the root of the Control Repo being worked on.
8
18
  #
9
19
  # An environment.conf is required for a PDK compatible Control Repo,
@@ -46,5 +56,35 @@ module PDK
46
56
  CONTROL_REPO_FILES.any? { |file| PDK::Util::Filesystem.file?(File.join(path, file)) }
47
57
  end
48
58
  module_function :control_repo_root?
59
+
60
+ # Returns a PDK::Config::Namespace for the specified environment.conf file.
61
+ # Note there is no validation of the path.
62
+ #
63
+ # @param path [String] The path to the environment.conf file
64
+ #
65
+ # @return [PDK::Config::IniFile] The configuration file
66
+ def environment_conf_as_config(path)
67
+ PDK::Config::IniFile.new('environment', file: path) do
68
+ setting :modulepath do
69
+ # As per https://puppet.com/docs/puppet/latest/config_file_environment.html#allowed-settings
70
+ default_to { 'modules:$basemodulepath' }
71
+ end
72
+
73
+ setting :manifest do
74
+ # As per https://puppet.com/docs/puppet/latest/config_file_environment.html#allowed-settings
75
+ default_to { 'manifests/' }
76
+ end
77
+ end
78
+ end
79
+ module_function :environment_conf_as_config
80
+
81
+ def default_ignored_pathspec(ignore_dotfiles = true)
82
+ require 'pathspec'
83
+
84
+ PathSpec.new(DEFAULT_IGNORED).tap do |ps|
85
+ ps.add('.*') if ignore_dotfiles
86
+ end
87
+ end
88
+ module_function :default_ignored_pathspec
49
89
  end
50
90
  end
@@ -3,47 +3,40 @@ require 'pdk'
3
3
  module PDK
4
4
  module Generate
5
5
  class DefinedType < PuppetObject
6
- OBJECT_TYPE = :defined_type
7
6
  PUPPET_STRINGS_TYPE = 'defined_types'.freeze
8
7
 
9
- # Prepares the data needed to render the new defined type template.
10
- #
11
- # @return [Hash{Symbol => Object}] a hash of information that will be
12
- # provided to the defined type and defined type spec templates during
13
- # rendering.
14
- def template_data
15
- data = { name: object_name }
8
+ def initialize(*_args)
9
+ super
10
+ object_name_parts = @object_name.split('::')
16
11
 
17
- data
12
+ @object_name = if object_name_parts.first == module_name
13
+ object_name
14
+ else
15
+ [module_name, object_name].join('::')
16
+ end
18
17
  end
19
18
 
20
- # Calculates the path to the .pp file that the new defined type will be
21
- # written to.
22
- #
23
- # @return [String] the path where the new defined type will be written.
24
- def target_object_path
25
- @target_pp_path ||= begin
26
- define_name_parts = object_name.split('::')[1..-1]
27
- define_name_parts << 'init' if define_name_parts.empty?
28
-
29
- "#{File.join(module_dir, 'manifests', *define_name_parts)}.pp"
30
- end
19
+ def friendly_name
20
+ 'Defined Type'.freeze
31
21
  end
32
22
 
33
- # Calculates the path to the file where the tests for the new defined
34
- # type will be written.
35
- #
36
- # @return [String] the path where the tests for the new defined type
37
- # will be written.
38
- def target_spec_path
39
- @target_spec_path ||= begin
40
- define_name_parts = object_name.split('::')
23
+ def template_files
24
+ # Calculate the defined type tests name
25
+ define_name_parts = object_name.split('::')
26
+ # drop the module name if the object name contains multiple parts
27
+ define_name_parts.delete_at(0) if define_name_parts.length > 1
28
+ files = { 'defined_type_spec.erb' => File.join('spec', 'defines', *define_name_parts) + '_spec.rb' }
29
+ return files if spec_only?
30
+
31
+ define_name_parts = object_name.split('::')[1..-1]
32
+ define_name_parts << 'init' if define_name_parts.empty?
33
+ files['defined_type.erb'] = File.join('manifests', *define_name_parts) + '.pp'
41
34
 
42
- # drop the module name if the object name contains multiple parts
43
- define_name_parts.delete_at(0) if define_name_parts.length > 1
35
+ files
36
+ end
44
37
 
45
- "#{File.join(module_dir, 'spec', 'defines', *define_name_parts)}_spec.rb"
46
- end
38
+ def template_data
39
+ { name: object_name }
47
40
  end
48
41
  end
49
42
  end
@@ -0,0 +1,25 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Generate
5
+ class Fact < PuppetObject
6
+ def friendly_name
7
+ 'Custom Fact'.freeze
8
+ end
9
+
10
+ def template_files
11
+ files = {
12
+ 'fact_spec.erb' => File.join('spec', 'unit', 'facter', object_name) + '_spec.rb',
13
+ }
14
+ return files if spec_only?
15
+ files.merge(
16
+ 'fact.erb' => File.join('lib', 'facter', object_name) + '.rb',
17
+ )
18
+ end
19
+
20
+ def template_data
21
+ { name: object_name }
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Generate
5
+ class Function < PuppetObject
6
+ def initialize(*_args)
7
+ super
8
+ object_name_parts = @object_name.split('::')
9
+
10
+ @object_name = if object_name_parts.first == module_name
11
+ object_name
12
+ else
13
+ [module_name, object_name].join('::')
14
+ end
15
+ end
16
+
17
+ def friendly_name
18
+ 'Function'.freeze
19
+ end
20
+
21
+ def template_files
22
+ # Calculate the function tests name
23
+ func_name_parts = object_name.split('::')
24
+ # Drop the module name if the object name contains multiple parts
25
+ func_name_parts.delete_at(0) if func_name_parts.length > 1
26
+ files = {
27
+ File.join('functions', 'function_spec.erb') => File.join('spec', 'functions', *func_name_parts) + '_spec.rb',
28
+ }
29
+ return files if spec_only?
30
+ func_name_parts = object_name.split('::')[1..-1]
31
+ template_file = File.join('functions', "#{options[:type]}_function.erb")
32
+
33
+ files[template_file] = if options[:type].eql?('v4')
34
+ File.join('lib', 'puppet', 'functions', module_name, *func_name_parts) + '.rb'
35
+ else
36
+ File.join('functions', *func_name_parts) + '.pp'
37
+ end
38
+ files
39
+ end
40
+
41
+ def template_data
42
+ func_name = object_name.split('::').last
43
+ namespace = object_name.split('::')[0...-1].join('::')
44
+ { name: object_name, func_name: func_name, namespace: namespace }
45
+ end
46
+ end
47
+ end
48
+ end
@@ -58,17 +58,18 @@ module PDK
58
58
  end
59
59
 
60
60
  begin
61
- PDK::Module::TemplateDir.with(template_uri, metadata.data, true) do |templates|
62
- templates.render do |file_path, file_content, file_status|
63
- next if file_status == :delete
64
- file = Pathname.new(temp_target_dir) + file_path
61
+ context = PDK::Context::None.new(temp_target_dir)
62
+ PDK::Template.with(template_uri, context) do |template_dir|
63
+ template_dir.render_new_module(metadata.data['name'], metadata.data) do |relative_file_path, file_content, file_status|
64
+ next if [:delete, :unmanage].include?(file_status)
65
+ file = Pathname.new(temp_target_dir) + relative_file_path
65
66
  file.dirname.mkpath
66
67
  PDK::Util::Filesystem.write_file(file, file_content)
67
68
  end
68
69
 
69
70
  # Add information about the template used to generate the module to the
70
71
  # metadata (for a future update command).
71
- metadata.update!(templates.metadata)
72
+ metadata.update!(template_dir.metadata)
72
73
 
73
74
  metadata.write!(File.join(temp_target_dir, 'metadata.json'))
74
75
  end
@@ -82,11 +83,11 @@ module PDK
82
83
  # If the user specifies our default template url via the command
83
84
  # line, remove the saved template-url answer so that the template_uri
84
85
  # resolution can find new default URLs in the future.
85
- PDK.answers.update!('template-url' => nil) if opts.key?(:'template-url')
86
+ PDK.config.set(%w[user module_defaults template-url], nil) if opts.key?(:'template-url')
86
87
  else
87
88
  # Save the template-url answers if the module was generated using a
88
89
  # template/reference other than ours.
89
- PDK.answers.update!('template-url' => template_uri.metadata_format)
90
+ PDK.config.set(%w[user module_defaults template-url], template_uri.metadata_format)
90
91
  end
91
92
 
92
93
  begin
@@ -135,13 +136,13 @@ module PDK
135
136
  require 'pdk/answer_file'
136
137
  require 'pdk/module/metadata'
137
138
 
138
- opts[:username] = (opts[:username] || PDK.answers['forge_username'] || username_from_login).downcase
139
+ opts[:username] = (opts[:username] || PDK.config.get_within_scopes('module_defaults.forge_username') || username_from_login).downcase
139
140
 
140
141
  defaults = PDK::Module::Metadata::DEFAULTS.dup
141
142
 
142
143
  defaults['name'] = "#{opts[:username]}-#{opts[:module_name]}" unless opts[:module_name].nil?
143
- defaults['author'] = PDK.answers['author'] unless PDK.answers['author'].nil?
144
- defaults['license'] = PDK.answers['license'] unless PDK.answers['license'].nil?
144
+ PDK.config.with_scoped_value('module_defaults.author') { |val| defaults['author'] = val }
145
+ PDK.config.with_scoped_value('module_defaults.license') { |val| defaults['license'] = val }
145
146
  defaults['license'] = opts[:license] if opts.key?(:license)
146
147
 
147
148
  metadata = PDK::Module::Metadata.new(defaults)
@@ -342,13 +343,9 @@ module PDK
342
343
  end
343
344
 
344
345
  require 'pdk/answer_file'
345
- PDK.answers.update!(
346
- {
347
- 'forge_username' => opts[:username],
348
- 'author' => answers['author'],
349
- 'license' => answers['license'],
350
- }.delete_if { |_key, value| value.nil? },
351
- )
346
+ PDK.config.set(%w[user module_defaults forge_username], opts[:username]) unless opts[:username].nil?
347
+ PDK.config.set(%w[user module_defaults author], answers['author']) unless answers['author'].nil?
348
+ PDK.config.set(%w[user module_defaults license], answers['license']) unless answers['license'].nil?
352
349
  end
353
350
  end
354
351
  end
@@ -3,74 +3,25 @@ require 'pdk'
3
3
  module PDK
4
4
  module Generate
5
5
  class Provider < PuppetObject
6
- OBJECT_TYPE = :provider
7
-
8
- # Prepares the data needed to render the new defined type template.
9
- #
10
- # @return [Hash{Symbol => Object}] a hash of information that will be
11
- # provided to the defined type and defined type spec templates during
12
- # rendering.
13
- def template_data
14
- data = {
15
- name: object_name,
16
- provider_class: Provider.class_name_from_object_name(object_name),
17
- }
18
-
19
- data
20
- end
21
-
22
- def raise_precondition_error(error)
23
- raise PDK::CLI::ExitWithError, _('%{error}: Creating a provider needs some local configuration in your module.' \
24
- ' Please follow the docs at https://puppet.com/docs/puppet/latest/create_types_and_providers_resource_api.html.') % { error: error }
25
- end
26
-
27
- def check_preconditions
28
- super
29
- # These preconditions can be removed once the pdk-templates are carrying the puppet-resource_api gem by default, and have switched
30
- # the default mock_with value.
31
- sync_path = PDK::Util.find_upwards('.sync.yml')
32
- if sync_path.nil?
33
- raise_precondition_error(_('.sync.yml not found'))
34
- end
35
- sync = YAML.load_file(sync_path)
36
- if !sync.is_a? Hash
37
- raise_precondition_error(_('.sync.yml contents is not a Hash'))
38
- elsif !sync.key? 'Gemfile'
39
- raise_precondition_error(_('Gemfile configuration not found'))
40
- elsif !sync['Gemfile'].key? 'optional'
41
- raise_precondition_error(_('Gemfile.optional configuration not found'))
42
- elsif !sync['Gemfile']['optional'].key? ':development'
43
- raise_precondition_error(_('Gemfile.optional.:development configuration not found'))
44
- elsif sync['Gemfile']['optional'][':development'].none? { |g| g['gem'] == 'puppet-resource_api' }
45
- raise_precondition_error(_('puppet-resource_api not found in the Gemfile config'))
46
- elsif !sync.key? 'spec/spec_helper.rb'
47
- raise_precondition_error(_('spec/spec_helper.rb configuration not found'))
48
- elsif !sync['spec/spec_helper.rb'].key? 'mock_with'
49
- raise_precondition_error(_('spec/spec_helper.rb.mock_with configuration not found'))
50
- elsif !sync['spec/spec_helper.rb']['mock_with'] == ':rspec'
51
- raise_precondition_error(_('spec/spec_helper.rb.mock_with not set to \':rspec\''))
52
- end
53
- end
54
-
55
- # @return [String] the path where the new provider will be written.
56
- def target_object_path
57
- @target_object_path ||= File.join(module_dir, 'lib', 'puppet', 'provider', object_name, object_name) + '.rb'
6
+ def friendly_name
7
+ 'Resource API Provider'.freeze
58
8
  end
59
9
 
60
- # @return [String] the path where the new type will be written.
61
- def target_type_path
62
- @target_type_path ||= File.join(module_dir, 'lib', 'puppet', 'type', object_name) + '.rb'
63
- end
64
-
65
- # @return [String] the path where the tests for the new provider
66
- # will be written.
67
- def target_spec_path
68
- @target_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'provider', object_name, object_name) + '_spec.rb'
10
+ def template_files
11
+ files = {
12
+ 'provider_spec.erb' => File.join('spec', 'unit', 'puppet', 'provider', object_name, object_name) + '_spec.rb',
13
+ 'provider_type_spec.erb' => File.join('spec', 'unit', 'puppet', 'type', object_name) + '_spec.rb',
14
+ }
15
+ return files if spec_only?
16
+ files.merge(
17
+ 'provider.erb' => File.join('lib', 'puppet', 'provider', object_name, object_name) + '.rb',
18
+ 'provider_type.erb' => File.join('lib', 'puppet', 'type', object_name) + '.rb',
19
+ )
69
20
  end
70
21
 
71
- # @return [String] the path where the tests for the new type will be written.
72
- def target_type_spec_path
73
- @target_type_spec_path ||= File.join(module_dir, 'spec', 'unit', 'puppet', 'type', object_name) + '_spec.rb'
22
+ def template_data
23
+ { name: object_name,
24
+ provider_class: class_name_from_object_name(object_name) }
74
25
  end
75
26
  end
76
27
  end
@@ -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