🐛 fix(core): 容忍 Cask 升级时的瞬时下载失败并优化错误校验逻辑

This commit is contained in:
2026-06-02 08:56:34 +08:00
parent 9b8fd9f136
commit e6861f53ef
2 changed files with 40 additions and 2 deletions
+13
View File
@@ -18,6 +18,7 @@
- 检查并安装 `buo/cask-upgrade` tap,保留 GUI 应用升级相关兼容能力。
- 使用 `brew upgrade --formula` 升级命令行工具。
- 使用 `brew upgrade --cask --greedy --force` 强制升级 GUI 应用。
- 当 Cask 升级命令因瞬时下载错误返回失败,但复核确认已无过期 Cask 时,继续执行后续清理。
- 执行 `brew cleanup --prune=all` 清理旧版本和缓存。
- 支持固定终端宽度,避免非交互环境下输出宽度异常。
- 启动器支持通过 macOS Keychain 保存并读取 sudo 密码,用于 `sudo -A -v` 预刷新 sudo 凭据。
@@ -227,6 +228,18 @@ Warning: 'brew doctor' detected issues. Manual review and resolution are recomme
source ~/.zshrc
```
### Cask 下载曾经报错但最终升级完成
`brew upgrade --cask --greedy --force` 有时会在下载阶段出现 `curl: (18) Transferred a partial file` 之类的瞬时错误,随后 Homebrew 又重试并完成安装。此时命令仍可能返回非零退出码。
脚本会在 Cask 升级命令失败后执行一次:
```bash
brew outdated --cask --greedy
```
如果没有剩余过期 Cask,脚本会把前一次错误视为瞬时失败并继续执行 `brew cleanup --prune=all`。如果仍有过期 Cask,脚本会保留失败退出状态,方便发现真实失败项。
## 注意事项
- 脚本启用了 `set -e` 和 `set -o pipefail`,关键命令失败会终止流程。
+27 -2
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Homebrew 智能升级脚本(强制 Cask 更新增强版 v5.3 - 移除 sudo 密码注入
# Homebrew 智能升级脚本(强制 Cask 更新增强版 v5.4 - 容忍 Cask 瞬时下载失败
# ================== 脚本环境设置 ==================
@@ -46,6 +46,31 @@ terminal_width() {
separator() { local width; width=$(terminal_width); printf '=%.0s' $(seq 1 "$width"); printf "\n"; }
print_header() { echo -e "${BLUE}$1${NC}"; }
run_cask_upgrade() {
local status=0 outdated_casks
brew upgrade --cask --greedy --force || status=$?
if [[ "$status" -eq 0 ]]; then
return 0
fi
echo -e "${YELLOW}Warning: Cask upgrade reported a non-zero exit code (${status}). Verifying whether outdated casks remain...${NC}"
if ! outdated_casks="$(brew outdated --cask --greedy 2>/dev/null)"; then
echo -e "${YELLOW}Warning: Unable to verify outdated casks after the failed upgrade command.${NC}"
return "$status"
fi
if [[ -z "$outdated_casks" ]]; then
echo -e "${YELLOW}Warning: No outdated casks remain. Treating the previous Cask error as transient and continuing.${NC}"
return 0
fi
echo -e "${YELLOW}The following casks still appear to be outdated:${NC}"
printf '%s\n' "$outdated_casks"
return "$status"
}
# ================== 流程开始 ==================
separator
@@ -94,7 +119,7 @@ export HOMEBREW_COLOR=1
export COLUMNS
COLUMNS="$(terminal_width)"
brew upgrade --cask --greedy --force
run_cask_upgrade
separator
printf "\n"