1
0
mirror of https://github.com/tw93/Mole.git synced 2026-03-22 20:50:06 +00:00

fix brew uninstall fallback consistency

This commit is contained in:
Tw93
2026-03-17 16:39:42 +08:00
parent e1f427c9aa
commit 6c53906427
3 changed files with 164 additions and 8 deletions

View File

@@ -530,15 +530,35 @@ batch_uninstall_applications() {
if brew_uninstall_cask "$cask_name" "$app_path"; then
used_brew_successfully=true
else
# Fallback to manual removal if brew fails
if [[ "$needs_sudo" == true ]]; then
if ! safe_sudo_remove "$app_path"; then
reason="brew failed, manual removal failed"
# Only fall back to manual app removal when Homebrew no longer
# tracks the cask. Otherwise we would recreate the mismatch
# where brew still reports the app as installed after Mole
# removes the bundle manually.
local cask_state=2
if command -v is_brew_cask_installed > /dev/null 2>&1; then
if is_brew_cask_installed "$cask_name"; then
cask_state=0
else
cask_state=$?
fi
fi
if [[ $cask_state -eq 1 ]]; then
if [[ "$needs_sudo" == true ]]; then
if ! safe_sudo_remove "$app_path"; then
reason="brew cleanup incomplete, manual removal failed"
fi
else
if ! safe_remove "$app_path" true; then
reason="brew cleanup incomplete, manual removal failed"
fi
fi
elif [[ $cask_state -eq 0 ]]; then
reason="brew uninstall failed, package still installed"
suggestion="Run brew uninstall --cask --zap $cask_name"
else
if ! safe_remove "$app_path" true; then
reason="brew failed, manual removal failed"
fi
reason="brew uninstall failed, package state unknown"
suggestion="Run brew uninstall --cask --zap $cask_name"
fi
fi
elif [[ "$needs_sudo" == true ]]; then

View File

@@ -35,6 +35,21 @@ is_homebrew_available() {
command -v brew > /dev/null 2>&1
}
# Check whether a cask is still recorded as installed in Homebrew.
# Exit codes:
# 0 - cask is installed
# 1 - cask is not installed
# 2 - install state could not be determined
is_brew_cask_installed() {
local cask_name="$1"
[[ -n "$cask_name" ]] || return 2
is_homebrew_available || return 2
local cask_list
cask_list=$(HOMEBREW_NO_ENV_HINTS=1 brew list --cask 2> /dev/null) || return 2
grep -qxF "$cask_name" <<< "$cask_list"
}
# Extract cask token from a Caskroom path
# Args: $1 - path (must be inside Caskroom)
# Prints: cask token to stdout
@@ -211,7 +226,12 @@ brew_uninstall_cask() {
# Verify removal (only if not timed out)
local cask_gone=true app_gone=true
HOMEBREW_NO_ENV_HINTS=1 brew list --cask 2> /dev/null | grep -qxF "$cask_name" && cask_gone=false
if is_brew_cask_installed "$cask_name"; then
cask_gone=false
else
local cask_state=$?
[[ $cask_state -eq 1 ]] || cask_gone=false
fi
[[ -n "$app_path" && -e "$app_path" ]] && app_gone=false
# Success: uninstall worked and both are gone, or already uninstalled