Class: PDK::Validate::MetadataSyntax

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/metadata/metadata_syntax.rb

Constant Summary

Constants inherited from BaseValidator

BaseValidator::ALLOW_EMPTY_TARGETS, BaseValidator::IGNORE_DOTFILES, BaseValidator::INVOKE_STYLE

Class Method Summary collapse

Methods inherited from BaseValidator

allow_empty_targets?, cmd_path, ignore_dotfiles?, ignore_pathspec, parse_options, parse_targets, process_invalid, process_skipped

Class Method Details

.create_spinner(targets = [], options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 23

def self.create_spinner(targets = [], options = {})
  return unless PDK::CLI::Util.interactive?
  options = options.merge(PDK::CLI::Util.spinner_opts_for_platform)

  exec_group = options[:exec_group]
  @spinner = if exec_group
               exec_group.add_spinner(spinner_text(targets), options)
             else
               TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options)
             end
  @spinner.auto_spin
end

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 44

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

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

  return 0 if targets.empty?

  return_val = 0
  create_spinner(targets, options)

  # The pure ruby JSON parser gives much nicer parse error messages than
  # the C extension at the cost of slightly slower parsing. We require it
  # here and restore the C extension at the end of the method (if it was
  # being used).
  require 'json/pure'
  JSON.parser = JSON::Pure::Parser

  targets.each do |target|
    unless File.readable?(target)
      report.add_event(
        file: target,
        source: name,
        state: :failure,
        severity: 'error',
        message: _('Could not be read.'),
      )
      return_val = 1
      next
    end

    begin
      JSON.parse(File.read(target))

      report.add_event(
        file:     target,
        source:   name,
        state:    :passed,
        severity: 'ok',
      )
    rescue JSON::ParserError => e
      # Because the message contains a raw segment of the file, we use
      # String#dump here to unescape any escape characters like newlines.
      # We then strip out the surrounding quotes and the exclaimation
      # point that json_pure likes to put in exception messages.
      sane_message = e.message.dump[%r{\A"(.+?)!?"\Z}, 1]

      report.add_event(
        file:     target,
        source:   name,
        state:    :failure,
        severity: 'error',
        message:  sane_message,
      )
      return_val = 1
    end
  end

  stop_spinner(return_val)
  return_val
ensure
  JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser)
end

.nameObject



9
10
11
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 9

def self.name
  'metadata-syntax'
end

.patternObject



13
14
15
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 13

def self.pattern
  ['metadata.json', 'tasks/*.json']
end

.spinner_text(_targets = []) ⇒ Object



17
18
19
20
21
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 17

def self.spinner_text(_targets = [])
  _('Checking metadata syntax (%{targets}).') % {
    targets: pattern.join(' '),
  }
end

.stop_spinner(exit_code) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/pdk/validate/metadata/metadata_syntax.rb', line 36

def self.stop_spinner(exit_code)
  if exit_code.zero? && @spinner
    @spinner.success
  elsif @spinner
    @spinner.error
  end
end