Add script for bulk autocorrect with Rubocop

We also had an old script which wasn't used and needed manual steps.
This commit is contained in:
Maikel Linke
2023-04-19 13:01:55 +10:00
parent 0b03152f3b
commit c08e264afa
2 changed files with 27 additions and 19 deletions

27
script/rubocop-autocorrect.sh Executable file
View File

@@ -0,0 +1,27 @@
#!/bin/sh
#
# Fixes safe cops automatically and creates a commit for each.
#
if git add --dry-run --all | grep --quiet .; then
echo "Dirty working tree. Please start on a fresh branch."
exit 1
fi
# Refresh todo file so that following commits include only related changes:
bundle exec rubocop --regenerate-todo
git commit --all --message "Regenerate Rubocop's TODO file"
# Iterate over all safe cops:
grep "This cop supports safe autocorrection" -A 5 .rubocop_todo.yml\
| grep '^[A-Z]'\
| tr -d :\
| while read cop; do
echo "Trying to autocorrect safely: $cop"
bundle exec rubocop --regenerate-todo --except "$cop"
echo "Safely autocorrect $cop\n" > .git/COMMIT_EDITMSG
bundle exec rubocop --autocorrect >> .git/COMMIT_EDITMSG
git add --all
git commit --file .git/COMMIT_EDITMSG
done

View File

@@ -1,19 +0,0 @@
#!/bin/sh
# Usage
#
# 1. Clean any git unstagged or untracked changes. Consider creating a new branch
# 2. Remove a cop's exclusion paragraph from the .rubocop_todo.yml
# 3. Run:
#
# $ ./script/rubocop_autocorrect <removed_cop_name>
#
# This will commit all the changes.
set -e
COP="$1"
bundle exec rubocop -a --only "$COP"
git add -A
git commit -m "Auto correct Rubocop's ${COP} cop"