Skip to content

Instantly share code, notes, and snippets.

@fabiopelosin
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiopelosin/10275367 to your computer and use it in GitHub Desktop.
Save fabiopelosin/10275367 to your computer and use it in GitHub Desktop.

Rubocop Preview

Creates a commit for each violation autofixed by rubocop. The result can be used to make a pull request for discussion purposes.

Features

  • Runs rake spec before commiting any violation.
  • Skips empty violations.

Usage

$ cd ~/repo
$ git checkout -b rubocop_preview
$ path/to/rubocop_preview.rb .
#!/usr/bin/env ruby
require 'pathname'
require 'yaml'
path = Pathname(ARGV[0])
fail 'Missing path argument' unless path
fail "Path doesn't exists" unless path.exist?
Dir.chdir(path) do
puts
puts path
configuration_filename = 'rubocop-todo.yml'
configuration_path = path + configuration_filename
configuration = YAML.load(configuration_path.read)
violations = configuration.keys
violations.each do |violation|
puts
puts violation
configuration_before = configuration.dup
configuration.delete(violation)
File.open(configuration_path, 'w') { |f| f.write(configuration.to_yaml) }
puts ' - auto-correcting...'
`rubocop --auto-correct`
git_status = `git status --porcelain 2>/dev/null`.chomp.lines
dirty_files = git_status.map { |line| line.chomp.split(' ').last }
autocorrected_files = dirty_files - [configuration_filename]
if autocorrected_files.count.zero?
puts '- auto-correct is not supported for this violation'
configuration = configuration_before
else
puts " - #{autocorrected_files.count} corrected"
puts ' - running specs...'
`rake spec`
if $?.exitstatus.zero?
puts ' - committing change...'
p `git add .`
p `git commit -m "[Rubocop] Fix #{violation}"`
else
puts 'Specs failed... reverting'
`git reset --hard HEAD`
configuration = configuration_before
end
end
end
end
@daicoden
Copy link

line 15 needs to be lowercase configuration_filename

@fabiopelosin
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment