From 25cba0cf38cc75db6cbdb2fd8d91dd5db10b6723 Mon Sep 17 00:00:00 2001 From: tw93 Date: Tue, 3 Mar 2026 16:16:52 +0800 Subject: [PATCH] refactor(clean): simplify Xcode DeviceSupport cleanup logic - Use array slicing instead of manual loop for splitting dirs - Remove redundant early returns with duplicated safe_clean calls - Consolidate cache/logs cleanup to single location at function end --- lib/clean/dev.sh | 93 ++++++++++++++++++------------------------------ 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/lib/clean/dev.sh b/lib/clean/dev.sh index c76db4c..25ae109 100644 --- a/lib/clean/dev.sh +++ b/lib/clean/dev.sh @@ -359,77 +359,52 @@ clean_xcode_device_support() { version_dirs+=("$entry") done < <(command find "$ds_dir" -mindepth 1 -maxdepth 1 -print0 2> /dev/null) - if [[ ${#version_dirs[@]} -le $keep_count ]]; then - # Nothing to remove, still clean caches/logs inside existing versions - safe_clean "$ds_dir"/*/Symbols/System/Library/Caches/* "$display_name symbol cache" - safe_clean "$ds_dir"/*.log "$display_name logs" - return 0 - fi - # Sort by modification time (most recent first) local -a sorted_dirs=() while IFS= read -r line; do sorted_dirs+=("${line#* }") done < <( for entry in "${version_dirs[@]}"; do - local mtime - mtime=$(stat -f%m "$entry" 2> /dev/null || echo "0") - printf '%s %s\n' "$mtime" "$entry" + printf '%s %s\n' "$(stat -f%m "$entry" 2> /dev/null || echo 0)" "$entry" done | sort -rn ) - # Split into keep vs remove - local -a stale_dirs=() - local idx=0 - for entry in "${sorted_dirs[@]}"; do - if [[ $idx -lt $keep_count ]]; then - ((idx++)) || true - continue - fi - stale_dirs+=("$entry") - ((idx++)) || true - done + # Get stale versions (everything after keep_count) + local -a stale_dirs=("${sorted_dirs[@]:$keep_count}") - if [[ ${#stale_dirs[@]} -eq 0 ]]; then - safe_clean "$ds_dir"/*/Symbols/System/Library/Caches/* "$display_name symbol cache" - safe_clean "$ds_dir"/*.log "$display_name logs" - return 0 + if [[ ${#stale_dirs[@]} -gt 0 ]]; then + # Calculate total size of stale versions + local stale_size_kb=0 entry_size_kb + for stale_entry in "${stale_dirs[@]}"; do + entry_size_kb=$(get_path_size_kb "$stale_entry" 2> /dev/null || echo 0) + stale_size_kb=$((stale_size_kb + entry_size_kb)) + done + local stale_size_human + stale_size_human=$(bytes_to_human "$((stale_size_kb * 1024))") + + if [[ "$DRY_RUN" == "true" ]]; then + echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} ${display_name} · would remove ${#stale_dirs[@]} old versions (${stale_size_human}), keeping ${keep_count} most recent" + note_activity + else + # Remove old versions + local removed_count=0 + for stale_entry in "${stale_dirs[@]}"; do + if should_protect_path "$stale_entry" || is_path_whitelisted "$stale_entry"; then + continue + fi + if safe_remove "$stale_entry"; then + removed_count=$((removed_count + 1)) + fi + done + + if [[ $removed_count -gt 0 ]]; then + echo -e " ${GREEN}${ICON_SUCCESS}${NC} ${display_name} · removed ${removed_count} old versions, ${stale_size_human}" + note_activity + fi + fi fi - # Calculate total size of stale versions - local stale_size_kb=0 - local stale_entry - for stale_entry in "${stale_dirs[@]}"; do - local entry_size_kb - entry_size_kb=$(get_path_size_kb "$stale_entry" 2> /dev/null || echo "0") - stale_size_kb=$((stale_size_kb + entry_size_kb)) - done - local stale_size_human - stale_size_human=$(bytes_to_human "$((stale_size_kb * 1024))") - - if [[ "$DRY_RUN" == "true" ]]; then - echo -e " ${YELLOW}${ICON_DRY_RUN}${NC} ${display_name} · would remove ${#stale_dirs[@]} old versions (${stale_size_human}), keeping ${keep_count} most recent" - note_activity - return 0 - fi - - # Remove old versions - local removed_count=0 - for stale_entry in "${stale_dirs[@]}"; do - if should_protect_path "$stale_entry" || is_path_whitelisted "$stale_entry"; then - continue - fi - if safe_remove "$stale_entry"; then - removed_count=$((removed_count + 1)) - fi - done - - if [[ $removed_count -gt 0 ]]; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} ${display_name} · removed ${removed_count} old versions, ${stale_size_human}" - note_activity - fi - - # Still clean caches/logs inside the kept versions + # Clean caches/logs inside kept versions safe_clean "$ds_dir"/*/Symbols/System/Library/Caches/* "$display_name symbol cache" safe_clean "$ds_dir"/*.log "$display_name logs" }