Module: RubocopRunner
- Defined in:
- lib/rubocop_runner.rb,
lib/rubocop_runner/version.rb
Constant Summary collapse
- RUBY_PATTERN =
/\.(rb|gemspec)$/
- RUBY_NAMES =
%w(Guardfile Gemfile Rakefile config.ru).freeze
- DEFAULT_ARGS =
%w(--auto-correct --format fuubar --force-exclusion --fail-level autocorrect).freeze
- RUBOCOP_RUNNER_HOME =
Pathname.new(File.join(File.dirname(__FILE__), '..')).realpath
- VERSION =
'2.0.1'.freeze
Class Method Summary collapse
- .binary?(file) ⇒ Boolean
- .conflict_files ⇒ Object
- .create_backup(pre_commit_path) ⇒ Object
- .files ⇒ Object
- .install(root = '.') ⇒ Object
- .merge? ⇒ Boolean
- .root ⇒ Object
- .ruby_file?(filename) ⇒ Boolean
- .run ⇒ Object
- .staged_files ⇒ Object
- .staged_ruby_files ⇒ Object
Class Method Details
.binary?(file) ⇒ Boolean
8 9 10 11 12 |
# File 'lib/rubocop_runner.rb', line 8 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
30 31 32 33 34 35 |
# File 'lib/rubocop_runner.rb', line 30 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
74 75 76 77 78 79 |
# File 'lib/rubocop_runner.rb', line 74 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
37 38 39 40 41 42 43 |
# File 'lib/rubocop_runner.rb', line 37 def files if merge? conflict_files else staged_files end end |
.install(root = '.') ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rubocop_runner.rb', line 81 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) end |
.merge? ⇒ Boolean
26 27 28 |
# File 'lib/rubocop_runner.rb', line 26 def merge? File.exist?('.git/MERGE_MSG') && File.exist?('.git/MERGE_HEAD') end |
.root ⇒ Object
70 71 72 |
# File 'lib/rubocop_runner.rb', line 70 def root RUBOCOP_RUNNER_HOME end |
.ruby_file?(filename) ⇒ Boolean
48 49 50 |
# File 'lib/rubocop_runner.rb', line 48 def ruby_file?(filename) RUBY_NAMES.include?(filename) || !(filename =~ RUBY_PATTERN).nil? end |
.run ⇒ Object
62 63 64 65 |
# File 'lib/rubocop_runner.rb', line 62 def run return 0 if staged_ruby_files.empty? ::RuboCop::CLI.new.run(DEFAULT_ARGS + staged_ruby_files) end |
.staged_files ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rubocop_runner.rb', line 14 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
52 53 54 55 56 |
# File 'lib/rubocop_runner.rb', line 52 def staged_ruby_files @ruby_files ||= files.select do |file| !File.directory?(file) && File.exist?(file) && ruby_file?(file) end end |