forked from DataDog/dd-trace-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup-gradle-module.sh
More file actions
executable file
·65 lines (59 loc) · 1.64 KB
/
Copy pathcleanup-gradle-module.sh
File metadata and controls
executable file
·65 lines (59 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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' ! -name '.*')
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