pdk 1.14.1 → 1.18.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (128) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +155 -2
  3. data/lib/pdk.rb +28 -19
  4. data/lib/pdk/answer_file.rb +2 -95
  5. data/lib/pdk/bolt.rb +19 -0
  6. data/lib/pdk/cli.rb +4 -5
  7. data/lib/pdk/cli/bundle.rb +5 -1
  8. data/lib/pdk/cli/config.rb +3 -1
  9. data/lib/pdk/cli/config/get.rb +3 -1
  10. data/lib/pdk/cli/console.rb +1 -1
  11. data/lib/pdk/cli/convert.rb +16 -10
  12. data/lib/pdk/cli/exec.rb +2 -1
  13. data/lib/pdk/cli/exec/command.rb +45 -4
  14. data/lib/pdk/cli/exec_group.rb +78 -43
  15. data/lib/pdk/cli/get.rb +20 -0
  16. data/lib/pdk/cli/get/config.rb +24 -0
  17. data/lib/pdk/cli/module/build.rb +1 -1
  18. data/lib/pdk/cli/module/generate.rb +1 -1
  19. data/lib/pdk/cli/new/class.rb +2 -2
  20. data/lib/pdk/cli/new/defined_type.rb +2 -2
  21. data/lib/pdk/cli/new/provider.rb +2 -2
  22. data/lib/pdk/cli/new/task.rb +2 -2
  23. data/lib/pdk/cli/new/test.rb +2 -2
  24. data/lib/pdk/cli/new/transport.rb +2 -2
  25. data/lib/pdk/cli/release.rb +192 -0
  26. data/lib/pdk/cli/release/prep.rb +39 -0
  27. data/lib/pdk/cli/release/publish.rb +40 -0
  28. data/lib/pdk/cli/remove.rb +20 -0
  29. data/lib/pdk/cli/remove/config.rb +80 -0
  30. data/lib/pdk/cli/set.rb +20 -0
  31. data/lib/pdk/cli/set/config.rb +119 -0
  32. data/lib/pdk/cli/update.rb +18 -8
  33. data/lib/pdk/cli/util.rb +7 -3
  34. data/lib/pdk/cli/util/update_manager_printer.rb +82 -0
  35. data/lib/pdk/cli/validate.rb +26 -44
  36. data/lib/pdk/config.rb +265 -8
  37. data/lib/pdk/config/ini_file.rb +183 -0
  38. data/lib/pdk/config/ini_file_setting.rb +39 -0
  39. data/lib/pdk/config/namespace.rb +26 -6
  40. data/lib/pdk/config/setting.rb +3 -2
  41. data/lib/pdk/context.rb +99 -0
  42. data/lib/pdk/context/control_repo.rb +60 -0
  43. data/lib/pdk/context/module.rb +28 -0
  44. data/lib/pdk/context/none.rb +22 -0
  45. data/lib/pdk/control_repo.rb +90 -0
  46. data/lib/pdk/generate.rb +1 -0
  47. data/lib/pdk/generate/defined_type.rb +25 -32
  48. data/lib/pdk/generate/module.rb +42 -35
  49. data/lib/pdk/generate/provider.rb +16 -65
  50. data/lib/pdk/generate/puppet_class.rb +25 -31
  51. data/lib/pdk/generate/puppet_object.rb +84 -189
  52. data/lib/pdk/generate/resource_api_object.rb +55 -0
  53. data/lib/pdk/generate/task.rb +28 -46
  54. data/lib/pdk/generate/transport.rb +21 -75
  55. data/lib/pdk/module.rb +1 -1
  56. data/lib/pdk/module/build.rb +38 -25
  57. data/lib/pdk/module/convert.rb +61 -42
  58. data/lib/pdk/module/metadata.rb +1 -3
  59. data/lib/pdk/module/release.rb +254 -0
  60. data/lib/pdk/module/update.rb +24 -16
  61. data/lib/pdk/module/update_manager.rb +8 -1
  62. data/lib/pdk/report.rb +18 -12
  63. data/lib/pdk/report/event.rb +6 -3
  64. data/lib/pdk/template.rb +59 -0
  65. data/lib/pdk/template/fetcher.rb +98 -0
  66. data/lib/pdk/template/fetcher/git.rb +85 -0
  67. data/lib/pdk/template/fetcher/local.rb +28 -0
  68. data/lib/pdk/template/renderer.rb +96 -0
  69. data/lib/pdk/template/renderer/v1.rb +25 -0
  70. data/lib/pdk/template/renderer/v1/legacy_template_dir.rb +116 -0
  71. data/lib/pdk/template/renderer/v1/renderer.rb +132 -0
  72. data/lib/pdk/template/renderer/v1/template_file.rb +102 -0
  73. data/lib/pdk/template/template_dir.rb +67 -0
  74. data/lib/pdk/tests/unit.rb +5 -0
  75. data/lib/pdk/util.rb +55 -45
  76. data/lib/pdk/util/bundler.rb +9 -9
  77. data/lib/pdk/util/changelog_generator.rb +120 -0
  78. data/lib/pdk/util/env.rb +28 -11
  79. data/lib/pdk/util/filesystem.rb +62 -2
  80. data/lib/pdk/util/git.rb +60 -8
  81. data/lib/pdk/util/json_finder.rb +84 -0
  82. data/lib/pdk/util/puppet_strings.rb +3 -3
  83. data/lib/pdk/util/puppet_version.rb +4 -5
  84. data/lib/pdk/util/ruby_version.rb +9 -6
  85. data/lib/pdk/util/template_uri.rb +60 -48
  86. data/lib/pdk/util/version.rb +4 -4
  87. data/lib/pdk/validate.rb +79 -25
  88. data/lib/pdk/validate/control_repo/control_repo_validator_group.rb +23 -0
  89. data/lib/pdk/validate/control_repo/environment_conf_validator.rb +98 -0
  90. data/lib/pdk/validate/external_command_validator.rb +208 -0
  91. data/lib/pdk/validate/internal_ruby_validator.rb +100 -0
  92. data/lib/pdk/validate/invokable_validator.rb +215 -0
  93. data/lib/pdk/validate/metadata/metadata_json_lint_validator.rb +86 -0
  94. data/lib/pdk/validate/metadata/metadata_syntax_validator.rb +78 -0
  95. data/lib/pdk/validate/metadata/metadata_validator_group.rb +20 -0
  96. data/lib/pdk/validate/puppet/puppet_epp_validator.rb +133 -0
  97. data/lib/pdk/validate/puppet/puppet_lint_validator.rb +66 -0
  98. data/lib/pdk/validate/puppet/puppet_syntax_validator.rb +137 -0
  99. data/lib/pdk/validate/puppet/puppet_validator_group.rb +21 -0
  100. data/lib/pdk/validate/ruby/ruby_rubocop_validator.rb +80 -0
  101. data/lib/pdk/validate/ruby/ruby_validator_group.rb +19 -0
  102. data/lib/pdk/validate/tasks/tasks_metadata_lint_validator.rb +88 -0
  103. data/lib/pdk/validate/tasks/tasks_name_validator.rb +50 -0
  104. data/lib/pdk/validate/tasks/tasks_validator_group.rb +20 -0
  105. data/lib/pdk/validate/validator.rb +118 -0
  106. data/lib/pdk/validate/validator_group.rb +104 -0
  107. data/lib/pdk/validate/yaml/yaml_syntax_validator.rb +95 -0
  108. data/lib/pdk/validate/yaml/yaml_validator_group.rb +19 -0
  109. data/lib/pdk/version.rb +1 -1
  110. data/locales/pdk.pot +755 -319
  111. metadata +66 -24
  112. data/lib/pdk/module/templatedir.rb +0 -391
  113. data/lib/pdk/template_file.rb +0 -96
  114. data/lib/pdk/validate/base_validator.rb +0 -215
  115. data/lib/pdk/validate/metadata/metadata_json_lint.rb +0 -82
  116. data/lib/pdk/validate/metadata/metadata_syntax.rb +0 -111
  117. data/lib/pdk/validate/metadata_validator.rb +0 -26
  118. data/lib/pdk/validate/puppet/puppet_epp.rb +0 -137
  119. data/lib/pdk/validate/puppet/puppet_lint.rb +0 -64
  120. data/lib/pdk/validate/puppet/puppet_syntax.rb +0 -137
  121. data/lib/pdk/validate/puppet_validator.rb +0 -26
  122. data/lib/pdk/validate/ruby/rubocop.rb +0 -72
  123. data/lib/pdk/validate/ruby_validator.rb +0 -26
  124. data/lib/pdk/validate/tasks/metadata_lint.rb +0 -130
  125. data/lib/pdk/validate/tasks/name.rb +0 -90
  126. data/lib/pdk/validate/tasks_validator.rb +0 -29
  127. data/lib/pdk/validate/yaml/syntax.rb +0 -125
  128. data/lib/pdk/validate/yaml_validator.rb +0 -28
@@ -0,0 +1,98 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ module ControlRepo
6
+ class EnvironmentConfValidator < InternalRubyValidator
7
+ ALLOWED_SETTINGS = %w[modulepath manifest config_version environment_timeout].freeze
8
+
9
+ def name
10
+ 'environment-conf'
11
+ end
12
+
13
+ def valid_in_context?
14
+ context.is_a?(PDK::Context::ControlRepo)
15
+ end
16
+
17
+ def pattern
18
+ ['environment.conf']
19
+ end
20
+
21
+ def spinner_text
22
+ _('Checking Puppet Environment settings (%{patterns}).') % {
23
+ patterns: pattern.join(' '),
24
+ }
25
+ end
26
+
27
+ def validate_target(report, target)
28
+ unless PDK::Util::Filesystem.readable?(target)
29
+ report.add_event(
30
+ file: target,
31
+ source: name,
32
+ state: :failure,
33
+ severity: 'error',
34
+ message: _('Could not be read.'),
35
+ )
36
+ return 1
37
+ end
38
+
39
+ is_valid = true
40
+ begin
41
+ env_conf = PDK::ControlRepo.environment_conf_as_config(target)
42
+
43
+ env_conf.resolve.each do |setting_name, setting_value|
44
+ # Remove the 'environment.' setting_name prefix
45
+ setting_name = setting_name.slice(12..-1)
46
+ next if ALLOWED_SETTINGS.include?(setting_name)
47
+ # A hash indicates that the ini file has a section in it.
48
+ message = if setting_value.is_a?(Hash)
49
+ _("Invalid section '%{name}'") % { name: setting_name }
50
+ else
51
+ _("Invalid setting '%{name}'") % { name: setting_name }
52
+ end
53
+
54
+ report.add_event(
55
+ file: target,
56
+ source: name,
57
+ state: :failure,
58
+ severity: 'error',
59
+ message: message,
60
+ )
61
+ is_valid = false
62
+ end
63
+
64
+ timeout = env_conf.fetch('environment_timeout', nil)
65
+ unless timeout.nil? || timeout == '0' || timeout == 'unlimited'
66
+ report.add_event(
67
+ file: target,
68
+ source: name,
69
+ state: :failure,
70
+ severity: 'error',
71
+ message: _("environment_timeout is set to '%{timeout}' but should be 0, 'unlimited' or not set.") % { timeout: timeout },
72
+ )
73
+ is_valid = false
74
+ end
75
+
76
+ return 1 unless is_valid
77
+ report.add_event(
78
+ file: target,
79
+ source: name,
80
+ state: :passed,
81
+ severity: 'ok',
82
+ )
83
+ return 0
84
+ rescue StandardError => e
85
+ report.add_event(
86
+ file: target,
87
+ source: name,
88
+ state: :failure,
89
+ severity: 'error',
90
+ message: e.message,
91
+ )
92
+ return 1
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,208 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ # An abstract validator that runs external commands within a Ruby Bundled environment
6
+ # e.g. `puppet-lint`, or `puppet validate`
7
+ #
8
+ # At a a minimum child classes should implment the `name`, `cmd`, `pattern` and `parse_output` methods
9
+ #
10
+ # An example concrete implementation could look like:
11
+ #
12
+ # module PDK
13
+ # module Validate
14
+ # module Ruby
15
+ # class RubyRubocopValidator < ExternalCommandValidator
16
+ # def name
17
+ # 'rubocop'
18
+ # end
19
+ #
20
+ # def cmd
21
+ # 'rubocop'
22
+ # end
23
+ #
24
+ # def pattern
25
+ # '**/**.rb'
26
+ # end
27
+ #
28
+ # def parse_options(targets)
29
+ # ['--format', 'json']
30
+ # end
31
+ #
32
+ # def parse_output(report, result, _targets)
33
+ # ... ruby code ...
34
+ # report.add_event(
35
+ # line: offense['location']['line'],
36
+ # column: offense['location']['column'],
37
+ # message: offense['message'],
38
+ # severity: offense['corrected'] ? 'corrected' : offense['severity'],
39
+ # test: offense['cop_name'],
40
+ # state: :failure,
41
+ # )
42
+ # end
43
+ # end
44
+ # end
45
+ # end
46
+ # end
47
+ #
48
+ # @see PDK::Validate::InvokableValidator
49
+ class ExternalCommandValidator < InvokableValidator
50
+ # @return Array[PDK::CLI::Exec::Command] This is a private implementation attribute used for unit testing
51
+ # @api private
52
+ attr_reader :commands
53
+
54
+ # @see PDK::Validate::Validator.spinner
55
+ def spinner
56
+ # The validator has sub-commands with their own spinners.
57
+ nil
58
+ end
59
+
60
+ # Calculates the text of the spinner based on the target list
61
+ # @return [String]
62
+ # @abstract
63
+ def spinner_text_for_targets(targets); end
64
+
65
+ # The name of the command to be run for validation
66
+ # @return [String]
67
+ # @abstract
68
+ def cmd; end
69
+
70
+ # Alternate paths which the command (cmd) may exist in. Typically other ruby gem caches,
71
+ # or packaged installation bin directories.
72
+ # @return [Array[String]]
73
+ # @api private
74
+ def alternate_bin_paths
75
+ [
76
+ PDK::Util::RubyVersion.bin_path,
77
+ File.join(PDK::Util::RubyVersion.gem_home, 'bin'),
78
+ PDK::Util::RubyVersion.gem_paths_raw.map { |gem_path_raw| File.join(gem_path_raw, 'bin') },
79
+ PDK::Util.package_install? ? File.join(PDK::Util.pdk_package_basedir, 'bin') : nil,
80
+ ].flatten.compact
81
+ end
82
+
83
+ # The full path to the command (cmd)
84
+ # Can be overridden in child classes to a non-default path
85
+ # @return [String]
86
+ # @api private
87
+ def cmd_path
88
+ return @cmd_path unless @cmd_path.nil?
89
+ @cmd_path = File.join(context.root_path, 'bin', cmd)
90
+ # Return the path to the command if it exists on disk, or we have a gemfile (i.e. Bundled install)
91
+ # The Bundle may be created after the prepare_invoke so if the file doesn't exist, it may not be an error
92
+ return @cmd_path if PDK::Util::Filesystem.exist?(@cmd_path) || !PDK::Util::Bundler::BundleHelper.new.gemfile.nil?
93
+ # But if there is no Gemfile AND cmd doesn't exist in the default path, we need to go searching...
94
+ @cmd_path = alternate_bin_paths.map { |alternate_path| File.join(alternate_path, cmd) }
95
+ .find { |path| PDK::Util::Filesystem.exist?(path) }
96
+ return @cmd_path unless @cmd_path.nil?
97
+ # If we can't find it anywhere, just let the OS find it
98
+ @cmd_path = cmd
99
+ end
100
+
101
+ # An array of command line arguments to pass to the command for validation
102
+ # @return Array[String]
103
+ # @abstract
104
+ def parse_options(_targets)
105
+ []
106
+ end
107
+
108
+ # Parses the output from the command and appends formatted events to the report.
109
+ # This is called for each command, which is a group of targets
110
+ #
111
+ # @param report [PDK::Report] The report to add events to
112
+ # @param result [Hash[Symbol => Object]] The result of validation command process
113
+ # @param targets [Array[String]] The targets for this command result
114
+ # @api private
115
+ # @see PDK::CLI::Exec::Command.execute!
116
+ # @abstract
117
+ def parse_output(_report, _result, _targets); end
118
+
119
+ # Prepares for invokation by parsing targets and creating the needed commands.
120
+ # @api private
121
+ # @see PDK::Validate::Validator.prepare_invoke!
122
+ def prepare_invoke!
123
+ return if @prepared
124
+ super
125
+
126
+ @targets, @skipped, @invalid = parse_targets
127
+ @targets = [] if @targets.nil?
128
+
129
+ target_groups = if @targets.empty? && allow_empty_targets?
130
+ # If we have no targets and we allow empty targets, create an empty target group list
131
+ [[]]
132
+ elsif invoke_style == :per_target
133
+ # If invoking :per_target, split the targets array into an array of
134
+ # single element arrays (one per target).
135
+ @targets.combination(1).to_a.compact
136
+ else
137
+ # Else we're invoking :once, wrap the targets array in another array. This is so we
138
+ # can loop through the invokes with the same logic, regardless of which invoke style
139
+ # is needed.
140
+ @targets.each_slice(1000).to_a.compact
141
+ end
142
+
143
+ # Register all of the commands for all of the targets
144
+ @commands = []
145
+ target_groups.each do |invokation_targets|
146
+ next if invokation_targets.empty? && !allow_empty_targets?
147
+ cmd_argv = parse_options(invokation_targets).unshift(cmd_path).compact
148
+ cmd_argv.unshift(File.join(PDK::Util::RubyVersion.bin_path, 'ruby.exe'), '-W0') if Gem.win_platform?
149
+
150
+ command = PDK::CLI::Exec::Command.new(*cmd_argv).tap do |c|
151
+ c.context = :module
152
+ c.environment = { 'PUPPET_GEM_VERSION' => options[:puppet] } if options[:puppet]
153
+
154
+ if spinners_enabled?
155
+ parent_validator = options[:parent_validator]
156
+ if parent_validator.nil? || parent_validator.spinner.nil? || !parent_validator.spinner.is_a?(TTY::Spinner::Multi)
157
+ c.add_spinner(spinner_text_for_targets(invokation_targets))
158
+ else
159
+ spinner = TTY::Spinner.new("[:spinner] #{spinner_text_for_targets(invokation_targets)}", PDK::CLI::Util.spinner_opts_for_platform)
160
+ parent_validator.spinner.register(spinner)
161
+ c.register_spinner(spinner, PDK::CLI::Util.spinner_opts_for_platform)
162
+ end
163
+ end
164
+ end
165
+
166
+ @commands << { command: command, invokation_targets: invokation_targets }
167
+ end
168
+ nil
169
+ end
170
+
171
+ # Invokes the prepared commands as an ExecGroup
172
+ # @see PDK::Validate::Validator.invoke
173
+ def invoke(report)
174
+ prepare_invoke!
175
+
176
+ process_skipped(report, @skipped)
177
+ process_invalid(report, @invalid)
178
+
179
+ # Nothing to execute so return success
180
+ return 0 if @commands.empty?
181
+
182
+ # If there's no Gemfile, then we can't ensure the binstubs are correct
183
+ PDK::Util::Bundler.ensure_binstubs!(cmd) unless PDK::Util::Bundler::BundleHelper.new.gemfile.nil?
184
+
185
+ exec_group = PDK::CLI::ExecGroup.create(name, { parallel: false }, options)
186
+
187
+ # Register all of the commands for all of the targets
188
+ @commands.each do |item|
189
+ command = item[:command]
190
+ invokation_targets = item[:invokation_targets]
191
+
192
+ exec_group.register do
193
+ result = command.execute!
194
+ begin
195
+ parse_output(report, result, invokation_targets.compact)
196
+ rescue PDK::Validate::ParseOutputError => e
197
+ $stderr.puts e.message
198
+ end
199
+ result[:exit_code]
200
+ end
201
+ end
202
+
203
+ # Now execute and get the return code
204
+ exec_group.exit_code
205
+ end
206
+ end
207
+ end
208
+ end
@@ -0,0 +1,100 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ # An abstract validator that runs ruby code internal to the PDK e.g. JSON and YAML validation, on a single file.
6
+ # The validator code must run within the PDK Ruby environment as opposed to the bundled Ruby environment for a module.
7
+ #
8
+ # At a a minimum child classes should implment the `name`, `pattern` and `validate_target` methods
9
+ #
10
+ # An example concrete implementation could look like:
11
+ #
12
+ # module PDK
13
+ # module Validate
14
+ # module Tasks
15
+ # class TasksNameValidator < InternalRubyValidator
16
+ # def name
17
+ # 'task-name'
18
+ # end
19
+ #
20
+ # def pattern
21
+ # 'tasks/**/*'
22
+ # end
23
+ #
24
+ # def validate_target(report, target)
25
+ # task_name = File.basename(target, File.extname(target))
26
+ # ... ruby code ...
27
+ # success ? 0 : 1
28
+ # end
29
+ # end
30
+ # end
31
+ # end
32
+ # end
33
+ #
34
+ #
35
+ # @abstract
36
+ # @see PDK::Validate::InvokableValidator
37
+ class InternalRubyValidator < InvokableValidator
38
+ # @see PDK::Validate::Validator.prepare_invoke!
39
+ def prepare_invoke!
40
+ return if @prepared
41
+ super
42
+
43
+ # Parse the targets
44
+ @targets, @skipped, @invalid = parse_targets
45
+
46
+ nil
47
+ end
48
+
49
+ # Invokes the validator to call `validate_target` on each target
50
+ # @see PDK::Validate::Validator.invoke
51
+ def invoke(report)
52
+ prepare_invoke!
53
+
54
+ process_skipped(report, @skipped)
55
+ process_invalid(report, @invalid)
56
+
57
+ return 0 if @targets.empty?
58
+
59
+ return_val = 0
60
+
61
+ before_validation
62
+
63
+ start_spinner
64
+ @targets.each do |target|
65
+ validation_result = validate_target(report, target)
66
+ if validation_result.nil?
67
+ report.add_event(
68
+ file: target,
69
+ source: name,
70
+ state: :failure,
71
+ severity: 'error',
72
+ message: "Validation did not return an exit code for #{target}",
73
+ )
74
+ validation_result = 1
75
+ end
76
+ return_val = validation_result if validation_result > return_val
77
+ end
78
+
79
+ stop_spinner(return_val.zero?)
80
+ return_val
81
+ end
82
+
83
+ # Validates a single target
84
+ # It is the responsibility of this method to populate the report with validation messages
85
+ #
86
+ # @param report [PDK::Report] The report to add the events to
87
+ # @param target [String] The target to validate
88
+ #
89
+ # @return [Integer] The exitcode of the validation. Zero indicates success. A non-zero code indicates failure
90
+ # @api private
91
+ # @abstract
92
+ def validate_target(report, target); end
93
+
94
+ # Tasks to run before validation occurs. This is run once every time `.invoke` is called
95
+ # @api private
96
+ # @abstract
97
+ def before_validation; end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,215 @@
1
+ require 'pdk'
2
+
3
+ module PDK
4
+ module Validate
5
+ # A base class for file based validators.
6
+ # This class provides base methods and helpers to help determine the file targets to validate against.
7
+ # Acutal validator implementation should inherit from other child abstract classes e.g. ExternalCommandValdiator
8
+ # @see PDK::Validate::Validator
9
+ # @abstract
10
+ class InvokableValidator < Validator
11
+ # Controls how many times the validator is invoked.
12
+ #
13
+ # :once - The validator will be invoked once and passed all the
14
+ # targets.
15
+ # :per_target - The validator will be invoked for each target
16
+ # separately.
17
+ # @abstract
18
+ def invoke_style
19
+ :once
20
+ end
21
+
22
+ # Whether this Validator can be invoked in this context. By default any InvokableValidator can work in any Context, except ::None
23
+ # @see PDK::Validate::Validator
24
+ def valid_in_context?
25
+ !context.is_a?(PDK::Context::None)
26
+ end
27
+
28
+ # An array, or a string, of glob patterns to use to find targets
29
+ # @return [Array[String], String]
30
+ # @abstract
31
+ def pattern; end
32
+
33
+ # An array, or a string, of glob patterns to use to ignore targets
34
+ # @return [Array[String], String, Nil]
35
+ # @abstract
36
+ def pattern_ignore; end
37
+
38
+ # @see PDK::Validate::Validator.prepare_invoke!
39
+ def prepare_invoke!
40
+ return if @prepared
41
+ super
42
+
43
+ # Register the spinner
44
+ spinner
45
+ nil
46
+ end
47
+
48
+ # Parses the target strings provided from the CLI
49
+ #
50
+ # @param options [Hash] A Hash containing the input options from the CLI.
51
+ #
52
+ # @return targets [Array] An Array of Strings containing target file paths
53
+ # for the validator to validate.
54
+ # @return skipped [Array] An Array of Strings containing targets
55
+ # that are skipped due to target not containing
56
+ # any files that can be validated by the validator.
57
+ # @return invalid [Array] An Array of Strings containing targets that do
58
+ # not exist, and will not be run by validator.
59
+ def parse_targets
60
+ requested_targets = options.fetch(:targets, [])
61
+ # If no targets are specified and empty targets are allowed return with an empty list.
62
+ # It will be up to the validator (and whatever validation tool it uses) to determine the
63
+ # targets. For example, using rubocop with no targets, will allow rubocop to determine the
64
+ # target list using it's .rubocop.yml file
65
+ return [[], [], []] if requested_targets.empty? && allow_empty_targets?
66
+ # If no targets are specified, then we will run validations from the base context directory.
67
+ targets = requested_targets.empty? ? [context.root_path] : requested_targets
68
+ targets.map! { |r| r.gsub(File::ALT_SEPARATOR, File::SEPARATOR) } if File::ALT_SEPARATOR
69
+
70
+ # If this validator is not valid in this context then skip all of the targets
71
+ return [[], targets, []] unless valid_in_context?
72
+
73
+ skipped = []
74
+ invalid = []
75
+ matched = targets.map { |target|
76
+ if pattern.nil?
77
+ target
78
+ else
79
+ if PDK::Util::Filesystem.directory?(target) # rubocop:disable Style/IfInsideElse
80
+ target_root = context.root_path
81
+ pattern_glob = Array(pattern).map { |p| PDK::Util::Filesystem.glob(File.join(target_root, p), File::FNM_DOTMATCH) }
82
+ target_list = pattern_glob.flatten
83
+ .select { |glob| PDK::Util::Filesystem.fnmatch(File.join(PDK::Util::Filesystem.expand_path(PDK::Util.canonical_path(target)), '*'), glob, File::FNM_DOTMATCH) }
84
+ .map { |glob| Pathname.new(glob).relative_path_from(Pathname.new(context.root_path)).to_s }
85
+
86
+ ignore_list = ignore_pathspec
87
+ target_list = target_list.reject { |file| ignore_list.match(file) }
88
+
89
+ skipped << target if target_list.flatten.empty?
90
+ target_list
91
+ elsif PDK::Util::Filesystem.file?(target)
92
+ if Array(pattern).include? target
93
+ target
94
+ elsif Array(pattern).any? { |p| PDK::Util::Filesystem.fnmatch(PDK::Util::Filesystem.expand_path(p), PDK::Util::Filesystem.expand_path(target), File::FNM_DOTMATCH) }
95
+ target
96
+ else
97
+ skipped << target
98
+ next
99
+ end
100
+ else
101
+ invalid << target
102
+ next
103
+ end
104
+ end
105
+ }.compact.flatten.uniq
106
+ [matched, skipped, invalid]
107
+ end
108
+
109
+ # Whether the target parsing ignores "dotfiles" (e.g. .gitignore or .pdkignore) which are considered hidden files in POSIX
110
+ # @return [Boolean]
111
+ # @abstract
112
+ def ignore_dotfiles?
113
+ true
114
+ end
115
+
116
+ # @see PDK::Validate::Validator.spinner_text
117
+ # @abstract
118
+ def spinner_text
119
+ _('Running %{name} validator ...') % { name: name }
120
+ end
121
+
122
+ # @see PDK::Validate::Validator.spinner
123
+ def spinner
124
+ return nil unless spinners_enabled?
125
+ return @spinner unless @spinner.nil?
126
+ require 'pdk/cli/util/spinner'
127
+
128
+ @spinner = TTY::Spinner.new("[:spinner] #{spinner_text}", PDK::CLI::Util.spinner_opts_for_platform)
129
+ end
130
+
131
+ # Process any targets that were skipped by the validator and add the events to the validation report
132
+ # @param report [PDK::Report] The report to add the events to
133
+ # @param skipped [Array[String]] The list of skipped targets
134
+ def process_skipped(report, skipped = [])
135
+ skipped.each do |skipped_target|
136
+ PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target does not contain any files to validate (%{pattern}).') % { validator: name, target: skipped_target, pattern: pattern })
137
+ report.add_event(
138
+ file: skipped_target,
139
+ source: name,
140
+ message: _('Target does not contain any files to validate (%{pattern}).') % { pattern: pattern },
141
+ severity: :info,
142
+ state: :skipped,
143
+ )
144
+ end
145
+ end
146
+
147
+ # Process any targets that were invalid by the validator and add the events to the validation report
148
+ # @param report [PDK::Report] The report to add the events to
149
+ # @param invalid [Array[String]] The list of invalid targets
150
+ def process_invalid(report, invalid = [])
151
+ invalid.each do |invalid_target|
152
+ PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target file not found.') % { validator: name, target: invalid_target })
153
+ report.add_event(
154
+ file: invalid_target,
155
+ source: name,
156
+ message: _('File does not exist.'),
157
+ severity: :error,
158
+ state: :error,
159
+ )
160
+ end
161
+ end
162
+
163
+ # Controls how the validator behaves if not passed any targets.
164
+ #
165
+ # true - PDK will not pass the globbed targets to the validator command
166
+ # and it will instead rely on the underlying tool to find its
167
+ # own default targets.
168
+ # false - PDK will pass the globbed targets to the validator command.
169
+ # @abstract
170
+ def allow_empty_targets?
171
+ false
172
+ end
173
+
174
+ protected
175
+
176
+ # Takes the pattern used in a module context and transforms it depending on the
177
+ # context e.g. A Control Repo will use the module pattern in each module path
178
+ #
179
+ # @param [Array, String] The pattern when used in the module root. Does not start with '/'
180
+ #
181
+ # @return [Array[String]]
182
+ def contextual_pattern(module_pattern)
183
+ module_pattern = [module_pattern] unless module_pattern.is_a?(Array)
184
+ return module_pattern unless context.is_a?(PDK::Context::ControlRepo)
185
+ context.actualized_module_paths.map { |mod_path| module_pattern.map { |pat_path| mod_path + '/*/' + pat_path } }.flatten
186
+ end
187
+
188
+ private
189
+
190
+ # Helper method to collate the default ignored paths
191
+ # @return [PathSpec] Paths to ignore
192
+ def ignore_pathspec
193
+ ignore_pathspec = if context.is_a?(PDK::Context::Module)
194
+ require 'pdk/module'
195
+ PDK::Module.default_ignored_pathspec(ignore_dotfiles?)
196
+ elsif context.is_a?(PDK::Context::ControlRepo)
197
+ require 'pdk/control_repo'
198
+ PDK::ControlRepo.default_ignored_pathspec(ignore_dotfiles?)
199
+ else
200
+ PathSpec.new.tap do |ps|
201
+ ps.add('.*') if ignore_dotfiles?
202
+ end
203
+ end
204
+
205
+ unless pattern_ignore.nil?
206
+ Array(pattern_ignore).each do |pattern|
207
+ ignore_pathspec.add(pattern)
208
+ end
209
+ end
210
+
211
+ ignore_pathspec
212
+ end
213
+ end
214
+ end
215
+ end