Files
zyt/app/scripts/macos_helpers.sh
T
2026-08-10 17:29:05 +08:00

111 lines
2.9 KiB
Bash

#!/usr/bin/env bash
# Shared helpers for Finder-launched macOS entry points. This file is sourced.
MACOS_PAUSE_ON_SUCCESS=0
MACOS_UV_BIN=""
MACOS_TEMP_DIRS=()
macos_register_temp_dir() {
MACOS_TEMP_DIRS[${#MACOS_TEMP_DIRS[@]}]="$1"
}
macos_cleanup_temp_dirs() {
local directory
for directory in "${MACOS_TEMP_DIRS[@]}"; do
[[ -n "$directory" && -d "$directory" ]] || continue
case "$(basename "$directory")" in
doctor-uv.*|doctor-node.*)
rm -rf -- "$directory" || \
printf '临时目录清理失败:%s\n' "$directory" >&2
;;
*) printf '跳过不安全的临时目录清理目标:%s\n' "$directory" >&2 ;;
esac
done
}
macos_should_pause() {
[[ -t 0 && -t 1 && -z "${CI:-}" && "${DOCTOR_NONINTERACTIVE:-0}" != "1" ]]
}
macos_on_exit() {
local status="$1"
trap - EXIT
macos_cleanup_temp_dirs
if [[ "$status" -ne 0 ]]; then
printf '\n操作失败(退出码 %s)。请查看上方信息。\n' "$status" >&2
fi
if macos_should_pause && { [[ "$status" -ne 0 ]] || [[ "$MACOS_PAUSE_ON_SUCCESS" == "1" ]]; }; then
printf '\n按回车键关闭此窗口…'
IFS= read -r _ || true
fi
exit "$status"
}
macos_install_exit_trap() {
MACOS_PAUSE_ON_SUCCESS="${1:-0}"
trap 'macos_on_exit "$?"' EXIT
}
macos_die() {
printf '错误:%s\n' "$1" >&2
exit 1
}
macos_require_darwin() {
if [[ "$(uname -s)" != "Darwin" ]]; then
macos_die "此入口只能在 macOS 上运行。"
fi
}
macos_locate_uv() {
local candidate
candidate="$(command -v uv 2>/dev/null || true)"
if [[ -n "$candidate" && -x "$candidate" ]]; then
MACOS_UV_BIN="$candidate"
return 0
fi
if [[ -n "${HOME:-}" ]]; then
for candidate in "$HOME/.local/bin/uv" "$HOME/.cargo/bin/uv"; do
if [[ -x "$candidate" ]]; then
MACOS_UV_BIN="$candidate"
return 0
fi
done
fi
for candidate in /opt/homebrew/bin/uv /usr/local/bin/uv; do
if [[ -x "$candidate" ]]; then
MACOS_UV_BIN="$candidate"
return 0
fi
done
return 1
}
macos_ensure_uv() {
local curl_bin installer temp_root
if macos_locate_uv; then
return 0
fi
curl_bin="$(command -v curl 2>/dev/null || true)"
[[ -n "$curl_bin" ]] || macos_die "未找到 uv,也未找到用于安装 uv 的 curl。"
temp_root="${TMPDIR:-/tmp}"
temp_root="${temp_root%/}"
[[ -n "$temp_root" ]] || temp_root="/"
installer="$(mktemp -d "$temp_root/doctor-uv.XXXXXX")"
macos_register_temp_dir "$installer"
printf '未检测到 uv,正在通过官方 HTTPS 安装器安装…\n' >&2
if ! "$curl_bin" --proto '=https' --tlsv1.2 -fsSL \
'https://astral.sh/uv/install.sh' -o "$installer/install.sh"; then
macos_die "uv 安装器下载失败,请检查网络后重试。"
fi
if ! UV_NO_MODIFY_PATH=1 /bin/sh "$installer/install.sh"; then
macos_die "uv 安装失败。"
fi
hash -r
macos_locate_uv || macos_die "uv 已安装,但未在标准位置找到可执行文件。"
}