Created
January 5, 2024 20:48
-
-
Save huitseeker/99ba921da591497de7879d1724e3e96b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
# Constants | |
REGEX='s/^(\s*)pub(\(.*\))?\s/\1/' | |
EX=".rs" | |
MSG="removing public modifier(s) in" | |
CHECK="cargo check --all --tests --examples --benches" | |
# Check current directory is a Rust project root and tracked by Git | |
CUR_DIR=$(pwd) | |
if [ ! -f "$CUR_DIR/Cargo.toml" ] || [ ! -d "$CUR_DIR/src" ] || [ ! -d "$CUR_DIR/.git" ]; then | |
echo "Not a valid Rust project root or not a Git repository." | |
exit 1 | |
fi | |
# Check for a clean git working directory | |
if [ -n "$(git status --porcelain)" ]; then | |
echo "Working directory is not clean. Please commit any changes first." | |
exit 1 | |
fi | |
# Function to apply regex to a single line | |
apply_regex_to_line() { | |
local file="$1" | |
local line_num="$2" | |
gsed -i -E "${line_num}${REGEX}" "$file" | |
} | |
# Iterate over files | |
gfind "$CUR_DIR/src" -type f -name "*$EX" | while read -r F; do | |
line_count=$(wc -l < "$F") | |
for (( i=1; i<=line_count; i++ )); do | |
# Print current line and filename every 10th line | |
if (( i % 25 == 0 )); then | |
echo "Processing line $i in file $F" | |
fi | |
# Make a copy of the file | |
cp "$F" "${F}.bak" | |
# Apply regex to the current line | |
apply_regex_to_line "$F" "$i" | |
# Check if the file changed | |
if ! cmp -s "$F" "${F}.bak"; then | |
echo "Testing change in $F line $i" | |
# If changes were made, check and possibly commit them | |
if $CHECK > /dev/null 2>&1; then | |
relative_path=$(realpath --relative-to="$CUR_DIR" "$F") | |
# Commit changes with relative file path in the message | |
git add "$F" | |
git commit -m "$MSG $relative_path" | |
else | |
# Revert changes if check failed | |
mv "${F}.bak" "$F" | |
fi | |
fi | |
# Clean up backup file | |
rm -f "${F}.bak" | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment