Method: Rubocop::CLI#run

Defined in:
lib/rubocop/cli.rb

#run(args = ARGV) ⇒ Fixnum

Entry point for the application logic. Here we do the command line arguments processing and inspect the target files

Returns:

  • (Fixnum)

    UNIX exit code



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/rubocop/cli.rb', line 15

def run(args = ARGV)
  $options = { mode: :default }

  OptionParser.new do |opts|
    opts.banner = 'Usage: rubocop [options] [file1, file2, ...]'

    opts.on('-d', '--[no-]debug', 'Display debug info') do |d|
      $options[:debug] = d
    end
    opts.on('-e', '--emacs', 'Emacs style output') do
      $options[:mode] = :emacs_style
    end
    opts.on('-c FILE', '--config FILE', 'Configuration file') do |f|
      $options[:config] = YAML.load_file(f)
    end
    opts.on('-s', '--silent', 'Silence summary') do |s|
      $options[:silent] = s
    end
    opts.on('-v', '--version', 'Display version') do
      puts Rubocop::VERSION
      exit(0)
    end
  end.parse!(args)

  cops = Cop::Cop.all
  show_cops_on_duty(cops) if $options[:debug]
  total_offences = 0
  @configs = {}

  target_files(args).each do |file|
    report = Report.create(file, $options[:mode])
    source = File.readlines(file).map do |line|
      get_rid_of_invalid_byte_sequences(line)
      line.chomp
    end

    syntax_cop = Rubocop::Cop::Syntax.new
    syntax_cop.inspect(file, source, nil, nil)

    if syntax_cop.offences.map(&:severity).include?(:error)
      # In case of a syntax error we just report that error and do
      # no more checking in the file.
      report << syntax_cop
      total_offences += syntax_cop.offences.count
    else
      tokens, sexp, correlations = CLI.rip_source(source)
      config = $options[:config] || config_from_dotfile(File.dirname(file))

      cops.each do |cop_klass|
        cop_config = config[cop_klass.name.split('::').last] if config
        if cop_config.nil? || cop_config['Enabled']
          cop_klass.config = cop_config
          cop = cop_klass.new
          cop.correlations = correlations
          cop.inspect(file, source, tokens, sexp)
          total_offences += cop.offences.count
          report << cop if cop.has_report?
        end
      end
    end

    report.display unless report.empty?
  end

  unless $options[:silent]
    print "\n#{target_files(args).count} files inspected, "
    puts "#{total_offences} offences detected"
      .send(total_offences.zero? ? :green : :red)
  end

  return total_offences == 0 ? 0 : 1
end