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,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,220 @@
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
+ if target_list.flatten.empty?
90
+ PDK.logger.info(_('Validator \'%{validator}\' skipped for \'%{target}\'. No files matching \'%{pattern}\' found to validate.') % { validator: name, target: target, pattern: pattern })
91
+
92
+ skipped << target
93
+ end
94
+
95
+ target_list
96
+ elsif PDK::Util::Filesystem.file?(target)
97
+ if Array(pattern).include? target
98
+ target
99
+ elsif Array(pattern).any? { |p| PDK::Util::Filesystem.fnmatch(PDK::Util::Filesystem.expand_path(p), PDK::Util::Filesystem.expand_path(target), File::FNM_DOTMATCH) }
100
+ target
101
+ else
102
+ skipped << target
103
+ next
104
+ end
105
+ else
106
+ invalid << target
107
+ next
108
+ end
109
+ end
110
+ }.compact.flatten.uniq
111
+ [matched, skipped, invalid]
112
+ end
113
+
114
+ # Whether the target parsing ignores "dotfiles" (e.g. .gitignore or .pdkignore) which are considered hidden files in POSIX
115
+ # @return [Boolean]
116
+ # @abstract
117
+ def ignore_dotfiles?
118
+ true
119
+ end
120
+
121
+ # @see PDK::Validate::Validator.spinner_text
122
+ # @abstract
123
+ def spinner_text
124
+ _('Running %{name} validator ...') % { name: name }
125
+ end
126
+
127
+ # @see PDK::Validate::Validator.spinner
128
+ def spinner
129
+ return nil unless spinners_enabled?
130
+ return @spinner unless @spinner.nil?
131
+ require 'pdk/cli/util/spinner'
132
+
133
+ @spinner = TTY::Spinner.new("[:spinner] #{spinner_text}", PDK::CLI::Util.spinner_opts_for_platform)
134
+ end
135
+
136
+ # Process any targets that were skipped by the validator and add the events to the validation report
137
+ # @param report [PDK::Report] The report to add the events to
138
+ # @param skipped [Array[String]] The list of skipped targets
139
+ def process_skipped(report, skipped = [])
140
+ skipped.each do |skipped_target|
141
+ PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target does not contain any files to validate (%{pattern}).') % { validator: name, target: skipped_target, pattern: pattern })
142
+ report.add_event(
143
+ file: skipped_target,
144
+ source: name,
145
+ message: _('Target does not contain any files to validate (%{pattern}).') % { pattern: pattern },
146
+ severity: :info,
147
+ state: :skipped,
148
+ )
149
+ end
150
+ end
151
+
152
+ # Process any targets that were invalid by the validator and add the events to the validation report
153
+ # @param report [PDK::Report] The report to add the events to
154
+ # @param invalid [Array[String]] The list of invalid targets
155
+ def process_invalid(report, invalid = [])
156
+ invalid.each do |invalid_target|
157
+ PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target file not found.') % { validator: name, target: invalid_target })
158
+ report.add_event(
159
+ file: invalid_target,
160
+ source: name,
161
+ message: _('File does not exist.'),
162
+ severity: :error,
163
+ state: :error,
164
+ )
165
+ end
166
+ end
167
+
168
+ # Controls how the validator behaves if not passed any targets.
169
+ #
170
+ # true - PDK will not pass the globbed targets to the validator command
171
+ # and it will instead rely on the underlying tool to find its
172
+ # own default targets.
173
+ # false - PDK will pass the globbed targets to the validator command.
174
+ # @abstract
175
+ def allow_empty_targets?
176
+ false
177
+ end
178
+
179
+ protected
180
+
181
+ # Takes the pattern used in a module context and transforms it depending on the
182
+ # context e.g. A Control Repo will use the module pattern in each module path
183
+ #
184
+ # @param [Array, String] The pattern when used in the module root. Does not start with '/'
185
+ #
186
+ # @return [Array[String]]
187
+ def contextual_pattern(module_pattern)
188
+ module_pattern = [module_pattern] unless module_pattern.is_a?(Array)
189
+ return module_pattern unless context.is_a?(PDK::Context::ControlRepo)
190
+ context.actualized_module_paths.map { |mod_path| module_pattern.map { |pat_path| mod_path + '/*/' + pat_path } }.flatten
191
+ end
192
+
193
+ private
194
+
195
+ # Helper method to collate the default ignored paths
196
+ # @return [PathSpec] Paths to ignore
197
+ def ignore_pathspec
198
+ ignore_pathspec = if context.is_a?(PDK::Context::Module)
199
+ require 'pdk/module'
200
+ PDK::Module.default_ignored_pathspec(ignore_dotfiles?)
201
+ elsif context.is_a?(PDK::Context::ControlRepo)
202
+ require 'pdk/control_repo'
203
+ PDK::ControlRepo.default_ignored_pathspec(ignore_dotfiles?)
204
+ else
205
+ PathSpec.new.tap do |ps|
206
+ ps.add('.*') if ignore_dotfiles?
207
+ end
208
+ end
209
+
210
+ unless pattern_ignore.nil?
211
+ Array(pattern_ignore).each do |pattern|
212
+ ignore_pathspec.add(pattern)
213
+ end
214
+ end
215
+
216
+ ignore_pathspec
217
+ end
218
+ end
219
+ end
220
+ end