Class: PreCommit::RubocopCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/pre-commit/checks/rubocop_check.rb

Class Method Summary collapse

Class Method Details

.call(staged_files) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pre-commit/checks/rubocop_check.rb', line 7

def self.call(staged_files)
  staged_files = staged_files.grep(/\.rb$/)
  return if staged_files.empty?
  config_file = `git config pre-commit.rubocop.config`.chomp

  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 "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



28
29
30
31
32
33
34
35
36
# File 'lib/pre-commit/checks/rubocop_check.rb', line 28

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