Skip to content

Instantly share code, notes, and snippets.

@dumazy
Created June 5, 2024 09:56
Show Gist options
  • Save dumazy/423e6f9b61e91086124736f2741b0e92 to your computer and use it in GitHub Desktop.
Save dumazy/423e6f9b61e91086124736f2741b0e92 to your computer and use it in GitHub Desktop.
Apply all 'dart fix' separately
#!/bin/bash
# This script applies Dart fixes to a project and commits the changes for each fix separately.
# It uses the `dart fix --dry-run` command to find the fixes that can be applied.
# For each fix, it applies the fix and commits the changes with a message indicating the fix code.
checkDart() {
if ! command -v dart &> /dev/null
then
echo "Dart could not be found. Please install Dart first."
exit 1
fi
}
findFixes() {
# Run dart fix --dry-run and store output
output=$(dart fix --dry-run)
# Check if the dart command was successful
if [ $? -eq 0 ]; then
# Extract the codes from lines like "dart fix --apply --code=some_code"
codes=($(echo "$output" | grep -o -E "dart fix --apply --code=[a-zA-Z_]+" | sed -E "s/dart fix --apply --code=//"))
else
echo "There was an error running dart fix --dry-run."
exit 1
fi
}
applyFix() {
dart fix --apply --code=$1
}
commitChanges() {
git add .
git commit -m "Apply dart fix for $1"
}
main() {
checkDart
findFixes
for code in "${codes[@]}"; do
applyFix $code
commitChanges $code
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment