Skip to content

Instantly share code, notes, and snippets.

@mikestok
Created February 9, 2016 10:09
Show Gist options
  • Save mikestok/a13eb086aa09a7d042c6 to your computer and use it in GitHub Desktop.
Save mikestok/a13eb086aa09a7d042c6 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# WARNING: Rubocop 0.36.0 can turn not(foo) into notfoo :-( Need to check that
# or wait for 0.37.x to be safe.
#
# Perform automated cleanp of ruby files leaving only uncorrected problems'
# rubocop:disable lines in the files.
#
# e.g.
#
# for f in `grep -rl 'rubocop:disable all' app/controllers`; do
# echo $f
# rubocleanup "$f"
# git add "$f"
# git commit -m "Rubocop automated cleanup: $f"
# done
set -e
function cleanup {
local file=${1?"$0: missing file name"}
sed -e"/^ *# rubocop:/d" < "$file" > "$file~"
bundle exec rubocop --auto-correct "$file~" >/dev/null || true
disables=$(bundle exec rubocop -f j "$file~" \
| jq ".files[].offenses[].cop_name" \
| sort \
| uniq \
| tr -d '"' \
| sed -e 's/^/# rubocop:disable /')
if [ -z "$disables" ]; then
mv "$file~" "$file"
else
(echo "$disables"; cat "$file~") > "$file"
rm "$file~"
fi
}
for file in "$@"; do
cleanup "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment