Class: PreCommit::RubocopCheck

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :all) ⇒ RubocopCheck

Returns a new instance of RubocopCheck.



14
15
16
# File 'lib/pre-commit/checks/rubocop_check.rb', line 14

def initialize(type = :all)
  @type = type
end

Instance Attribute Details

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/pre-commit/checks/rubocop_check.rb', line 8

def type
  @type
end

Instance Method Details

#callObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pre-commit/checks/rubocop_check.rb', line 31

def call
  rb_files = reject_non_rb(files_to_check)
  return true if rb_files.empty?
  config_file = `git config pre-commit.rubocop.config`.chomp
  args = rb_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
  run(args)
end

#check_nameObject



10
11
12
# File 'lib/pre-commit/checks/rubocop_check.rb', line 10

def check_name
  "Rubocop"
end

#files_to_checkObject



18
19
20
21
22
23
24
25
# File 'lib/pre-commit/checks/rubocop_check.rb', line 18

def files_to_check
  case @type
  when :new
    Utils.new_files('.').split(" ")
  else
    Utils.staged_files('.').split(" ")
  end
end

#reject_non_rb(staged_files) ⇒ Object



27
28
29
# File 'lib/pre-commit/checks/rubocop_check.rb', line 27

def reject_non_rb(staged_files)
  staged_files.select { |f| f =~ /\.rb$/ }
end

#run(rb_files) ⇒ Object



49
50
51
52
# File 'lib/pre-commit/checks/rubocop_check.rb', line 49

def run(rb_files)
  rubocop = Rubocop::CLI.new
  return rubocop.run(rb_files) == 0
end