Skip to content

Add script to clean up old Gradle modules #9253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tooling/cleanup-gradle-module.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/usr/bin/env bash
set -euo pipefail

# Usage: cleanup-gradle-module.sh [--backup]
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="$(dirname "$SCRIPT_DIR")"
BACKUP_MODE=0
BACKUP_DIR="$SCRIPT_DIR/backup"

# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--backup)
BACKUP_MODE=1
shift
;;
*)
echo "Usage: $0 [--backup]" >&2
exit 1
;;
esac
done


if [[ $BACKUP_MODE -eq 1 ]]; then
mkdir -p "$BACKUP_DIR"
fi

echo "Searching for old Gradle modules in $ROOT_DIR..."
find "$ROOT_DIR" -type d | sort -r | while read -r dir; do
# Skip the root directory itself
[ "$dir" = "$ROOT_DIR" ] && continue
# Check if the directory contains only a 'build' directory
if [ -d "$dir/build" ]; then
# List all items except 'build'
OTHERS=$(find "$dir" -mindepth 1 -maxdepth 1 ! -name 'build')
if [ -z "$OTHERS" ]; then
if [[ $BACKUP_MODE -eq 1 ]]; then
# Move to backup dir, preserving relative path
RELATIVE_PATH="${dir#$ROOT_DIR/}"
DEST="$BACKUP_DIR/$RELATIVE_PATH"
mkdir -p "$(dirname "$DEST")"
mv "$dir" "$DEST"
echo "Moved to backup: $dir -> $DEST"
else
rm -rf "$dir"
echo "Removed: $dir"
fi
fi
fi
done

# At the end, if in backup mode and backup folder exists, prompt to remove it
if [[ $BACKUP_MODE -eq 1 && -d "$BACKUP_DIR" ]]; then
read -r -p "Remove backup folder '$BACKUP_DIR'? [y/N] " confirm
case $confirm in
[yY][eE][sS]|[yY])
rm -rf "$BACKUP_DIR"
echo "Removed backup folder."
;;
*)
echo "Backup folder retained at $BACKUP_DIR."
;;
esac
fi