doctor-macos-release-app.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. APP_PATH="${1:-${ZCODE_MACOS_RELEASE_APP_PATH:-/Applications/ZCode.app}}"
  4. ZCODE_HOME_DIR="${ZCODE_HOME:-${HOME:+$HOME/.zcode}}"
  5. CUA_HELPER_INSTALL_VARIANT="${ZCODE_CUA_HELPER_INSTALL_VARIANT:-}"
  6. # 安装包身份与后端环境分轴:ZCODE_PREVIEW_IDENTITY=1 让生产后端的构建仍是 ZCode Preview。
  7. # 只认 "1",与 CI workflow / release 门的精确比较同一套语义(其它拼写由 resolve_zcode_env 拒绝)。
  8. is_preview_identity_requested() {
  9. [[ "${ZCODE_PREVIEW_IDENTITY:-}" = "1" ]]
  10. }
  11. if [ -z "$CUA_HELPER_INSTALL_VARIANT" ] && { [ "${ZCODE_ENV:-production}" = "test" ] || is_preview_identity_requested; }; then
  12. CUA_HELPER_INSTALL_VARIANT="preview"
  13. fi
  14. if [ -n "$CUA_HELPER_INSTALL_VARIANT" ] && [[ ! "$CUA_HELPER_INSTALL_VARIANT" =~ ^[A-Za-z0-9_-]+$ ]]; then
  15. echo "[macos-release-doctor] invalid ZCODE_CUA_HELPER_INSTALL_VARIANT: $CUA_HELPER_INSTALL_VARIANT" >&2
  16. exit 1
  17. fi
  18. CUA_HELPER_INSTALL_SUBDIR="${CUA_HELPER_INSTALL_VARIANT:+$CUA_HELPER_INSTALL_VARIANT/}"
  19. # Shell cannot import producer TS constants. macosReleaseAppDoctor.test.ts locks these literals to
  20. # @zcode/zcode-cua/broker/helperConstants so a future producer rename fails loudly.
  21. ZCODE_HOME_HELPER_APP_PATH="${ZCODE_HOME_DIR:+$ZCODE_HOME_DIR/computer-use/${CUA_HELPER_INSTALL_SUBDIR}ZCode Computer Use.app}"
  22. HELPER_APP_PATH="${ZCODE_CUA_HELPER_APP_PATH:-$ZCODE_HOME_HELPER_APP_PATH}"
  23. APP_BUNDLE_NAME="$(basename "$APP_PATH")"
  24. APP_DISPLAY_NAME="${APP_BUNDLE_NAME%.app}"
  25. APP_EXECUTABLE_NAME="${ZCODE_APP_EXECUTABLE_NAME:-$APP_DISPLAY_NAME}"
  26. HELPER_EXECUTABLE_NAME="${ZCODE_CUA_HELPER_EXECUTABLE_NAME:-ZCode Computer Use}"
  27. REQUIRE_CUA_HELPER="${ZCODE_CUA_REQUIRE_HELPER:-0}"
  28. REQUIRE_CUA_HELPER_STAPLE=0
  29. if [[ "$REQUIRE_CUA_HELPER" =~ ^(1|true|on)$ ]]; then
  30. REQUIRE_CUA_HELPER_STAPLE=1
  31. fi
  32. if [ "${APP_PATH:-}" = "--help" ] || [ "${APP_PATH:-}" = "-h" ]; then
  33. cat <<'USAGE'
  34. Usage:
  35. bash scripts/doctor-macos-release-app.sh /Applications/ZCode.app
  36. ZCODE_CUA_REQUIRE_HELPER=1 bash scripts/doctor-macos-release-app.sh /Applications/ZCode.app
  37. ZCODE_CUA_HELPER_APP_PATH=/path/to/ZCode\ Computer\ Use.app ZCODE_CUA_REQUIRE_HELPER=1 bash scripts/doctor-macos-release-app.sh /Applications/ZCode.app
  38. ZCODE_MACOS_RELEASE_APP_PATH=/Applications/ZCode.app pnpm run doctor:macos-release
  39. Always validates the installed macOS release app with:
  40. codesign --verify --deep --strict <app>
  41. spctl -a -vv -t exec <app>
  42. Set ZCODE_CUA_REQUIRE_HELPER=1 for CUA/Helper release gates. In that mode this
  43. also validates the independently signed Helper bundle selected below with the
  44. same codesign/spctl checks and a local notarization staple check.
  45. Helper lookup order:
  46. 1. ZCODE_CUA_HELPER_APP_PATH (CI passes the Helper bundled inside ZCode.app)
  47. 2. ${ZCODE_HOME:-$HOME/.zcode}/computer-use/<optional variant>/ZCode Computer Use.app
  48. CI release acceptance validates the copy actually shipped at
  49. ZCode.app/Contents/Resources/cua-helper/ZCode Computer Use.app. Post-install checks
  50. may omit the override to validate the user-level installed copy. A missing selected
  51. Helper.app fails closed.
  52. USAGE
  53. exit 0
  54. fi
  55. require_command() {
  56. local command_name="$1"
  57. if ! command -v "$command_name" >/dev/null 2>&1; then
  58. echo "[macos-release-doctor] missing required command: $command_name" >&2
  59. exit 1
  60. fi
  61. }
  62. assert_bundle_dir() {
  63. local label="$1"
  64. local bundle_path="$2"
  65. if [ -z "$bundle_path" ]; then
  66. echo "[macos-release-doctor] ${label} missing: <no product helper path; set ZCODE_HOME or HOME>" >&2
  67. exit 1
  68. fi
  69. if [ ! -d "$bundle_path" ]; then
  70. echo "[macos-release-doctor] ${label} missing: $bundle_path" >&2
  71. exit 1
  72. fi
  73. }
  74. assert_executable() {
  75. local label="$1"
  76. local executable_path="$2"
  77. if [ ! -x "$executable_path" ]; then
  78. echo "[macos-release-doctor] ${label} executable missing or not executable: $executable_path" >&2
  79. exit 1
  80. fi
  81. }
  82. assert_macho_executable() {
  83. local label="$1"
  84. local executable_path="$2"
  85. local magic
  86. magic="$(od -An -N4 -tx1 -v "$executable_path" | tr -d ' \n')"
  87. case "$magic" in
  88. feedface|feedfacf|cafebabe|cafebabf|cffaedfe|cefaedfe|bebafeca|bfbafeca) ;;
  89. *)
  90. echo "[macos-release-doctor] ${label} is not a Mach-O executable: $executable_path" >&2
  91. exit 1
  92. ;;
  93. esac
  94. }
  95. run_quiet_validation() {
  96. local label="$1"
  97. shift
  98. local output_file
  99. output_file="$(mktemp "${TMPDIR:-/tmp}/zcode-macos-release-doctor.XXXXXX")"
  100. if "$@" >"$output_file" 2>&1; then
  101. rm -f "$output_file"
  102. return 0
  103. fi
  104. echo "[macos-release-doctor] ${label} failed" >&2
  105. cat "$output_file" >&2
  106. rm -f "$output_file"
  107. return 1
  108. }
  109. validate_release_bundle() {
  110. local label="$1"
  111. local bundle_path="$2"
  112. local require_notarization_staple="${3:-0}"
  113. echo "[macos-release-doctor] validating ${label}: $bundle_path"
  114. run_quiet_validation "${label} codesign verify" codesign --verify --deep --strict "$bundle_path"
  115. run_quiet_validation "${label} Gatekeeper exec assessment" spctl -a -vv -t exec "$bundle_path"
  116. if [ "$require_notarization_staple" = "1" ]; then
  117. # 修复原因:xcrun 只属于 macOS 的 staple 验收工具,必须等 Helper 已存在且基础签名校验通过后再检查,
  118. # 否则 Linux 单测和真实故障都会先被“缺少 xcrun”掩盖,无法报告 Helper 缺失或签名错误。
  119. require_command xcrun
  120. # 仅检查 spctl 会被联网取票或缓存掩盖,必须验证最终 Helper 的本地 staple。
  121. run_quiet_validation "${label} notarization staple" xcrun stapler validate "$bundle_path"
  122. fi
  123. }
  124. require_command codesign
  125. require_command spctl
  126. # Bugfix: 过去 release/notarization 成功只说明 DMG 通过了 gate,不能证明安装后的主 app
  127. # 和 Computer Use Helper.app 都能被 Gatekeeper 以 exec 类型放行。这里先 fail-closed 检查 bundle
  128. # 结构,再分别跑 codesign/spctl,避免 helper 缺失或未独立签名时误报发布成功。
  129. # CI 显式传入最终 ZCode.app 内的 Helper;安装后验收则默认检查用户级复制品。
  130. assert_bundle_dir "$APP_BUNDLE_NAME" "$APP_PATH"
  131. assert_executable "$APP_BUNDLE_NAME" "$APP_PATH/Contents/MacOS/$APP_EXECUTABLE_NAME"
  132. validate_release_bundle "$APP_BUNDLE_NAME" "$APP_PATH"
  133. if [ "$REQUIRE_CUA_HELPER" != "1" ] && [ "$REQUIRE_CUA_HELPER" != "true" ] && [ "$REQUIRE_CUA_HELPER" != "on" ]; then
  134. if [ -d "$HELPER_APP_PATH" ]; then
  135. echo "[macos-release-doctor] standalone Computer Use Helper.app present; validating optional helper: $HELPER_APP_PATH"
  136. else
  137. echo "[macos-release-doctor] standalone Computer Use Helper.app not required; set ZCODE_CUA_REQUIRE_HELPER=1 for CUA release gates"
  138. echo "[macos-release-doctor] done"
  139. exit 0
  140. fi
  141. fi
  142. assert_bundle_dir "Computer Use Helper.app" "$HELPER_APP_PATH"
  143. assert_executable \
  144. "Computer Use Helper.app" \
  145. "$HELPER_APP_PATH/Contents/MacOS/$HELPER_EXECUTABLE_NAME"
  146. assert_macho_executable \
  147. "Computer Use Helper.app executable" \
  148. "$HELPER_APP_PATH/Contents/MacOS/$HELPER_EXECUTABLE_NAME"
  149. validate_release_bundle "Computer Use Helper.app" "$HELPER_APP_PATH" "$REQUIRE_CUA_HELPER_STAPLE"
  150. echo "[macos-release-doctor] done"