Class: Nanoc::Extra::Checking::Runner Private

Inherits:
Object
  • Object
show all
Defined in:
lib/nanoc/extra/checking/runner.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Runner is reponsible for running issue checks.

Constant Summary collapse

CHECKS_FILENAMES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

['Checks', 'Checks.rb', 'checks', 'checks.rb'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(site) ⇒ Runner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Runner.

Parameters:



9
10
11
# File 'lib/nanoc/extra/checking/runner.rb', line 9

def initialize(site)
  @site = site
end

Instance Method Details

#all_check_classesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
# File 'lib/nanoc/extra/checking/runner.rb', line 95

def all_check_classes
  Nanoc::Extra::Checking::Check.all.map(&:last).uniq
end

#check_classes_named(n) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



99
100
101
102
103
104
105
# File 'lib/nanoc/extra/checking/runner.rb', line 99

def check_classes_named(n)
  n.map do |a|
    klass = Nanoc::Extra::Checking::Check.named(a)
    raise Nanoc::Int::Errors::GenericTrivial, "Unknown check: #{a}" if klass.nil?
    klass
  end
end

#checks_filenameString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The name of the Checks file.

Returns:

  • (String)

    The name of the Checks file



14
15
16
# File 'lib/nanoc/extra/checking/runner.rb', line 14

def checks_filename
  @_checks_filename ||= CHECKS_FILENAMES.find { |f| File.file?(f) }
end

#dslObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
# File 'lib/nanoc/extra/checking/runner.rb', line 85

def dsl
  @dsl
end

#dsl_present?Boolean Also known as: has_dsl?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns true if a Checks file exists, false otherwise.

Returns:

  • (Boolean)

    true if a Checks file exists, false otherwise



19
20
21
# File 'lib/nanoc/extra/checking/runner.rb', line 19

def dsl_present?
  checks_filename && File.file?(checks_filename)
end

#list_checksvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Lists all available checks on stdout.



27
28
29
30
31
32
33
# File 'lib/nanoc/extra/checking/runner.rb', line 27

def list_checks
  load_dsl_if_available

  puts 'Available checks:'
  puts
  puts all_check_classes.map { |i| '  ' + i.identifier.to_s }.sort.join("\n")
end

#load_dsl_if_availableObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nanoc/extra/checking/runner.rb', line 65

def load_dsl_if_available
  @dsl_loaded ||= false
  unless @dsl_loaded
    @dsl =
      if dsl_present?
        Nanoc::Extra::Checking::DSL.from_file(checks_filename)
      else
        nil
      end
    @dsl_loaded = true
  end
end

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/nanoc/extra/checking/runner.rb', line 136

def print_issues(issues)
  require 'colored'

  return if issues.empty?
  puts 'Issues found!'
  issues.group_by(&:subject).to_a.sort_by { |s| subject_to_s(s.first) }.each do |pair|
    subject = pair.first
    issues  = pair.last
    next if issues.empty?

    puts "  #{subject_to_s(subject)}:"
    issues.each do |i|
      puts "    [ #{'ERROR'.red} ] #{i.check_class.identifier} - #{i.description}"
    end
  end
end

#require_dslObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



78
79
80
81
82
83
# File 'lib/nanoc/extra/checking/runner.rb', line 78

def require_dsl
  load_dsl_if_available
  if dsl.nil?
    raise Nanoc::Int::Errors::GenericTrivial, "No checks defined (no #{CHECKS_FILENAMES.first} file present)"
  end
end

#run_allBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs all checks.

Returns:

  • (Boolean)

    true if successful, false otherwise



38
39
40
41
42
# File 'lib/nanoc/extra/checking/runner.rb', line 38

def run_all
  load_dsl_if_available

  run_check_classes(all_check_classes)
end

#run_check_classes(classes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
92
93
# File 'lib/nanoc/extra/checking/runner.rb', line 89

def run_check_classes(classes)
  issues = run_checks(classes)
  print_issues(issues)
  issues.empty? ? true : false
end

#run_checks(classes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nanoc/extra/checking/runner.rb', line 107

def run_checks(classes)
  return [] if classes.empty?

  # TODO: remove me
  @site.compiler.build_reps

  checks = []
  issues = Set.new
  length = classes.map { |c| c.identifier.to_s.length }.max + 18
  classes.each do |klass|
    print format("  %-#{length}s", "Running check #{klass.identifier}")

    check = klass.create(@site)
    check.run

    checks << check
    issues.merge(check.issues)

    # TODO: report progress

    puts check.issues.empty? ? 'ok'.green : 'error'.red
  end
  issues
end

#run_for_deployBoolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the checks marked for deployment.

Returns:

  • (Boolean)

    true if successful, false otherwise



47
48
49
50
51
52
# File 'lib/nanoc/extra/checking/runner.rb', line 47

def run_for_deploy
  require_dsl

  return true if dsl.nil?
  run_check_classes(check_classes_named(dsl.deploy_checks))
end

#run_specific(check_class_names) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the checks with the given names.

Parameters:

  • check_class_names (Array<Symbol>)

    The names of the checks

Returns:

  • (Boolean)

    true if successful, false otherwise



59
60
61
62
63
# File 'lib/nanoc/extra/checking/runner.rb', line 59

def run_specific(check_class_names)
  load_dsl_if_available

  run_check_classes(check_classes_named(check_class_names))
end

#subject_to_s(s) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



132
133
134
# File 'lib/nanoc/extra/checking/runner.rb', line 132

def subject_to_s(s)
  s || '(global)'
end