Module: RubocopRunner

Defined in:
lib/rubocop_runner.rb,
lib/rubocop_runner/version.rb,
lib/rubocop_runner/rake_task.rb

Defined Under Namespace

Classes: RakeTask

Constant Summary collapse

RUBY_PATTERN =
/\.(rb|gemspec|rake)$/.freeze
RUBY_NAMES =
%w[Guardfile Gemfile Rakefile config.ru].freeze
DEFAULT_ARGS =
%w[-a
--format fuubar
--force-exclusion
--fail-level autocorrect].freeze
RUBOCOP_RUNNER_HOME =
Pathname.new(File.join(File.dirname(__FILE__), '..')).realpath
VERSION =
'2.2.1'

Class Method Summary collapse

Class Method Details

.binary?(file) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
15
# File 'lib/rubocop_runner.rb', line 10

def binary?(file)
  return true if File.ftype(file) != 'file'

  s = (File.read(file, File.stat(file).blksize) || '').split(//)
  ((s.size - s.grep(' '..'~').size) / s.size.to_f) > 0.30
end

.conflict_files ⇒ Object



33
34
35
36
37
38
# File 'lib/rubocop_runner.rb', line 33

def conflict_files
  IO.read('.git/MERGE_MSG')
    .each_line
    .select { |e| e.start_with?("\t") }
    .map(&:strip)
end

.create_backup(pre_commit_path) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/rubocop_runner.rb', line 78

def create_backup(pre_commit_path)
  return unless File.exist?(pre_commit_path)

  FileUtils.mv(pre_commit_path,
               pre_commit_path.join('.bkp'),
               force: true)
end

.files ⇒ Object



40
41
42
43
44
45
46
# File 'lib/rubocop_runner.rb', line 40

def files
  if merge?
    conflict_files
  else
    staged_files
  end
end

.install(root = '.') ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rubocop_runner.rb', line 86

def install(root = '.')
  require 'fileutils'
  git_root = Pathname.new "#{root}/.git"
  return false unless File.exist?(git_root)

  pre_commit_path = git_root.join('hooks', 'pre-commit')
  create_backup(pre_commit_path)

  pre_commit_template_path = RubocopRunner.root.join('lib',
                                                     'template',
                                                     'pre-commit')
  FileUtils.cp(pre_commit_template_path, pre_commit_path)
  FileUtils.chmod('+x', pre_commit_path)
  puts 'RubocopRunner installed!'
end

.merge? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/rubocop_runner.rb', line 29

def merge?
  File.exist?('.git/MERGE_MSG') && File.exist?('.git/MERGE_HEAD')
end

.root ⇒ Object



74
75
76
# File 'lib/rubocop_runner.rb', line 74

def root
  RUBOCOP_RUNNER_HOME
end

.ruby_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rubocop_runner.rb', line 51

def ruby_file?(filename)
  RUBY_NAMES.include?(filename) || !(filename =~ RUBY_PATTERN).nil?
end

.run ⇒ Object



65
66
67
68
69
# File 'lib/rubocop_runner.rb', line 65

def run
  return 0 if staged_ruby_files.empty?

  ::RuboCop::CLI.new.run(DEFAULT_ARGS + staged_ruby_files)
end

.staged_files ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rubocop_runner.rb', line 17

def staged_files
  files = `git diff --cached --name-only --diff-filter=ACM`.split
  files.reject do |f|
    if File.ftype(f) != 'file'
      true
    else
      size = File.size(f)
      size > 1_000_000 || (size > 20 && binary?(f))
    end
  end
end

.staged_ruby_files ⇒ Object



55
56
57
58
59
# File 'lib/rubocop_runner.rb', line 55

def staged_ruby_files
  @ruby_files ||= files.select do |file|
    !File.directory?(file) && File.exist?(file) && ruby_file?(file)
  end
end