Class: PDK::Validate::Tasks::Name

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/tasks/name.rb

Constant Summary collapse

INVALID_TASK_MSG =
_(
  'Invalid task name. Task names must start with a lowercase letter ' \
  'and can only contain lowercase letters, numbers, and underscores.',
)

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pdk/validate/tasks/name.rb', line 26

def self.create_spinner(targets = [], options = {})
  require 'pdk/cli/util'

  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
               require 'pdk/cli/util/spinner'

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

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



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
# File 'lib/pdk/validate/tasks/name.rb', line 51

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)

  targets.each do |target|
    task_name = File.basename(target, File.extname(target))
    if PDK::CLI::Util::OptionValidator.valid_task_name?(task_name)
      report.add_event(
        file:     target,
        source:   name,
        state:    :passed,
        severity: 'ok',
      )
    else
      report.add_event(
        file:     target,
        source:   name,
        state:    :failure,
        severity: 'error',
        message:  INVALID_TASK_MSG,
      )

      return_val = 1
    end
  end

  stop_spinner(return_val)
  return_val
end

.nameObject



12
13
14
# File 'lib/pdk/validate/tasks/name.rb', line 12

def self.name
  'task-name'
end

.patternObject



16
17
18
# File 'lib/pdk/validate/tasks/name.rb', line 16

def self.pattern
  'tasks/**/*'
end

.spinner_text(_targets = []) ⇒ Object



20
21
22
23
24
# File 'lib/pdk/validate/tasks/name.rb', line 20

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

.stop_spinner(exit_code) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/pdk/validate/tasks/name.rb', line 43

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