Class: Quality::Runner

Inherits:
Object
  • Object
show all
Includes:
Tools::BigFiles, Tools::Cane, Tools::Flay, Tools::Flog, Tools::Punchlist, Tools::Reek, Tools::Rubocop
Defined in:
lib/quality/runner.rb

Overview

Knows how to run different quality tools based on a configuration already determined.

Instance Method Summary collapse

Constructor Details

#initialize(config, gem_spec: Gem::Specification, quality_checker_class: Quality::QualityChecker, count_io: IO, count_file: File, globber: Dir) ⇒ Runner

Returns a new instance of Runner.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/quality/runner.rb', line 23

def initialize(config,
               gem_spec: Gem::Specification,
               quality_checker_class: Quality::QualityChecker,
               count_io: IO,
               count_file: File,
               globber: Dir)
  @config = config
  @gem_spec = gem_spec
  @quality_checker_class = quality_checker_class
  @count_io = count_io
  @count_file = count_file
  @globber = globber
end

Instance Method Details

#count_existing_violations(filename) ⇒ Object



73
74
75
76
77
# File 'lib/quality/runner.rb', line 73

def count_existing_violations(filename)
  existing_violations = @count_io.read(filename).to_i
  fail("Problem with file #{filename}") if existing_violations < 0
  existing_violations
end

#ratchet_quality_cmd(cmd, command_options, &count_violations_on_line) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/quality/runner.rb', line 87

def ratchet_quality_cmd(cmd,
                        command_options,
                        &count_violations_on_line)
  quality_checker = @quality_checker_class.new(cmd,
                                               command_options,
                                               @config.output_dir,
                                               @config.verbose)
  quality_checker.execute(&count_violations_on_line)
end

#ruby_filesObject



97
98
99
# File 'lib/quality/runner.rb', line 97

def ruby_files
  @config.ruby_files
end

#ruby_files_globObject



101
102
103
# File 'lib/quality/runner.rb', line 101

def ruby_files_glob
  @config.ruby_files_glob
end

#run_qualityObject



37
38
39
# File 'lib/quality/runner.rb', line 37

def run_quality
  tools.each { |tool| run_quality_with_tool(tool) }
end

#run_quality_with_tool(tool) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/quality/runner.rb', line 41

def run_quality_with_tool(tool)
  installed = @gem_spec.find_all_by_name(tool).any?
  suppressed = @config.skip_tools.include? tool

  if installed && !suppressed
    method("quality_#{tool}".to_sym).call
  elsif !installed
    puts "#{tool} not installed"
  end
end

#run_ratchetObject



52
53
54
55
56
57
58
# File 'lib/quality/runner.rb', line 52

def run_ratchet
  # XXX: a lot of things know about globbing--isn't this config's job?
  @globber.glob("#{@config.output_dir}/*_high_water_mark")
    .each do |filename|
    run_ratchet_on_file(filename)
  end
end

#run_ratchet_on_file(filename) ⇒ Object



60
61
62
63
64
65
# File 'lib/quality/runner.rb', line 60

def run_ratchet_on_file(filename)
  puts "Processing #{filename}"
  existing_violations = count_existing_violations(filename)
  new_violations = [0, existing_violations - 1].max
  write_violations(filename, new_violations)
end

#source_files_globObject



105
106
107
# File 'lib/quality/runner.rb', line 105

def source_files_glob
  @config.source_files_glob
end

#toolsObject



79
80
81
82
83
84
85
# File 'lib/quality/runner.rb', line 79

def tools
  self.class.ancestors.map do |ancestor|
    ancestor_name = ancestor.to_s
    next unless ancestor_name.start_with?('Quality::Tools::')
    ancestor_name.split('::').last.downcase
  end.compact
end

#write_violations(filename, new_violations) ⇒ Object



67
68
69
70
71
# File 'lib/quality/runner.rb', line 67

def write_violations(filename, new_violations)
  @count_file.open(filename, 'w') do |file|
    file.write(new_violations.to_s + "\n")
  end
end