|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Usage: cleanup-gradle-module.sh [--backup] |
| 5 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 6 | +ROOT_DIR="$(dirname "$SCRIPT_DIR")" |
| 7 | +BACKUP_MODE=0 |
| 8 | +BACKUP_DIR="$SCRIPT_DIR/backup" |
| 9 | + |
| 10 | +# Parse arguments |
| 11 | +while [[ $# -gt 0 ]]; do |
| 12 | + case $1 in |
| 13 | + --backup) |
| 14 | + BACKUP_MODE=1 |
| 15 | + shift |
| 16 | + ;; |
| 17 | + *) |
| 18 | + echo "Usage: $0 [--backup]" >&2 |
| 19 | + exit 1 |
| 20 | + ;; |
| 21 | + esac |
| 22 | +done |
| 23 | + |
| 24 | + |
| 25 | +if [[ $BACKUP_MODE -eq 1 ]]; then |
| 26 | + mkdir -p "$BACKUP_DIR" |
| 27 | +fi |
| 28 | + |
| 29 | +echo "Searching for old Gradle modules in $ROOT_DIR..." |
| 30 | +find "$ROOT_DIR" -type d | sort -r | while read -r dir; do |
| 31 | + # Skip the root directory itself |
| 32 | + [ "$dir" = "$ROOT_DIR" ] && continue |
| 33 | + # Check if the directory contains only a 'build' directory |
| 34 | + if [ -d "$dir/build" ]; then |
| 35 | + # List all items except 'build' |
| 36 | + OTHERS=$(find "$dir" -mindepth 1 -maxdepth 1 ! -name 'build') |
| 37 | + if [ -z "$OTHERS" ]; then |
| 38 | + if [[ $BACKUP_MODE -eq 1 ]]; then |
| 39 | + # Move to backup dir, preserving relative path |
| 40 | + RELATIVE_PATH="${dir#$ROOT_DIR/}" |
| 41 | + DEST="$BACKUP_DIR/$RELATIVE_PATH" |
| 42 | + mkdir -p "$(dirname "$DEST")" |
| 43 | + mv "$dir" "$DEST" |
| 44 | + echo "Moved to backup: $dir -> $DEST" |
| 45 | + else |
| 46 | + rm -rf "$dir" |
| 47 | + echo "Removed: $dir" |
| 48 | + fi |
| 49 | + fi |
| 50 | + fi |
| 51 | +done |
| 52 | + |
| 53 | +# At the end, if in backup mode and backup folder exists, prompt to remove it |
| 54 | +if [[ $BACKUP_MODE -eq 1 && -d "$BACKUP_DIR" ]]; then |
| 55 | + read -r -p "Remove backup folder '$BACKUP_DIR'? [y/N] " confirm |
| 56 | + case $confirm in |
| 57 | + [yY][eE][sS]|[yY]) |
| 58 | + rm -rf "$BACKUP_DIR" |
| 59 | + echo "Removed backup folder." |
| 60 | + ;; |
| 61 | + *) |
| 62 | + echo "Backup folder retained at $BACKUP_DIR." |
| 63 | + ;; |
| 64 | + esac |
| 65 | +fi |
0 commit comments