module RubocopRunner

Constants

DEFAULT_ARGS
RUBOCOP_RUNNER_HOME
RUBY_NAMES
RUBY_PATTERN
VERSION

Public Instance Methods

binary?(file) click to toggle source

from github.com/djberg96/ptools/blob/master/lib/ptools.rb#L90

# 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() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/rubocop_runner.rb, line 40
def files
  if merge?
    conflict_files
  else
    staged_files
  end
end
install(root = '.') click to toggle source
# 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?() click to toggle source
# File lib/rubocop_runner.rb, line 29
def merge?
  File.exist?('.git/MERGE_MSG') && File.exist?('.git/MERGE_HEAD')
end
root() click to toggle source
# File lib/rubocop_runner.rb, line 74
def root
  RUBOCOP_RUNNER_HOME
end
ruby_file?(filename) click to toggle source
# File lib/rubocop_runner.rb, line 51
def ruby_file?(filename)
  RUBY_NAMES.include?(filename) || !(filename =~ RUBY_PATTERN).nil?
end
run() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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