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
@@ -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
@@ -0,0 +1,90 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module ControlRepo
5
+ CONTROL_REPO_FILES = %w[environment.conf Puppetfile].freeze
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
+
17
+ # Returns path to the root of the Control Repo being worked on.
18
+ #
19
+ # An environment.conf is required for a PDK compatible Control Repo,
20
+ # whereas Puppetfile is optional.
21
+ #
22
+ # Note - A Bolt Project can also be a Control Repo.
23
+ #
24
+ # Note - Non-Directory environments can exist however directory based
25
+ # environments are the supported/preferred way.
26
+ #
27
+ # @see https://puppet.com/docs/pe/latest/control_repo.html
28
+ #
29
+ # @param strict_check [Boolean] When strict_check is true, only return the path
30
+ # if the Control Repo is strictly _only_ a control repository. For example,
31
+ # not also a Puppet Bolt project directory Default is false.
32
+ #
33
+ # @return [String, nil] Fully qualified base path to Control Repo, or nil if
34
+ # the current working dir does not appear to be within a Control Repo.
35
+ def find_control_repo_root(strict_check = false)
36
+ environment_conf_path = PDK::Util.find_upwards('environment.conf')
37
+ path = if environment_conf_path
38
+ File.dirname(environment_conf_path)
39
+ elsif control_repo_root?(Dir.pwd)
40
+ Dir.pwd
41
+ else
42
+ nil
43
+ end
44
+ return path if path.nil? || !strict_check
45
+ PDK::Bolt.bolt_project_root?(path) ? nil : path
46
+ end
47
+ module_function :find_control_repo_root
48
+
49
+ # Returns true or false depending on if any of the common files in a Control Repo
50
+ # are found in the specified directory. If a directory is not specified, the current
51
+ # working directory is used.
52
+ #
53
+ # @return [boolean] True if any folders from CONTROL_REPO_FILES are found in the current dir,
54
+ # false otherwise.
55
+ def control_repo_root?(path = Dir.pwd)
56
+ CONTROL_REPO_FILES.any? { |file| PDK::Util::Filesystem.file?(File.join(path, file)) }
57
+ end
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
89
+ end
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
@@ -46,18 +46,30 @@ module PDK
46
46
 
47
47
  template_uri = PDK::Util::TemplateURI.new(opts)
48
48
 
49
+ if template_uri.default? && template_uri.default_ref?
50
+ PDK.logger.info _('Using the default template-url and template-ref.')
51
+ else
52
+ PDK.logger.info _(
53
+ "Using the %{method} template-url and template-ref '%{template_uri}'." % {
54
+ method: opts.key?(:'template-url') ? _('specified') : _('saved'),
55
+ template_uri: template_uri.metadata_format,
56
+ },
57
+ )
58
+ end
59
+
49
60
  begin
50
- PDK::Module::TemplateDir.with(template_uri, metadata.data, true) do |templates|
51
- templates.render do |file_path, file_content, file_status|
52
- next if file_status == :delete
53
- 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
54
66
  file.dirname.mkpath
55
67
  PDK::Util::Filesystem.write_file(file, file_content)
56
68
  end
57
69
 
58
70
  # Add information about the template used to generate the module to the
59
71
  # metadata (for a future update command).
60
- metadata.update!(templates.metadata)
72
+ metadata.update!(template_dir.metadata)
61
73
 
62
74
  metadata.write!(File.join(temp_target_dir, 'metadata.json'))
63
75
  end
@@ -67,15 +79,15 @@ module PDK
67
79
 
68
80
  # Only update the answers files after metadata has been written.
69
81
  require 'pdk/answer_file'
70
- if template_uri.default?
82
+ if template_uri.default? && template_uri.default_ref?
71
83
  # If the user specifies our default template url via the command
72
84
  # line, remove the saved template-url answer so that the template_uri
73
85
  # resolution can find new default URLs in the future.
74
- 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')
75
87
  else
76
88
  # Save the template-url answers if the module was generated using a
77
89
  # template/reference other than ours.
78
- PDK.answers.update!('template-url' => template_uri.metadata_format)
90
+ PDK.config.set(%w[user module_defaults template-url], template_uri.metadata_format)
79
91
  end
80
92
 
81
93
  begin
@@ -87,8 +99,13 @@ module PDK
87
99
  end
88
100
  end
89
101
 
90
- PDK.logger.info _('Module \'%{name}\' generated at path \'%{path}\', from template \'%{url}\'.') % { name: opts[:module_name], path: target_dir, url: template_uri.bare_uri }
91
- PDK.logger.info(_('In your module directory, add classes with the \'pdk new class\' command.'))
102
+ PDK.logger.info _("Module '%{name}' generated at path '%{path}'.") % {
103
+ name: opts[:module_name],
104
+ path: target_dir,
105
+ }
106
+ PDK.logger.info _(
107
+ "In your module directory, add classes with the 'pdk new class' command.",
108
+ )
92
109
  end
93
110
  rescue Errno::EACCES => e
94
111
  raise PDK::CLI::FatalError, _("Failed to move '%{source}' to '%{target}': %{message}") % {
@@ -119,13 +136,13 @@ module PDK
119
136
  require 'pdk/answer_file'
120
137
  require 'pdk/module/metadata'
121
138
 
122
- 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
123
140
 
124
141
  defaults = PDK::Module::Metadata::DEFAULTS.dup
125
142
 
126
143
  defaults['name'] = "#{opts[:username]}-#{opts[:module_name]}" unless opts[:module_name].nil?
127
- defaults['author'] = PDK.answers['author'] unless PDK.answers['author'].nil?
128
- 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 }
129
146
  defaults['license'] = opts[:license] if opts.key?(:license)
130
147
 
131
148
  metadata = PDK::Module::Metadata.new(defaults)
@@ -326,13 +343,9 @@ module PDK
326
343
  end
327
344
 
328
345
  require 'pdk/answer_file'
329
- PDK.answers.update!(
330
- {
331
- 'forge_username' => opts[:username],
332
- 'author' => answers['author'],
333
- 'license' => answers['license'],
334
- }.delete_if { |_key, value| value.nil? },
335
- )
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?
336
349
  end
337
350
  end
338
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