#!/usr/bin/env bash set -euo pipefail APP_PATH="${1:-${ZCODE_MACOS_RELEASE_APP_PATH:-/Applications/ZCode.app}}" # 安装包身份与后端环境分轴:ZCODE_PREVIEW_IDENTITY=1 让生产后端的构建仍是 ZCode Preview。 # 只认 "1",与 CI workflow / release 门的精确比较同一套语义(其它拼写一律视为未开启)。 is_preview_identity_requested() { [[ "${ZCODE_PREVIEW_IDENTITY:-}" = "1" ]] } APP_BUNDLE_NAME="$(basename "$APP_PATH")" APP_DISPLAY_NAME="${APP_BUNDLE_NAME%.app}" APP_EXECUTABLE_NAME="${ZCODE_APP_EXECUTABLE_NAME:-$APP_DISPLAY_NAME}" if [ "${APP_PATH:-}" = "--help" ] || [ "${APP_PATH:-}" = "-h" ]; then cat <<'USAGE' Usage: bash scripts/doctor-macos-release-app.sh /Applications/ZCode.app ZCODE_MACOS_RELEASE_APP_PATH=/Applications/ZCode.app pnpm run doctor:macos-release Always validates the installed macOS release app with: codesign --verify --deep --strict spctl -a -vv -t exec USAGE exit 0 fi require_command() { local command_name="$1" if ! command -v "$command_name" >/dev/null 2>&1; then echo "[macos-release-doctor] missing required command: $command_name" >&2 exit 1 fi } assert_bundle_dir() { local label="$1" local bundle_path="$2" if [ -z "$bundle_path" ]; then echo "[macos-release-doctor] ${label} missing: " >&2 exit 1 fi if [ ! -d "$bundle_path" ]; then echo "[macos-release-doctor] ${label} missing: $bundle_path" >&2 exit 1 fi } assert_executable() { local label="$1" local executable_path="$2" if [ ! -x "$executable_path" ]; then echo "[macos-release-doctor] ${label} executable missing or not executable: $executable_path" >&2 exit 1 fi } assert_macho_executable() { local label="$1" local executable_path="$2" local magic magic="$(od -An -N4 -tx1 -v "$executable_path" | tr -d ' \n')" case "$magic" in feedface|feedfacf|cafebabe|cafebabf|cffaedfe|cefaedfe|bebafeca|bfbafeca) ;; *) echo "[macos-release-doctor] ${label} is not a Mach-O executable: $executable_path" >&2 exit 1 ;; esac } run_quiet_validation() { local label="$1" shift local output_file output_file="$(mktemp "${TMPDIR:-/tmp}/zcode-macos-release-doctor.XXXXXX")" if "$@" >"$output_file" 2>&1; then rm -f "$output_file" return 0 fi echo "[macos-release-doctor] ${label} failed" >&2 cat "$output_file" >&2 rm -f "$output_file" return 1 } validate_release_bundle() { local label="$1" local bundle_path="$2" local require_notarization_staple="${3:-0}" echo "[macos-release-doctor] validating ${label}: $bundle_path" run_quiet_validation "${label} codesign verify" codesign --verify --deep --strict "$bundle_path" run_quiet_validation "${label} Gatekeeper exec assessment" spctl -a -vv -t exec "$bundle_path" if [ "$require_notarization_staple" = "1" ]; then # xcrun 只用于 macOS staple 验收,等目标 bundle 存在且基础签名校验通过后再检查, # 避免缺少 xcrun 的报错掩盖 bundle 缺失或签名错误。 require_command xcrun # 仅检查 spctl 可能依赖联网取票或缓存,仍需验证目标 bundle 的本地 staple。 run_quiet_validation "${label} notarization staple" xcrun stapler validate "$bundle_path" fi } require_command codesign require_command spctl # 过去 release/notarization 成功只说明 DMG 通过了 gate,不能证明安装后的主 app # 能被 Gatekeeper 以 exec 类型放行。这里先 fail-closed 检查 bundle 结构和主可执行 # 文件,再跑 codesign/spctl,避免安装不完整或签名损坏时误报发布成功。 assert_bundle_dir "$APP_BUNDLE_NAME" "$APP_PATH" assert_executable "$APP_BUNDLE_NAME" "$APP_PATH/Contents/MacOS/$APP_EXECUTABLE_NAME" validate_release_bundle "$APP_BUNDLE_NAME" "$APP_PATH" echo "[macos-release-doctor] done"