From 61644caf92928c32ef7b43c9a18613ed458e8efa Mon Sep 17 00:00:00 2001 From: Tw93 Date: Tue, 2 Dec 2025 10:58:40 +0800 Subject: [PATCH] Uninstall supports multi-level directory search --- bin/uninstall.sh | 237 +---------------------- lib/{uninstall.sh => uninstall/batch.sh} | 115 +++++++++-- tests/uninstall.bats | 10 +- 3 files changed, 110 insertions(+), 252 deletions(-) rename lib/{uninstall.sh => uninstall/batch.sh} (73%) diff --git a/bin/uninstall.sh b/bin/uninstall.sh index f38d850..0f79c72 100755 --- a/bin/uninstall.sh +++ b/bin/uninstall.sh @@ -17,7 +17,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/../lib/core/common.sh" source "$SCRIPT_DIR/../lib/ui/menu_paginated.sh" source "$SCRIPT_DIR/../lib/ui/app_selector.sh" -source "$SCRIPT_DIR/../lib/uninstall.sh" +source "$SCRIPT_DIR/../lib/uninstall/batch.sh" # Note: Bundle preservation logic is now in lib/core/common.sh @@ -29,37 +29,6 @@ total_items=0 files_cleaned=0 total_size_cleaned=0 -# Get app last used date in human readable format -get_app_last_used() { - local app_path="$1" - local last_used - last_used=$(mdls -name kMDItemLastUsedDate -raw "$app_path" 2> /dev/null) - - if [[ "$last_used" == "(null)" || -z "$last_used" ]]; then - echo "Never" - else - local last_used_epoch - last_used_epoch=$(date -j -f "%Y-%m-%d %H:%M:%S %z" "$last_used" "+%s" 2> /dev/null) - local current_epoch - current_epoch=$(date "+%s") - local days_ago=$(((current_epoch - last_used_epoch) / 86400)) - - if [[ $days_ago -eq 0 ]]; then - echo "Today" - elif [[ $days_ago -eq 1 ]]; then - echo "Yesterday" - elif [[ $days_ago -lt 30 ]]; then - echo "${days_ago} days ago" - elif [[ $days_ago -lt 365 ]]; then - local months_ago=$((days_ago / 30)) - echo "${months_ago} month(s) ago" - else - local years_ago=$((days_ago / 365)) - echo "${years_ago} year(s) ago" - fi - fi -} - # Compact the "last used" descriptor for aligned summaries format_last_used_summary() { local value="$1" @@ -213,8 +182,9 @@ scan_applications() { app_data_tuples+=("${app_path}|${app_name}|${bundle_id}|${display_name}") done < <( # Scan both system and user application directories - find /Applications -name "*.app" -maxdepth 1 -print0 2> /dev/null - find ~/Applications -name "*.app" -maxdepth 1 -print0 2> /dev/null + # Using maxdepth 3 to find apps in subdirectories (e.g., Adobe apps in /Applications/Adobe X/) + find /Applications -name "*.app" -maxdepth 3 -print0 2> /dev/null + find ~/Applications -name "*.app" -maxdepth 3 -print0 2> /dev/null ) # Second pass: process each app with parallel size calculation @@ -399,205 +369,6 @@ load_applications() { return 0 } -# Old display_apps function removed - replaced by new menu system - -# Read a single key with proper escape sequence handling -# This function has been replaced by the menu.sh library - -# Note: App file discovery and size calculation functions moved to lib/core/common.sh -# Use find_app_files() and calculate_total_size() from common.sh - -# Uninstall selected applications -uninstall_applications() { - local total_size_freed=0 - - echo "" - echo -e "${PURPLE}${ICON_ARROW} Uninstalling selected applications${NC}" - - if [[ ${#selected_apps[@]} -eq 0 ]]; then - log_warning "No applications selected for uninstallation" - return 0 - fi - - for selected_app in "${selected_apps[@]}"; do - IFS='|' read -r epoch app_path app_name bundle_id size last_used <<< "$selected_app" - - echo "" - - # Check if app is running (use app path for precise matching) - if pgrep -f "$app_path" > /dev/null 2>&1; then - echo -e "${YELLOW}${ICON_ERROR} $app_name is currently running${NC}" - read -p " Force quit $app_name? (y/N): " -n 1 -r - echo - if [[ $REPLY =~ ^[Yy]$ ]]; then - # Retry kill operation with verification to avoid TOCTOU - local retry=0 - while [[ $retry -lt 3 ]]; do - pkill -f "$app_path" 2> /dev/null || true - sleep 1 - # Verify app was killed - if ! pgrep -f "$app_path" > /dev/null 2>&1; then - break - fi - ((retry++)) - done - - # Final check - if pgrep -f "$app_path" > /dev/null 2>&1; then - log_warning "Failed to quit $app_name after $retry attempts" - fi - else - echo -e " ${BLUE}${ICON_EMPTY}${NC} Skipped $app_name" - continue - fi - fi - - # Find related files (user-level) - local related_files - related_files=$(find_app_files "$bundle_id" "$app_name") - - # Find system-level files (requires sudo) - local system_files - system_files=$(find_app_system_files "$bundle_id" "$app_name") - - # Calculate total size - local app_size_kb - app_size_kb=$(du -sk "$app_path" 2> /dev/null | awk '{print $1}' || echo "0") - local related_size_kb - related_size_kb=$(calculate_total_size "$related_files") - local system_size_kb - system_size_kb=$(calculate_total_size "$system_files") - local total_kb=$((app_size_kb + related_size_kb + system_size_kb)) - - # Show what will be removed - echo -e "${BLUE}${ICON_CONFIRM}${NC} $app_name - Files to be removed:" - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Application: $(echo "$app_path" | sed "s|$HOME|~|")" - - # Show user-level files - while IFS= read -r file; do - [[ -n "$file" && -e "$file" ]] && echo -e " ${GREEN}${ICON_SUCCESS}${NC} $(echo "$file" | sed "s|$HOME|~|")" - done <<< "$related_files" - - # Show system-level files - if [[ -n "$system_files" ]]; then - while IFS= read -r file; do - [[ -n "$file" && -e "$file" ]] && echo -e " ${BLUE}${ICON_SOLID}${NC} System: $file" - done <<< "$system_files" - fi - - local size_display - if [[ $total_kb -gt 1048576 ]]; then # > 1GB - size_display=$(echo "$total_kb" | awk '{printf "%.2fGB", $1/1024/1024}') - elif [[ $total_kb -gt 1024 ]]; then # > 1MB - size_display=$(echo "$total_kb" | awk '{printf "%.1fMB", $1/1024}') - else - size_display="${total_kb}KB" - fi - - echo -e " ${BLUE}Total size: $size_display${NC}" - echo - - read -p " Proceed with uninstalling $app_name? (y/N): " -n 1 -r - echo - - if [[ $REPLY =~ ^[Yy]$ ]]; then - # Stop Launch Agents and Daemons before removal - # User-level Launch Agents - for plist in ~/Library/LaunchAgents/"$bundle_id"*.plist; do - if [[ -f "$plist" ]]; then - launchctl unload "$plist" 2> /dev/null || true - fi - done - # System-level Launch Agents - for plist in /Library/LaunchAgents/"$bundle_id"*.plist; do - if [[ -f "$plist" ]]; then - sudo launchctl unload "$plist" 2> /dev/null || true - fi - done - # System-level Launch Daemons - for plist in /Library/LaunchDaemons/"$bundle_id"*.plist; do - if [[ -f "$plist" ]]; then - sudo launchctl unload "$plist" 2> /dev/null || true - fi - done - - # Remove the application - if safe_remove "$app_path" true; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Removed application" - else - echo -e " ${RED}${ICON_ERROR}${NC} Failed to remove $app_path" - continue - fi - - # Remove user-level related files - while IFS= read -r file; do - if [[ -n "$file" && -e "$file" ]]; then - # Handle symbolic links separately (only remove the link, not the target) - if [[ -L "$file" ]]; then - if rm "$file" 2> /dev/null; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Removed $(echo "$file" | sed "s|$HOME|~|" | xargs basename)" - fi - else - if safe_remove "$file" true; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Removed $(echo "$file" | sed "s|$HOME|~|" | xargs basename)" - fi - fi - fi - done <<< "$related_files" - - # Remove system-level files (requires sudo) - if [[ -n "$system_files" ]]; then - echo -e " ${BLUE}${ICON_SOLID}${NC} Admin access required for system files" - while IFS= read -r file; do - if [[ -n "$file" && -e "$file" ]]; then - # Handle symbolic links separately (only remove the link, not the target) - if [[ -L "$file" ]]; then - if sudo rm "$file" 2> /dev/null; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Removed $(basename "$file")" - else - echo -e " ${YELLOW}${ICON_ERROR}${NC} Failed to remove: $file" - fi - else - if safe_sudo_remove "$file"; then - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Removed $(basename "$file")" - else - echo -e " ${YELLOW}${ICON_ERROR}${NC} Failed to remove: $file" - fi - fi - fi - done <<< "$system_files" - fi - - ((total_size_freed += total_kb)) - ((files_cleaned++)) - ((total_items++)) - - echo -e " ${GREEN}${ICON_SUCCESS}${NC} $app_name uninstalled successfully" - else - echo -e " ${BLUE}${ICON_EMPTY}${NC} Skipped $app_name" - fi - done - - # Show final summary - echo -e "${PURPLE}${ICON_ARROW} Uninstallation Summary${NC}" - - if [[ $total_size_freed -gt 0 ]]; then - local freed_display - if [[ $total_size_freed -gt 1048576 ]]; then # > 1GB - freed_display=$(echo "$total_size_freed" | awk '{printf "%.2fGB", $1/1024/1024}') - elif [[ $total_size_freed -gt 1024 ]]; then # > 1MB - freed_display=$(echo "$total_size_freed" | awk '{printf "%.1fMB", $1/1024}') - else - freed_display="${total_size_freed}KB" - fi - - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Freed $freed_display of disk space" - fi - - echo -e " ${GREEN}${ICON_SUCCESS}${NC} Applications uninstalled: $files_cleaned" - ((total_size_cleaned += total_size_freed)) -} - # Cleanup function - restore cursor and clean up cleanup() { # Restore cursor using common function diff --git a/lib/uninstall.sh b/lib/uninstall/batch.sh similarity index 73% rename from lib/uninstall.sh rename to lib/uninstall/batch.sh index d893902..3ebdfd3 100755 --- a/lib/uninstall.sh +++ b/lib/uninstall/batch.sh @@ -44,6 +44,59 @@ decode_file_list() { } # Note: find_app_files() and calculate_total_size() functions now in lib/core/common.sh +# Stop Launch Agents and Daemons for an app +# Args: $1 = bundle_id, $2 = has_system_files (true/false) +stop_launch_services() { + local bundle_id="$1" + local has_system_files="${2:-false}" + + # User-level Launch Agents + for plist in ~/Library/LaunchAgents/"$bundle_id"*.plist; do + [[ -f "$plist" ]] && launchctl unload "$plist" 2> /dev/null || true + done + + # System-level services (requires sudo) + if [[ "$has_system_files" == "true" ]]; then + for plist in /Library/LaunchAgents/"$bundle_id"*.plist; do + [[ -f "$plist" ]] && sudo launchctl unload "$plist" 2> /dev/null || true + done + for plist in /Library/LaunchDaemons/"$bundle_id"*.plist; do + [[ -f "$plist" ]] && sudo launchctl unload "$plist" 2> /dev/null || true + done + fi +} + +# Remove a list of files (handles both regular files and symlinks) +# Args: $1 = file_list (newline-separated), $2 = use_sudo (true/false) +# Returns: number of files removed +remove_file_list() { + local file_list="$1" + local use_sudo="${2:-false}" + local count=0 + + while IFS= read -r file; do + [[ -n "$file" && -e "$file" ]] || continue + + if [[ -L "$file" ]]; then + # Symlink: use direct rm + if [[ "$use_sudo" == "true" ]]; then + sudo rm "$file" 2> /dev/null && ((count++)) || true + else + rm "$file" 2> /dev/null && ((count++)) || true + fi + else + # Regular file/directory: use safe_remove + if [[ "$use_sudo" == "true" ]]; then + safe_sudo_remove "$file" && ((count++)) || true + else + safe_remove "$file" true && ((count++)) || true + fi + fi + done <<< "$file_list" + + echo "$count" +} + # Batch uninstall with single confirmation batch_uninstall_applications() { local total_size_freed=0 @@ -75,18 +128,27 @@ batch_uninstall_applications() { sudo_apps+=("$app_name") fi - # Calculate size for summary + # Calculate size for summary (including system files) local app_size_kb=$(du -sk "$app_path" 2> /dev/null | awk '{print $1}' || echo "0") local related_files=$(find_app_files "$bundle_id" "$app_name") local related_size_kb=$(calculate_total_size "$related_files") - local total_kb=$((app_size_kb + related_size_kb)) + local system_files=$(find_app_system_files "$bundle_id" "$app_name") + local system_size_kb=$(calculate_total_size "$system_files") + local total_kb=$((app_size_kb + related_size_kb + system_size_kb)) ((total_estimated_size += total_kb)) + # Check if system files require sudo + if [[ -n "$system_files" ]]; then + sudo_apps+=("$app_name") + fi + # Store details for later use - # Base64 encode related_files to handle multi-line data safely (single line) + # Base64 encode file lists to handle multi-line data safely (single line) local encoded_files encoded_files=$(printf '%s' "$related_files" | base64 | tr -d '\n') - app_details+=("$app_name|$app_path|$bundle_id|$total_kb|$encoded_files") + local encoded_system_files + encoded_system_files=$(printf '%s' "$system_files" | base64 | tr -d '\n') + app_details+=("$app_name|$app_path|$bundle_id|$total_kb|$encoded_files|$encoded_system_files") done # Format size display (convert KB to bytes for bytes_to_human()) @@ -97,8 +159,9 @@ batch_uninstall_applications() { echo -e "${PURPLE}Files to be removed:${NC}" echo "" for detail in "${app_details[@]}"; do - IFS='|' read -r app_name app_path bundle_id total_kb encoded_files <<< "$detail" + IFS='|' read -r app_name app_path bundle_id total_kb encoded_files encoded_system_files <<< "$detail" local related_files=$(decode_file_list "$encoded_files" "$app_name") + local system_files=$(decode_file_list "$encoded_system_files" "$app_name") local app_size_display=$(bytes_to_human "$((total_kb * 1024))") echo -e "${BLUE}${ICON_CONFIRM}${NC} ${app_name} ${GRAY}(${app_size_display})${NC}" @@ -116,10 +179,22 @@ batch_uninstall_applications() { fi done <<< "$related_files" + # Show system files + local sys_file_count=0 + while IFS= read -r file; do + if [[ -n "$file" && -e "$file" ]]; then + if [[ $sys_file_count -lt $max_files ]]; then + echo -e " ${BLUE}${ICON_SOLID}${NC} System: $file" + fi + ((sys_file_count++)) + fi + done <<< "$system_files" + # Show count of remaining files if truncated - if [[ $file_count -gt $max_files ]]; then - local remaining=$((file_count - max_files)) - echo -e " ${GRAY} ... and ${remaining} more files${NC}" + local total_hidden=$((file_count > max_files ? file_count - max_files : 0)) + ((total_hidden += sys_file_count > max_files ? sys_file_count - max_files : 0)) + if [[ $total_hidden -gt 0 ]]; then + echo -e " ${GRAY} ... and ${total_hidden} more files${NC}" fi done @@ -189,14 +264,24 @@ batch_uninstall_applications() { local -a failed_items=() local -a success_items=() for detail in "${app_details[@]}"; do - IFS='|' read -r app_name app_path bundle_id total_kb encoded_files <<< "$detail" + IFS='|' read -r app_name app_path bundle_id total_kb encoded_files encoded_system_files <<< "$detail" local related_files=$(decode_file_list "$encoded_files" "$app_name") + local system_files=$(decode_file_list "$encoded_system_files" "$app_name") local reason="" local needs_sudo=false [[ ! -w "$(dirname "$app_path")" || "$(get_file_owner "$app_path")" == "root" ]] && needs_sudo=true + + # Stop Launch Agents and Daemons before removal + local has_system_files="false" + [[ -n "$system_files" ]] && has_system_files="true" + stop_launch_services "$bundle_id" "$has_system_files" + + # Force quit app if still running if ! force_kill_app "$app_name" "$app_path"; then reason="still running" fi + + # Remove the application if [[ -z "$reason" ]]; then if [[ "$needs_sudo" == true ]]; then safe_sudo_remove "$app_path" || reason="remove failed" @@ -204,12 +289,14 @@ batch_uninstall_applications() { safe_remove "$app_path" true || reason="remove failed" fi fi + + # Remove user-level and system-level files if [[ -z "$reason" ]]; then - local files_removed=0 - while IFS= read -r file; do - [[ -n "$file" && -e "$file" ]] || continue - safe_remove "$file" true && ((files_removed++)) || true - done <<< "$related_files" + # Remove user-level files + remove_file_list "$related_files" "false" > /dev/null + # Remove system-level files (requires sudo) + remove_file_list "$system_files" "true" > /dev/null + ((total_size_freed += total_kb)) ((success_count++)) ((files_cleaned++)) diff --git a/tests/uninstall.bats b/tests/uninstall.bats index a7ad6cb..39a2be0 100644 --- a/tests/uninstall.bats +++ b/tests/uninstall.bats @@ -78,7 +78,7 @@ EOF run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' set -euo pipefail source "$PROJECT_ROOT/lib/core/common.sh" -source "$PROJECT_ROOT/lib/uninstall.sh" +source "$PROJECT_ROOT/lib/uninstall/batch.sh" # Test stubs request_sudo_access() { return 0; } @@ -120,7 +120,7 @@ EOF run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' set -euo pipefail source "$PROJECT_ROOT/lib/core/common.sh" -source "$PROJECT_ROOT/lib/uninstall.sh" +source "$PROJECT_ROOT/lib/uninstall/batch.sh" # Valid base64 encoded path list valid_data=$(printf '/path/one\n/path/two' | base64) @@ -135,7 +135,7 @@ EOF run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' set -euo pipefail source "$PROJECT_ROOT/lib/core/common.sh" -source "$PROJECT_ROOT/lib/uninstall.sh" +source "$PROJECT_ROOT/lib/uninstall/batch.sh" # Invalid base64 - function should return empty and fail if result=$(decode_file_list "not-valid-base64!!!" "TestApp" 2>/dev/null); then @@ -154,7 +154,7 @@ EOF run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' set -euo pipefail source "$PROJECT_ROOT/lib/core/common.sh" -source "$PROJECT_ROOT/lib/uninstall.sh" +source "$PROJECT_ROOT/lib/uninstall/batch.sh" # Empty base64 empty_data=$(printf '' | base64) @@ -170,7 +170,7 @@ EOF run env HOME="$HOME" PROJECT_ROOT="$PROJECT_ROOT" bash --noprofile --norc << 'EOF' set -euo pipefail source "$PROJECT_ROOT/lib/core/common.sh" -source "$PROJECT_ROOT/lib/uninstall.sh" +source "$PROJECT_ROOT/lib/uninstall/batch.sh" # Relative path - function should reject it bad_data=$(printf 'relative/path' | base64)