Created
February 26, 2015 01:13
-
-
Save ryanoglesby08/a24d602bf5d5a58f444f to your computer and use it in GitHub Desktop.
A rake task to run rubocop on changed ruby files in a git repository. Meant to be run as part of a pre-commit task.
This file contains hidden or 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
desc 'Run Rubocop on uncommitted changed files' | |
task :git_rubocop do | |
RUBY_FILENAME_PATTERNS = %w(Gemfile Rakefile .rb .rake) | |
def ruby_file?(filename) | |
RUBY_FILENAME_PATTERNS.each do |pattern| | |
return true if filename.include? pattern | |
end | |
false | |
end | |
def deleted?(git_file) | |
git_file.split(' ').first.include? 'D' | |
end | |
def filename_from(git_file) | |
git_file.split(' ').last | |
end | |
git_files = `git status -uno --porcelain` | |
filenames = git_files.split("\n") | |
.reject { |git_file| deleted?(git_file) } | |
.map { |git_file| filename_from(git_file) } | |
.select { |filename| ruby_file?(filename) } | |
.join(' ') | |
sh "bundle exec rubocop #{filenames}" unless filenames.empty? | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment