Class: WhyClasses::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/why_classes/runner.rb

Overview

Orchestrates a run: find files, analyse each with the active rules, optionally apply corrections, hand the results to a formatter, and compute an exit code.

Defined Under Namespace

Classes: FileReport

Constant Summary collapse

SEVERITY_RANK =
{ none: 0, convention: 1, warning: 2 }.freeze
FORMATTERS =
{
  "progress" => Formatters::ProgressFormatter,
  "clang" => Formatters::ClangFormatter,
  "json" => Formatters::JsonFormatter,
  "diff" => Formatters::DiffFormatter
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, configuration: nil) ⇒ Runner

Returns a new instance of Runner.



35
36
37
38
# File 'lib/why_classes/runner.rb', line 35

def initialize(options, configuration: nil)
  @options = options
  @configuration = configuration || build_configuration
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



40
41
42
# File 'lib/why_classes/runner.rb', line 40

def configuration
  @configuration
end

Instance Method Details

#offenses_for(source_file) ⇒ Object

Analyse a single already-built SourceFile and return its filtered, position-sorted offenses. Handy for specs.



50
51
52
53
54
55
56
# File 'lib/why_classes/runner.rb', line 50

def offenses_for(source_file)
  return [] unless source_file.parsed?

  rules = Registry.active(configuration: @configuration, only: @options.only, except: @options.except)
  raw = rules.flat_map { |rule_class| rule_class.new(@configuration).call(source_file) }
  DisableComments.filter(raw, source_file.comments).sort_by(&:position)
end

#run(io: $stdout) ⇒ Object



42
43
44
45
46
# File 'lib/why_classes/runner.rb', line 42

def run(io: $stdout)
  reports = analyze_files(FileFinder.find(@options.paths, @configuration))
  formatter_for(@options.mode, io).call(reports)
  exit_code(reports)
end