Created
April 15, 2021 22:05
-
-
Save brand-it/ef1267c274cadf0b021fb0d2afe77b96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require 'json' | |
require 'pry' | |
def files | |
@files ||= begin | |
`git diff --staged --name-only --cached`.split("\n").reject do |file| | |
!File.exists?(file) | |
end | |
end | |
end | |
def rubocop | |
@rubocop ||= JSON.parse( | |
`bundle exec rubocop --parallel --force-exclusion --format json #{files.join(' ')}`, | |
symbolize_names: true | |
) | |
end | |
def violations | |
rubocop[:files].select { |x| x[:offenses].any? } | |
end | |
def format_violations | |
violations.each do |violation| | |
violation[:offenses].each do |offense| | |
puts "#{violation[:path]}:#{offense.dig(:location, :line)}:#{offense.dig(:location, :column)} #{offense[:cop_name]} #{offense[:message]}\n" | |
end | |
end | |
puts "\n\n#{rubocop.dig(:summary, :inspected_file_count)} file inspected, #{rubocop.dig(:summary, :offense_count)} offense detected" | |
end | |
def success(message) | |
puts message | |
exit 0 | |
end | |
def rubocop_autocorrect | |
system("bundle exec rubocop -A #{violations.map { |v| v[:path] }.join(' ')}") | |
end | |
def waiting | |
index = 0 | |
chars = %w[| / - \\] | |
while @rubocop.nil? | |
print "rubocop analyzing ... #{chars[(index += 1) % chars.length]}\r" | |
sleep(1) | |
end | |
puts 'rubocop analyzing ... done' | |
end | |
success('no file to check') if files.size.zero? | |
[].tap do |threads| | |
threads << Thread.new { rubocop } | |
threads << Thread.new { waiting } | |
threads.map(&:join) | |
end | |
exit true if violations.empty? | |
format_violations | |
# Open input from keyboard usin IO.new(0) normal standard in is closed | |
fd = IO.sysopen '/dev/tty', 'r' | |
ios = IO.new(fd, 'r') | |
print 'Do you want to fix and exit?(Y) ' | |
if ios.gets.chomp == 'Y' | |
rubocop_autocorrect | |
exit 1 | |
end | |
print 'Do you want to commit anyway?(Y) ' | |
exit ios.gets.chomp == 'Y' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment