1+ # frozen_string_literal: true
2+
13require 'rubocop_runner/version'
24require 'rubocop'
35
@@ -7,6 +9,7 @@ module RubocopRunner
79 # from https://github.com/djberg96/ptools/blob/master/lib/ptools.rb#L90
810 def binary? ( file )
911 return true if File . ftype ( file ) != 'file'
12+
1013 s = ( File . read ( file , File . stat ( file ) . blksize ) || '' ) . split ( // )
1114 ( ( s . size - s . grep ( ' ' ..'~' ) . size ) / s . size . to_f ) > 0.30
1215 end
@@ -42,8 +45,8 @@ def files
4245 end
4346 end
4447
45- RUBY_PATTERN = /\. (rb|gemspec|rake)$/
46- RUBY_NAMES = %w( Guardfile Gemfile Rakefile config.ru ) . freeze
48+ RUBY_PATTERN = /\. (rb|gemspec|rake)$/ . freeze
49+ RUBY_NAMES = %w[ Guardfile Gemfile Rakefile config.ru ] . freeze
4750
4851 def ruby_file? ( filename )
4952 RUBY_NAMES . include? ( filename ) || !( filename =~ RUBY_PATTERN ) . nil?
@@ -55,12 +58,13 @@ def staged_ruby_files
5558 end
5659 end
5760
58- DEFAULT_ARGS = %w( --auto-correct
61+ DEFAULT_ARGS = %w[ --auto-correct
5962 --format fuubar
6063 --force-exclusion
61- --fail-level autocorrect ) . freeze
64+ --fail-level autocorrect ] . freeze
6265 def run
6366 return 0 if staged_ruby_files . empty?
67+
6468 ::RuboCop ::CLI . new . run ( DEFAULT_ARGS + staged_ruby_files )
6569 end
6670
@@ -73,6 +77,7 @@ def root
7377
7478 def create_backup ( pre_commit_path )
7579 return unless File . exist? ( pre_commit_path )
80+
7681 FileUtils . mv ( pre_commit_path ,
7782 pre_commit_path . join ( '.bkp' ) ,
7883 force : true )
@@ -82,6 +87,7 @@ def install(root = '.')
8287 require 'fileutils'
8388 git_root = Pathname . new "#{ root } /.git"
8489 return false unless File . exist? ( git_root )
90+
8591 pre_commit_path = git_root . join ( 'hooks' , 'pre-commit' )
8692 create_backup ( pre_commit_path )
8793
0 commit comments