Skip to content

Instantly share code, notes, and snippets.

@trueheart78
Last active May 9, 2017 18:22
Show Gist options
  • Save trueheart78/72dac2a1b4b7d9957fdad7e6ca54f0f8 to your computer and use it in GitHub Desktop.
Save trueheart78/72dac2a1b4b7d9957fdad7e6ca54f0f8 to your computer and use it in GitHub Desktop.
RuboCopper
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