From 8fa3a2c9c5a958ae1343cc64a908b079f7657eec Mon Sep 17 00:00:00 2001 From: Tw93 Date: Sat, 6 Dec 2025 20:28:49 +0800 Subject: [PATCH] Clean up and calculate more correctly. --- lib/clean/app_caches.sh | 19 ++++++++++++++- lib/clean/dev.sh | 49 +++++++++++++++++++------------------ lib/core/common.sh | 54 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 26 deletions(-) diff --git a/lib/clean/app_caches.sh b/lib/clean/app_caches.sh index 973a62b..65c202d 100644 --- a/lib/clean/app_caches.sh +++ b/lib/clean/app_caches.sh @@ -5,14 +5,31 @@ set -euo pipefail # Clean Xcode and iOS development tools +# Archives can be significant in size (app packaging files) +# DeviceSupport files for old iOS versions can accumulate +# Note: Skips critical files if Xcode is running clean_xcode_tools() { - safe_clean ~/Library/Developer/Xcode/DerivedData/* "Xcode derived data" + # Check if Xcode is running for safer cleanup of critical resources + local xcode_running=false + if pgrep -x "Xcode" > /dev/null 2>&1; then + xcode_running=true + fi + + # Safe to clean regardless of Xcode state safe_clean ~/Library/Developer/CoreSimulator/Caches/* "Simulator cache" safe_clean ~/Library/Developer/CoreSimulator/Devices/*/data/tmp/* "Simulator temp files" safe_clean ~/Library/Caches/com.apple.dt.Xcode/* "Xcode cache" safe_clean ~/Library/Developer/Xcode/iOS\ Device\ Logs/* "iOS device logs" safe_clean ~/Library/Developer/Xcode/watchOS\ Device\ Logs/* "watchOS device logs" safe_clean ~/Library/Developer/Xcode/Products/* "Xcode build products" + + # Clean build artifacts only if Xcode is not running + if [[ "$xcode_running" == "false" ]]; then + safe_clean ~/Library/Developer/Xcode/DerivedData/* "Xcode derived data" + safe_clean ~/Library/Developer/Xcode/Archives/* "Xcode archives" + else + echo -e " ${YELLOW}${ICON_WARNING}${NC} Xcode is running, skipping DerivedData and Archives cleanup" + fi } # Clean code editors (VS Code, Sublime, etc.) diff --git a/lib/clean/dev.sh b/lib/clean/dev.sh index 40a7cc1..b9fd275 100644 --- a/lib/clean/dev.sh +++ b/lib/clean/dev.sh @@ -4,19 +4,16 @@ set -euo pipefail # Clean npm cache (command + directories) +# npm cache clean clears official npm cache, safe_clean handles alternative package managers # Env: DRY_RUN clean_dev_npm() { if command -v npm > /dev/null 2>&1; then - if [[ "$DRY_RUN" != "true" ]]; then - clean_tool_cache "npm cache" npm cache clean --force - else - echo -e " ${YELLOW}→${NC} npm cache (would clean)" - fi + # clean_tool_cache now calculates size before cleanup for better statistics + clean_tool_cache "npm cache" npm cache clean --force note_activity fi - safe_clean ~/.npm/_cacache/* "npm cache directory" - safe_clean ~/.npm/_logs/* "npm logs" + # Clean alternative package manager caches safe_clean ~/.tnpm/_cacache/* "tnpm cache directory" safe_clean ~/.tnpm/_logs/* "tnpm logs" safe_clean ~/.yarn/cache/* "Yarn cache" @@ -24,19 +21,16 @@ clean_dev_npm() { } # Clean Python/pip cache (command + directories) +# pip cache purge clears official pip cache, safe_clean handles other Python tools # Env: DRY_RUN clean_dev_python() { if command -v pip3 > /dev/null 2>&1; then - if [[ "$DRY_RUN" != "true" ]]; then - clean_tool_cache "pip cache" bash -c 'pip3 cache purge >/dev/null 2>&1 || true' - else - echo -e " ${YELLOW}→${NC} pip cache (would clean)" - fi + # clean_tool_cache now calculates size before cleanup for better statistics + clean_tool_cache "pip cache" bash -c 'pip3 cache purge >/dev/null 2>&1 || true' note_activity fi - safe_clean ~/.cache/pip/* "pip cache directory" - safe_clean ~/Library/Caches/pip/* "pip cache (macOS)" + # Clean Python ecosystem caches safe_clean ~/.pyenv/cache/* "pyenv cache" safe_clean ~/.cache/poetry/* "Poetry cache" safe_clean ~/.cache/uv/* "uv cache" @@ -53,19 +47,14 @@ clean_dev_python() { } # Clean Go cache (command + directories) +# go clean handles build and module caches comprehensively # Env: DRY_RUN clean_dev_go() { if command -v go > /dev/null 2>&1; then - if [[ "$DRY_RUN" != "true" ]]; then - clean_tool_cache "Go cache" bash -c 'go clean -modcache >/dev/null 2>&1 || true; go clean -cache >/dev/null 2>&1 || true' - else - echo -e " ${YELLOW}→${NC} Go cache (would clean)" - fi + # clean_tool_cache now calculates size before cleanup for better statistics + clean_tool_cache "Go cache" bash -c 'go clean -modcache >/dev/null 2>&1 || true; go clean -cache >/dev/null 2>&1 || true' note_activity fi - - safe_clean ~/Library/Caches/go-build/* "Go build cache" - safe_clean ~/go/pkg/mod/cache/* "Go module cache" } # Clean Rust/cargo cache directories @@ -137,6 +126,9 @@ clean_dev_frontend() { } # Clean mobile development tools +# iOS simulator cleanup can free significant space (70GB+ in some cases) +# DeviceSupport files accumulate for each iOS version connected +# Simulator runtime caches can grow large over time clean_dev_mobile() { # Clean Xcode unavailable simulators # Removes old and unused local iOS simulator data from old unused runtimes @@ -147,12 +139,23 @@ clean_dev_mobile() { note_activity fi + # Clean iOS DeviceSupport - more comprehensive cleanup + # DeviceSupport directories store debug symbols for each iOS version + # Safe to clean caches and logs, but preserve device support files themselves + safe_clean ~/Library/Developer/Xcode/iOS\ DeviceSupport/*/Symbols/System/Library/Caches/* "iOS device symbol cache" + safe_clean ~/Library/Developer/Xcode/iOS\ DeviceSupport/*.log "iOS device support logs" + safe_clean ~/Library/Developer/Xcode/watchOS\ DeviceSupport/*/Symbols/System/Library/Caches/* "watchOS device symbol cache" + safe_clean ~/Library/Developer/Xcode/tvOS\ DeviceSupport/*/Symbols/System/Library/Caches/* "tvOS device symbol cache" + + # Clean simulator runtime caches + # RuntimeRoot caches can accumulate system library caches + safe_clean ~/Library/Developer/CoreSimulator/Profiles/Runtimes/*/Contents/Resources/RuntimeRoot/System/Library/Caches/* "Simulator runtime cache" + safe_clean ~/Library/Caches/Google/AndroidStudio*/* "Android Studio cache" safe_clean ~/Library/Caches/CocoaPods/* "CocoaPods cache" safe_clean ~/.cache/flutter/* "Flutter cache" safe_clean ~/.android/build-cache/* "Android build cache" safe_clean ~/.android/cache/* "Android SDK cache" - safe_clean ~/Library/Developer/Xcode/iOS\ DeviceSupport/*/Symbols/System/Library/Caches/* "iOS device cache" safe_clean ~/Library/Developer/Xcode/UserData/IB\ Support/* "Xcode Interface Builder cache" safe_clean ~/.cache/swift-package-manager/* "Swift package manager cache" } diff --git a/lib/core/common.sh b/lib/core/common.sh index 2ea12c6..4fa9cfb 100755 --- a/lib/core/common.sh +++ b/lib/core/common.sh @@ -1141,12 +1141,62 @@ with_spinner() { clean_tool_cache() { local label="$1" shift || true + + # Calculate size before cleanup for statistics + # This improves "Space freed" reporting by including command-based cleanups + local size_before=0 + local cache_dirs=() + + # Detect cache directories based on tool name + case "$label" in + "npm cache") + cache_dirs=("$HOME/.npm/_cacache" "$HOME/.npm/_logs") + ;; + "pip cache") + cache_dirs=("$HOME/.cache/pip" "$HOME/Library/Caches/pip") + ;; + "Go cache") + cache_dirs=("$HOME/Library/Caches/go-build" "$HOME/go/pkg/mod/cache") + ;; + "Docker build cache") + # Docker cache size is calculated by docker itself, skip + ;; + "Homebrew") + cache_dirs=("$HOME/Library/Caches/Homebrew") + ;; + esac + + # Calculate total size of cache directories + if [[ ${#cache_dirs[@]} -gt 0 ]]; then + for dir in "${cache_dirs[@]}"; do + if [[ -d "$dir" ]]; then + local dir_size=$(get_path_size_kb "$dir" 2>/dev/null || echo "0") + ((size_before += dir_size)) + fi + done + fi + if [[ "$DRY_RUN" == "true" ]]; then - echo -e " ${YELLOW}→${NC} $label (would clean)" + if [[ $size_before -gt 0 ]]; then + local size_human=$(bytes_to_human "$((size_before * 1024))") + echo -e " ${YELLOW}→${NC} $label ${YELLOW}($size_human dry)${NC}" + ((total_size_cleaned += size_before)) || true + ((total_items++)) || true + else + echo -e " ${YELLOW}→${NC} $label (would clean)" + fi return 0 fi + if MOLE_SPINNER_PREFIX=" " with_spinner "$label" "$@"; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} $label" + if [[ $size_before -gt 0 ]]; then + local size_human=$(bytes_to_human "$((size_before * 1024))") + echo -e " ${GREEN}${ICON_SUCCESS}${NC} $label ${GREEN}($size_human)${NC}" + ((total_size_cleaned += size_before)) || true + ((total_items++)) || true + else + echo -e " ${GREEN}${ICON_SUCCESS}${NC} $label" + fi else local exit_code=$? # Timeout returns 124, don't show error message (already shown by with_spinner)