Class: PreCommit::Checks::Rubocop

Inherits:
Plugin
  • Object
show all
Defined in:
lib/plugins/pre_commit/checks/rubocop.rb

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.aliasesObject



8
9
10
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 8

def self.aliases
  [ :rubocop_all, :rubocop_new ]
end

.descriptionObject



53
54
55
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 53

def self.description
  "Runs rubocop to detect errors."
end

.excludesObject



12
13
14
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 12

def self.excludes
  [ :ruby_symbol_hashrocket ]
end

Instance Method Details

#call(staged_files) ⇒ Object



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
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 16

def call(staged_files)
  require 'rubocop'
rescue LoadError => e
  $stderr.puts "Could not find rubocop: #{e}"
else
  staged_files = staged_files.grep(/\.rb$/)
  return if staged_files.empty?
  config_file = config.get_combined('rubocop.config')

  args = staged_files
  if !config_file.empty?
    if !File.exist? config_file
      $stderr.puts "Warning: rubocop config file '" + config_file + "' does not exist"
      $stderr.puts "Set the path to the config file using:"
      $stderr.puts "\tgit config pre-commit.rubocop.config 'path/relative/to/git/dir/rubocop.yml'"
      $stderr.puts "Or in 'config/pre-commit.yml':"
      $stderr.puts "\trubocop.config: path/relative/to/git/dir/rubocop.yml"
      $stderr.puts "rubocop will use its default configuration or look for a .rubocop.yml file\n\n"
    else
      args = ['-c', config_file] + args
    end
  end

  success, captured = capture { ::Rubocop::CLI.new.run(args) == 0 }
  captured unless success
end

#captureObject



43
44
45
46
47
48
49
50
51
# File 'lib/plugins/pre_commit/checks/rubocop.rb', line 43

def capture
  $stdout, stdout = StringIO.new, $stdout
  $stderr, stderr = StringIO.new, $stderr
  result = yield
  [result, $stdout.string + $stderr.string]
ensure
  $stdout = stdout
  $stderr = stderr
end