Class: PDK::Validate::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/validate/base_validator.rb

Constant Summary collapse

INVOKE_STYLE =

Controls how many times the validator is invoked.

:once -       The validator will be invoked once and passed all the
              targets.
:per_target - The validator will be invoked for each target
              separately.
:once

Class Method Summary collapse

Class Method Details

.cmd_pathObject



15
16
17
# File 'lib/pdk/validate/base_validator.rb', line 15

def self.cmd_path
  File.join(PDK::Util.module_root, 'bin', cmd)
end

.invoke(report, options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/pdk/validate/base_validator.rb', line 103

def self.invoke(report, options = {})
  targets, skipped, invalid = parse_targets(options)

  process_skipped(report, skipped)
  process_invalid(report, invalid)

  return 0 if targets.empty?

  PDK::Util::Bundler.ensure_binstubs!(cmd)

  # If invoking :per_target, split the targets array into an array of
  # single element arrays (one per target). If invoking :once, wrap the
  # targets array in another array. This is so we can loop through the
  # invokes with the same logic, regardless of which invoke style is
  # needed.
  targets = (self::INVOKE_STYLE == :per_target) ? targets.combination(1).to_a : Array[targets]
  exit_codes = []

  targets.each do |invokation_targets|
    cmd_argv = parse_options(options, invokation_targets).unshift(cmd_path)
    cmd_argv.unshift('ruby', '-W0') if Gem.win_platform?

    command = PDK::CLI::Exec::Command.new(*cmd_argv).tap do |c|
      c.context = :module
      exec_group = options[:exec_group]
      if exec_group
        sub_spinner = exec_group.add_spinner(spinner_text(invokation_targets))
        c.register_spinner(sub_spinner)
      else
        c.add_spinner(spinner_text(invokation_targets))
      end
    end

    result = command.execute!
    exit_codes << result[:exit_code]

    parse_output(report, result, invokation_targets)
  end

  exit_codes.max
end

.parse_options(_options, targets) ⇒ Object



69
70
71
# File 'lib/pdk/validate/base_validator.rb', line 69

def self.parse_options(_options, targets)
  targets
end

.parse_targets(options) ⇒ Object

Parses the target strings provided from the CLI

Parameters:

  • options (Hash)

    A Hash containing the input options from the CLI.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pdk/validate/base_validator.rb', line 30

def self.parse_targets(options)
  # If no targets are specified, then we will run validations from the
  # base module directory.
  targets = if options[:targets].nil? || options[:targets].empty?
              [PDK::Util.module_root]
            else
              options[:targets]
            end

  targets.map! { |r| r.gsub(File::ALT_SEPARATOR, File::SEPARATOR) } if File::ALT_SEPARATOR
  skipped = []
  invalid = []
  matched = targets.map { |target|
    if respond_to?(:pattern)
      if File.directory?(target)
        pattern_glob = Array(pattern).map { |p| Dir.glob(File.join(PDK::Util.module_root, p)) }
        target_list = pattern_glob.flatten.select { |file| File.fnmatch(File.join(File.expand_path(target), '*'), file) }
        skipped << target if target_list.flatten.empty?
        target_list
      elsif File.file?(target)
        if Array(pattern).include? target
          target
        elsif Array(pattern).any? { |p| File.fnmatch(File.expand_path(p), File.expand_path(target)) }
          target
        else
          skipped << target
          next
        end
      else
        invalid << target
        next
      end
    else
      target
    end
  }.compact.flatten
  [matched, skipped, invalid]
end

.process_invalid(report, invalid = []) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pdk/validate/base_validator.rb', line 90

def self.process_invalid(report, invalid = [])
  invalid.each do |invalid_target|
    PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target file not found.') % { validator: name, target: invalid_target })
    report.add_event(
      file:     invalid_target,
      source:   name,
      message:  _('File does not exist.'),
      severity: :error,
      state:    :error,
    )
  end
end

.process_skipped(report, skipped = []) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pdk/validate/base_validator.rb', line 77

def self.process_skipped(report, skipped = [])
  skipped.each do |skipped_target|
    PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target does not contain any files to validate (%{pattern}).') % { validator: name, target: skipped_target, pattern: pattern })
    report.add_event(
      file:     skipped_target,
      source:   name,
      message:  _('Target does not contain any files to validate (%{pattern}).') % { pattern: pattern },
      severity: :info,
      state:    :skipped,
    )
  end
end

.spinner_text(_targets = nil) ⇒ Object



73
74
75
# File 'lib/pdk/validate/base_validator.rb', line 73

def self.spinner_text(_targets = nil)
  _('Invoking %{cmd}') % { cmd: cmd }
end