-
-
Save harssh/7aeec04b4fbe4701ec64802174dda7b0 to your computer and use it in GitHub Desktop.
extended rake tasks to run rubocop only on uncommitted files / diff with master or entire project
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
namespace :clean_code do | |
# rubcop only available in dev | |
if Rails.env.development? | |
require 'rubocop/rake_task' | |
def diff_files | |
cmd = %q( git diff --name-only --diff-filter=ACMRTUXB \ | |
$(git merge-base HEAD origin/master) \ | |
| egrep '\.rake$|\.rb$' ) | |
diff = `#{cmd}` | |
diff.split("\n") | |
end | |
def uncommitted_files | |
cmd = "git ls-files -m | xargs ls -1 2>/dev/null | egrep '\.rake$|\.rb$'" | |
diff = `#{cmd}` | |
diff.split("\n") | |
end | |
def patterns_for_diff_files | |
# should contain at least one file | |
patterns = ['lib/tasks/clean_code.rake'] | |
patterns += diff_files | |
end | |
def patterns_for_changed_files | |
# should contain at least one file | |
patterns = ['lib/tasks/clean_code.rake'] | |
patterns += uncommitted_files | |
end | |
desc 'Run RuboCop on the entire project' | |
RuboCop::RakeTask.new('rubocop') do |task| | |
task.fail_on_error = true | |
end | |
desc 'Run RuboCop on the project based on git diff with master branch' | |
RuboCop::RakeTask.new('rubocop_diff') do |task| | |
task.patterns = patterns_for_diff_files | |
task.fail_on_error = true | |
end | |
desc 'Run RuboCop on uncommitted files' | |
RuboCop::RakeTask.new('rubocop_changed') do |task| | |
task.patterns = patterns_for_changed_files | |
task.fail_on_error = true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment