Skip to content

Instantly share code, notes, and snippets.

@thorstenspringhart
Created January 26, 2018 21:47
Show Gist options
  • Save thorstenspringhart/170e72edfd8ffb44548c988aad3a3947 to your computer and use it in GitHub Desktop.
Save thorstenspringhart/170e72edfd8ffb44548c988aad3a3947 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
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