Last active
May 9, 2017 18:22
-
-
Save trueheart78/72dac2a1b4b7d9957fdad7e6ca54f0f8 to your computer and use it in GitHub Desktop.
RuboCopper
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
class RuboCopper | |
def analyze | |
system 'rubocop', '--display-cop-names', *files | |
end | |
def autofix | |
system 'rubocop', '--auto-correct', '--display-cop-names', *files | |
end | |
def valid? | |
`rubocop #{files.join(' ')}`.include? 'no offenses detected' | |
end | |
private | |
def files | |
@files ||= (changed_files + untracked_files + staged_files) | |
.split("\n") | |
.uniq | |
.select { |f| ruby_file?(f) && File.exist?(f) } | |
end | |
def changed_files | |
@changed_files ||= `git diff --name-only master #{branch}` | |
end | |
def untracked_files | |
@untracked_files ||= `git ls-files . --exclude-standard --others` | |
end | |
def staged_files | |
@staged_files ||= `git diff --name-only --cached` | |
end | |
def ruby_file?(file) | |
file[-3..(file.length)] == '.rb' | |
end | |
def branch | |
@branch ||= `git rev-parse --abbrev-ref HEAD`.chomp | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment