@@ -5,31 +5,57 @@ task :rubocop do
55 sh 'bundle exec rubocop'
66end
77
8- desc 'Run RuboCop with auto-correct '
9- task 'rubocop:auto_correct ' do
8+ desc 'Run RuboCop with autocorrect '
9+ task 'rubocop:autocorrect ' do
1010 sh 'bundle exec rubocop -A'
1111end
1212
1313desc 'Run all linters'
1414task lint : :rubocop
1515
1616desc 'Auto-fix all linting issues'
17- task 'lint:fix' => 'rubocop:auto_correct'
17+ task 'lint:fix' => 'rubocop:autocorrect'
18+
19+ # Helper method to check if file is likely binary
20+ def binary_file? ( filepath )
21+ return false unless File . exist? ( filepath )
22+
23+ # Read first 8192 bytes to check for binary content
24+ File . open ( filepath , 'rb' ) do |file |
25+ chunk = file . read ( 8192 ) || ''
26+ # File is binary if it contains null bytes or has high ratio of non-printable chars
27+ return true if chunk . include? ( "\x00 " )
28+
29+ # Check for high ratio of non-printable characters
30+ non_printable = chunk . count ( "\x01 -\x08 \x0B \x0C \x0E -\x1F \x7F -\xFF " )
31+ non_printable . to_f / chunk . size > 0.3
32+ end
33+ rescue StandardError
34+ # If we can't read the file, assume it's not something we should check
35+ true
36+ end
37+
38+ # Maximum file size to check (10MB)
39+ MAX_FILE_SIZE = 10 * 1024 * 1024
1840
1941desc 'Ensure all files end with newline'
2042task :check_newlines do
2143 files_without_newline = [ ]
2244
23- # Define excluded directories
24- excluded_dirs = %w[ vendor/ node_modules/ .git/ pkg/ tmp/ coverage/ ]
45+ # Define excluded directories (matching RuboCop config)
46+ excluded_dirs = %w[ vendor/ node_modules/ .git/ pkg/ tmp/ coverage/ specs_e2e/ e2e/ spec/fixtures/ ]
2547
2648 # Get all relevant files and filter out excluded directories more efficiently
2749 Dir . glob ( '**/*.{rb,rake,yml,yaml,md,gemspec,ru,erb,js,json}' )
2850 . reject { |f | excluded_dirs . any? { |dir | f . start_with? ( dir ) } }
29- . select { |f | File . file? ( f ) }
51+ . select { |f | File . file? ( f ) && File . size ( f ) < MAX_FILE_SIZE && ! binary_file? ( f ) }
3052 . each do |file |
31- content = File . read ( file )
32- files_without_newline << file unless content . empty? || content . end_with? ( "\n " )
53+ # Read only the last few bytes to check for newline
54+ File . open ( file , 'rb' ) do |f |
55+ f . seek ( [ f . size - 2 , 0 ] . max )
56+ tail = f . read
57+ files_without_newline << file unless tail . nil? || tail . empty? || tail . end_with? ( "\n " )
58+ end
3359 end
3460
3561 if files_without_newline . any?
@@ -45,14 +71,15 @@ desc 'Fix files missing final newline'
4571task :fix_newlines do
4672 fixed_files = [ ]
4773
48- # Define excluded directories (same as check_newlines )
49- excluded_dirs = %w[ vendor/ node_modules/ .git/ pkg/ tmp/ coverage/ ]
74+ # Define excluded directories (matching RuboCop config )
75+ excluded_dirs = %w[ vendor/ node_modules/ .git/ pkg/ tmp/ coverage/ specs_e2e/ e2e/ spec/fixtures/ ]
5076
5177 # Get all relevant files and filter out excluded directories more efficiently
5278 Dir . glob ( '**/*.{rb,rake,yml,yaml,md,gemspec,ru,erb,js,json}' )
5379 . reject { |f | excluded_dirs . any? { |dir | f . start_with? ( dir ) } }
54- . select { |f | File . file? ( f ) }
80+ . select { |f | File . file? ( f ) && File . size ( f ) < MAX_FILE_SIZE && ! binary_file? ( f ) }
5581 . each do |file |
82+ # Read file to check if it needs a newline
5683 content = File . read ( file )
5784 unless content . empty? || content . end_with? ( "\n " )
5885 File . write ( file , content + "\n " )
0 commit comments