-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean.sh
More file actions
executable file
·187 lines (161 loc) · 6.36 KB
/
Copy pathclean.sh
File metadata and controls
executable file
·187 lines (161 loc) · 6.36 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/usr/bin/env bash
# clean.sh — GoudEngine repository cleanup utility
# Usage: ./clean.sh [--deep | --size | -h]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NUGET_OUTPUT="$SCRIPT_DIR/sdks/nuget_package_output"
NUGET_LOCAL="$HOME/nuget-local"
WORKTREES_DIR="$SCRIPT_DIR/.claude/worktrees"
KEEP_NUGET=3
usage() {
echo "Usage: $(basename "$0") [--deep | --size | -h]"
echo " (none) Lightweight: incremental dirs, bin/obj, __pycache__, .nupkg, screenshots"
echo " --deep Full: everything above + cargo clean, node_modules, NuGet pruning, worktrees"
echo " --size Report only — show sizes, no deletions"
echo " -h Show this help"
}
kb_to_human() {
local kb="$1"
if [ "$kb" -ge 1048576 ]; then printf "%.1f GB\n" "$(echo "scale=1; $kb/1048576" | bc)"
elif [ "$kb" -ge 1024 ]; then printf "%.1f MB\n" "$(echo "scale=1; $kb/1024" | bc)"
else echo "${kb} KB"; fi
}
path_kb() { [ -e "$1" ] && du -sk "$1" 2>/dev/null | awk '{print $1}' || echo 0; }
find_dirs() { find "$SCRIPT_DIR" "${@}" -type d 2>/dev/null || true; }
# Remove a set of directories found by find args; print reclaimed space.
remove_dirs() {
local label="$1"; shift
local total=0
while IFS= read -r d; do
[ -z "$d" ] && continue
local kb; kb=$(path_kb "$d")
total=$((total + kb))
rm -rf "$d"
done < <(find_dirs "$@")
echo " $label: $(kb_to_human $total)"
}
clean_incremental() {
echo "==> Pruning target/debug/incremental older than 7 days..."
local before; before=$(path_kb "$SCRIPT_DIR/target/debug/incremental")
find "$SCRIPT_DIR/target/debug/incremental" -mindepth 1 -maxdepth 1 \
-type d -mtime +7 -exec rm -rf {} + 2>/dev/null || true
local after; after=$(path_kb "$SCRIPT_DIR/target/debug/incremental")
echo " Reclaimed: $(kb_to_human $(( before - after )))"
}
clean_bin_obj() {
echo "==> Cleaning bin/ and obj/ (excluding target/)..."
remove_dirs "Reclaimed" \
-not \( -path "$SCRIPT_DIR/target/*" -prune \) \
\( -name "bin" -o -name "obj" \)
}
clean_pycache() {
echo "==> Cleaning __pycache__..."
remove_dirs "Reclaimed" -name "__pycache__"
}
clean_stale_nupkg() {
echo "==> Removing stale .nupkg files..."
local total=0
while IFS= read -r f; do
[ -z "$f" ] && continue
local kb; kb=$(path_kb "$f")
total=$((total + kb))
echo " Removing: $(basename "$f")"; rm -f "$f"
done < <(find "$NUGET_OUTPUT" -name "*.nupkg" -type f 2>/dev/null || true)
echo " Reclaimed: $(kb_to_human $total)"
}
clean_screenshots() {
echo "==> Removing flappy-web-*.png screenshots..."
local total=0
while IFS= read -r f; do
[ -z "$f" ] && continue
local kb; kb=$(path_kb "$f")
total=$((total + kb)); rm -f "$f"
done < <(find "$SCRIPT_DIR" -maxdepth 1 -name "flappy-web-*.png" -type f 2>/dev/null || true)
echo " Reclaimed: $(kb_to_human $total)"
}
clean_cargo() {
echo "==> Running cargo clean..."
local before; before=$(path_kb "$SCRIPT_DIR/target")
(cd "$SCRIPT_DIR" && cargo clean)
echo " Reclaimed: $(kb_to_human $(( before - $(path_kb "$SCRIPT_DIR/target") )))"
}
clean_node_modules() {
echo "==> Removing node_modules/ directories..."
remove_dirs "Reclaimed" -name "node_modules" -prune
}
prune_nuget_local() {
echo "==> Pruning ~/nuget-local (keep latest $KEEP_NUGET versions)..."
[ -d "$NUGET_LOCAL" ] || { echo " Not found, skipping."; return; }
local total_count
total_count=$(find "$NUGET_LOCAL" -name "GoudEngine.*.nupkg" -type f 2>/dev/null | wc -l | tr -d ' ')
local remove_count=$((total_count - KEEP_NUGET))
if [ "$remove_count" -le 0 ]; then
echo " Nothing to prune ($total_count packages, keeping $KEEP_NUGET)."
return
fi
local reclaimed=0
find "$NUGET_LOCAL" -name "GoudEngine.*.nupkg" -type f | sort -V | head -n "$remove_count" | while IFS= read -r pkg; do
local kb; kb=$(path_kb "$pkg")
reclaimed=$((reclaimed + kb))
echo " Removing: $(basename "$pkg")"; rm -f "$pkg"
done
echo " Pruned $remove_count of $total_count packages."
}
clean_worktrees() {
echo "==> Cleaning target/ dirs under .claude/worktrees/..."
remove_dirs "Reclaimed" \
-path "$WORKTREES_DIR/*" -maxdepth 3 -name "target"
}
report_sizes() {
echo "=== Reclaimable Space Report ==="
local grand=0
row() {
local label="$1" path="$2"
local kb; kb=$(path_kb "$path")
grand=$((grand + kb))
printf " %-46s %s\n" "$label" "$(kb_to_human $kb)"
}
row "target/" "$SCRIPT_DIR/target"
row "target/debug/incremental/" "$SCRIPT_DIR/target/debug/incremental"
row "~/nuget-local/" "$NUGET_LOCAL"
row ".claude/worktrees/" "$WORKTREES_DIR"
local nm=0
while IFS= read -r d; do [ -z "$d" ] && continue; nm=$((nm + $(path_kb "$d"))); done \
< <(find "$SCRIPT_DIR" -name "node_modules" -type d -prune 2>/dev/null || true)
grand=$((grand + nm))
printf " %-46s %s\n" "node_modules/ (all)" "$(kb_to_human $nm)"
local bo=0
while IFS= read -r d; do [ -z "$d" ] && continue; bo=$((bo + $(path_kb "$d"))); done \
< <(find "$SCRIPT_DIR" -not \( -path "$SCRIPT_DIR/target/*" -prune \) \
\( -name "bin" -o -name "obj" \) -type d 2>/dev/null || true)
grand=$((grand + bo))
printf " %-46s %s\n" "bin/ + obj/ (excl. target/)" "$(kb_to_human $bo)"
local pc=0
while IFS= read -r d; do [ -z "$d" ] && continue; pc=$((pc + $(path_kb "$d"))); done \
< <(find "$SCRIPT_DIR" -name "__pycache__" -type d 2>/dev/null || true)
grand=$((grand + pc))
printf " %-46s %s\n" "__pycache__/ dirs" "$(kb_to_human $pc)"
echo ""; printf " %-46s %s\n" "TOTAL ESTIMATED RECLAIMABLE" "$(kb_to_human $grand)"
echo ""; echo "Note: target/ rows overlap — run './clean.sh --deep' to reclaim all."
}
# ── main ──────────────────────────────────────────────────────────────────────
case "${1:-}" in
-h|--help) usage; exit 0 ;;
--size) report_sizes; exit 0 ;;
--deep) MODE=deep ;;
"") MODE=default ;;
*) echo "Unknown option: $1" >&2; usage >&2; exit 1 ;;
esac
echo "=== GoudEngine cleanup (mode: $MODE) ===" && echo ""
clean_incremental
clean_bin_obj
clean_pycache
clean_stale_nupkg
clean_screenshots
if [ "$MODE" = "deep" ]; then
clean_cargo
clean_node_modules
prune_nuget_local
clean_worktrees
fi
echo "" && echo "=== Done. ==="