doctor-macos-release-app.sh 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP_PATH="${1:-${ZCODE_MACOS_RELEASE_APP_PATH:-/Applications/ZCode.app}}"
  4. # 安装包身份与后端环境分轴:ZCODE_PREVIEW_IDENTITY=1 让生产后端的构建仍是 ZCode Preview。
  5. # 只认 "1",与 CI workflow / release 门的精确比较同一套语义(其它拼写一律视为未开启)。
  6. is_preview_identity_requested() {
  7. [[ "${ZCODE_PREVIEW_IDENTITY:-}" = "1" ]]
  8. }
  9. APP_BUNDLE_NAME="$(basename "$APP_PATH")"
  10. APP_DISPLAY_NAME="${APP_BUNDLE_NAME%.app}"
  11. APP_EXECUTABLE_NAME="${ZCODE_APP_EXECUTABLE_NAME:-$APP_DISPLAY_NAME}"
  12. if [ "${APP_PATH:-}" = "--help" ] || [ "${APP_PATH:-}" = "-h" ]; then
  13. cat <<'USAGE'
  14. Usage:
  15. bash scripts/doctor-macos-release-app.sh /Applications/ZCode.app
  16. ZCODE_MACOS_RELEASE_APP_PATH=/Applications/ZCode.app pnpm run doctor:macos-release
  17. Always validates the installed macOS release app with:
  18. codesign --verify --deep --strict <app>
  19. spctl -a -vv -t exec <app>
  20. USAGE
  21. exit 0
  22. fi
  23. require_command() {
  24. local command_name="$1"
  25. if ! command -v "$command_name" >/dev/null 2>&1; then
  26. echo "[macos-release-doctor] missing required command: $command_name" >&2
  27. exit 1
  28. fi
  29. }
  30. assert_bundle_dir() {
  31. local label="$1"
  32. local bundle_path="$2"
  33. if [ -z "$bundle_path" ]; then
  34. echo "[macos-release-doctor] ${label} missing: <no app path provided>" >&2
  35. exit 1
  36. fi
  37. if [ ! -d "$bundle_path" ]; then
  38. echo "[macos-release-doctor] ${label} missing: $bundle_path" >&2
  39. exit 1
  40. fi
  41. }
  42. assert_executable() {
  43. local label="$1"
  44. local executable_path="$2"
  45. if [ ! -x "$executable_path" ]; then
  46. echo "[macos-release-doctor] ${label} executable missing or not executable: $executable_path" >&2
  47. exit 1
  48. fi
  49. }
  50. assert_macho_executable() {
  51. local label="$1"
  52. local executable_path="$2"
  53. local magic
  54. magic="$(od -An -N4 -tx1 -v "$executable_path" | tr -d ' \n')"
  55. case "$magic" in
  56. feedface|feedfacf|cafebabe|cafebabf|cffaedfe|cefaedfe|bebafeca|bfbafeca) ;;
  57. *)
  58. echo "[macos-release-doctor] ${label} is not a Mach-O executable: $executable_path" >&2
  59. exit 1
  60. ;;
  61. esac
  62. }
  63. run_quiet_validation() {
  64. local label="$1"
  65. shift
  66. local output_file
  67. output_file="$(mktemp "${TMPDIR:-/tmp}/zcode-macos-release-doctor.XXXXXX")"
  68. if "$@" >"$output_file" 2>&1; then
  69. rm -f "$output_file"
  70. return 0
  71. fi
  72. echo "[macos-release-doctor] ${label} failed" >&2
  73. cat "$output_file" >&2
  74. rm -f "$output_file"
  75. return 1
  76. }
  77. validate_release_bundle() {
  78. local label="$1"
  79. local bundle_path="$2"
  80. local require_notarization_staple="${3:-0}"
  81. echo "[macos-release-doctor] validating ${label}: $bundle_path"
  82. run_quiet_validation "${label} codesign verify" codesign --verify --deep --strict "$bundle_path"
  83. run_quiet_validation "${label} Gatekeeper exec assessment" spctl -a -vv -t exec "$bundle_path"
  84. if [ "$require_notarization_staple" = "1" ]; then
  85. # xcrun 只用于 macOS staple 验收,等目标 bundle 存在且基础签名校验通过后再检查,
  86. # 避免缺少 xcrun 的报错掩盖 bundle 缺失或签名错误。
  87. require_command xcrun
  88. # 仅检查 spctl 可能依赖联网取票或缓存,仍需验证目标 bundle 的本地 staple。
  89. run_quiet_validation "${label} notarization staple" xcrun stapler validate "$bundle_path"
  90. fi
  91. }
  92. require_command codesign
  93. require_command spctl
  94. # 过去 release/notarization 成功只说明 DMG 通过了 gate,不能证明安装后的主 app
  95. # 能被 Gatekeeper 以 exec 类型放行。这里先 fail-closed 检查 bundle 结构和主可执行
  96. # 文件,再跑 codesign/spctl,避免安装不完整或签名损坏时误报发布成功。
  97. assert_bundle_dir "$APP_BUNDLE_NAME" "$APP_PATH"
  98. assert_executable "$APP_BUNDLE_NAME" "$APP_PATH/Contents/MacOS/$APP_EXECUTABLE_NAME"
  99. validate_release_bundle "$APP_BUNDLE_NAME" "$APP_PATH"
  100. echo "[macos-release-doctor] done"