Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

chore: 完善发布构建脚本

Bob78598a5f79ce
diff --git a/Docs/DevelopmentGuide.md b/Docs/DevelopmentGuide.md
index d6bca6d..d286349 100644
--- a/Docs/DevelopmentGuide.md
+++ b/Docs/DevelopmentGuide.md
@@ -447,6 +447,8 @@ Codex 版本探测错误:
| 脚本 | 失败条件 | 行为 |
| -------------------- | --------------------------------------------------------------- | -------------------------------------- |
+| `Scripts/build.sh` | 找不到 Xcode 工程、签名命令、notary 凭据或导出 App | `set -euo pipefail` 直接退出并打印错误 |
+| `Scripts/build.sh` | archive、Developer ID 导出、notary、staple 或签名校验失败 | 退出, 不替换 build 中最终 App |
| `Scripts/dmg.sh` | 找不到唯一 App, 版本号缺失, DMG 挂载失败, 缺少必要命令 | `set -euo pipefail` 直接退出并打印错误 |
| `Scripts/appcast.sh` | 找不到 DMG, appcast, Xcode 工程, build setting 或 `sign_update` | 直接退出并打印错误 |
| `Scripts/appcast.sh` | 无法解析 `sparkle:edSignature` | 打印 sign_update 原始输出并退出 |
@@ -965,18 +967,31 @@ App 再次成为 active 时, 也会刷新 Codex 版本区、同步可用性, 并
发布脚本位于 `Scripts/`, 修改后至少运行 shell 语法检查
+`Scripts/build.sh [options] [Output.app]`:
+
+- 使用 `xcodebuild archive` 生成 Release archive, 默认路径为 `build/CodexBar.xcarchive`
+- 构建参数统一使用命令行 options; 运行 `Scripts/build.sh --help` 查看完整参数
+- 每次运行前先清理 `build/` 目录, 只允许清理项目目录内的 build 路径或 `/private/tmp/` 下路径
+- 使用 `developer-id` export options 导出 App; 未传 `--export-options` 时临时生成
+- 默认通过 `-allowProvisioningUpdates` 允许自动签名补齐 provisioning, 可用 `--no-provisioning-updates` 关闭
+- notary 凭据优先使用 `--notary-profile`; 未设置时使用 `--apple-id`、`--notary-password` 和 `--team-id`; 如果两组参数同时提供, `--notary-profile` 生效, Apple ID 三件套会被忽略
+- 将导出的 App 压缩后提交 `xcrun notarytool submit --wait`, 成功后对 App 执行 `stapler staple` 和 `stapler validate`
+- 通过 `codesign --verify --deep --strict` 校验导出 App 和最终 App; 默认额外执行 `spctl --assess`
+- 成功后把最终 App 写入 `build/CodexBar.app` 或传入的 `Output.app`; notary 失败时不替换最终 App
+- 可用 `--skip-notarization` 只执行 archive/export/signature verify, 便于本地调试脚本
+
`Scripts/dmg.sh [App.app] [Output.dmg]`:
-- 解析 App 路径, 未指定时从项目根查找唯一 `.app`
+- 解析 App 路径, 未指定时从 `build/` 查找唯一 `.app`
- 读取 `MARKETING_VERSION`, 优先从 Xcode build settings 读取, 失败后回退 `project.pbxproj` 和 App `Info.plist`
- 准备临时 staging 目录, 复制 `.app`, 挂载后通过 Finder 创建 `/Applications` alias
- 创建可写 DMG
- 挂载 DMG 并用 AppleScript 写 Finder icon view 布局
-- 压缩为最终 UDZO DMG
+- 压缩为最终 UDZO DMG; 未指定输出路径时写入 `build/CodexBar-vX.Y.Z.dmg`
`Scripts/appcast.sh [CodexBar-vX.Y.Z.dmg]`:
-- 解析 DMG, appcast, 下载地址, release notes 地址和 Xcode 工程
+- 解析 DMG, appcast, 下载地址, release notes 地址和 Xcode 工程; 未指定 DMG 时从 `build/` 查找唯一 `.dmg`
- 读取 Release 配置中的 `MARKETING_VERSION`, `CURRENT_PROJECT_VERSION` 和 `PRODUCT_NAME`
- 查找 Sparkle `sign_update`
- 对 DMG 签名并解析 `sparkle:edSignature` 和长度
diff --git a/Scripts/appcast.sh b/Scripts/appcast.sh
index edfff61..7048849 100755
--- a/Scripts/appcast.sh
+++ b/Scripts/appcast.sh
@@ -3,6 +3,7 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
+BUILD_DIR="${BUILD_DIR:-${PROJECT_DIR}/build}"
DMG_PATH="${1:-}"
APPCAST_PATH="${APPCAST_PATH:-${PROJECT_DIR}/appcast.xml}"
@@ -14,11 +15,17 @@ SIGN_UPDATE="${SIGN_UPDATE:-}"
XCODE_PROJECT="${XCODE_PROJECT:-}"
XCODE_SCHEME="${XCODE_SCHEME:-CodexBar}"
+if [[ "${BUILD_DIR}" != /* ]]; then
+ BUILD_DIR="${PROJECT_DIR}/${BUILD_DIR}"
+fi
+
usage() {
cat >&2 <<USAGE
usage: Scripts/appcast.sh [CodexBar-vX.Y.Z.dmg]
Environment:
+ BUILD_DIR Directory used for default DMG lookup.
+ Defaults to build/.
APPCAST_PATH Path to appcast.xml. Defaults to appcast.xml.
DOWNLOAD_BASE_URL Base URL for DMG downloads.
Defaults to https://codexbar.zabrian.app/download.
@@ -61,18 +68,26 @@ read_build_setting() {
'
}
-# Print the single project-root entry matching a glob, or fail:
+# Print the single entry matching a glob in a directory, or fail:
# $1 noun for "no X found" $2 find -type $3 -name glob
# $4 noun for "multiple X found" $5 hint appended to the multiple-match error
-# Exits 1 when nothing matches, 2 when more than one matches (printing the list).
+# $6 directory to search. Exits 1 when nothing matches, 2 when more than one
+# matches (printing the list).
find_single() {
+ local search_dir="$6"
local items=()
+
+ if [[ ! -d "${search_dir}" ]]; then
+ echo "error: search directory not found: ${search_dir}" >&2
+ return 1
+ fi
+
while IFS= read -r item; do
items+=("${item}")
- done < <(find "${PROJECT_DIR}" -maxdepth 1 -type "$2" -name "$3" | sort)
+ done < <(find "${search_dir}" -maxdepth 1 -type "$2" -name "$3" | sort)
if [[ "${#items[@]}" -eq 0 ]]; then
- echo "error: no $1 found in project root" >&2
+ echo "error: no $1 found in ${search_dir}" >&2
return 1
fi
@@ -91,7 +106,7 @@ if [[ "${DMG_PATH}" == "-h" || "${DMG_PATH}" == "--help" ]]; then
fi
if [[ -z "${DMG_PATH}" ]]; then
- DMG_PATH="$(find_single DMG f '*.dmg' DMGs 'specify one explicitly:')" || {
+ DMG_PATH="$(find_single DMG f '*.dmg' DMGs 'specify one explicitly:' "${BUILD_DIR}")" || {
[[ "$?" -eq 1 ]] && usage
exit 1
}
@@ -114,7 +129,7 @@ if [[ ! -f "${APPCAST_PATH}" ]]; then
fi
if [[ -z "${XCODE_PROJECT}" ]]; then
- XCODE_PROJECT="$(find_single .xcodeproj d '*.xcodeproj' '.xcodeproj files' 'set XCODE_PROJECT')" || exit 1
+ XCODE_PROJECT="$(find_single .xcodeproj d '*.xcodeproj' '.xcodeproj files' 'set XCODE_PROJECT' "${PROJECT_DIR}")" || exit 1
elif [[ "${XCODE_PROJECT}" != /* ]]; then
XCODE_PROJECT="${PROJECT_DIR}/${XCODE_PROJECT}"
fi
@@ -206,13 +221,13 @@ if [[ "${INCLUDE_RELEASE_NOTES}" != "0" ]]; then
fi
APPCAST_ITEM=" <item>
- <title>${TITLE}</title>
- <sparkle:version>${BUILD_VERSION}</sparkle:version>
- <sparkle:shortVersionString>${SHORT_VERSION}</sparkle:shortVersionString>
- <sparkle:minimumSystemVersion>${MIN_SYSTEM_ESCAPED}</sparkle:minimumSystemVersion>
- ${RELEASE_NOTES_XML} <pubDate>${PUB_DATE}</pubDate>
- <enclosure url=\"${DOWNLOAD_URL_ESCAPED}\" sparkle:edSignature=\"${ED_SIGNATURE_ESCAPED}\" length=\"${ARCHIVE_LENGTH}\" type=\"application/octet-stream\"/>
- </item>"
+ <title>${TITLE}</title>
+ <sparkle:version>${BUILD_VERSION}</sparkle:version>
+ <sparkle:shortVersionString>${SHORT_VERSION}</sparkle:shortVersionString>
+ <sparkle:minimumSystemVersion>${MIN_SYSTEM_ESCAPED}</sparkle:minimumSystemVersion>
+ ${RELEASE_NOTES_XML} <pubDate>${PUB_DATE}</pubDate>
+ <enclosure url=\"${DOWNLOAD_URL_ESCAPED}\" sparkle:edSignature=\"${ED_SIGNATURE_ESCAPED}\" length=\"${ARCHIVE_LENGTH}\" type=\"application/octet-stream\"/>
+</item>"
echo "==> Updating appcast"
APPCAST_ITEM="${APPCAST_ITEM}" APPCAST_VERSION="${BUILD_VERSION}" perl -0pi -e '
diff --git a/Scripts/build.sh b/Scripts/build.sh
new file mode 100755
index 0000000..62de146
--- /dev/null
+++ b/Scripts/build.sh
@@ -0,0 +1,645 @@
+#!/usr/bin/env bash
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
+
+XCODE_PROJECT="${PROJECT_DIR}/CodexBar.xcodeproj"
+XCODE_SCHEME="CodexBar"
+CONFIGURATION="Release"
+BUILD_DIR="${PROJECT_DIR}/build"
+DERIVED_DATA_PATH=""
+ARCHIVE_PATH=""
+EXPORT_OPTIONS_PLIST=""
+ALLOW_PROVISIONING_UPDATES="1"
+SKIP_NOTARIZATION="0"
+SPCTL_ASSESS="1"
+
+# Configure the keychain profile once before release builds:
+# xcrun notarytool store-credentials "codexbar-notary" --apple-id "<Apple ID>" --team-id "<Team ID>"
+NOTARYTOOL_PROFILE=""
+APPLE_ID=""
+NOTARYTOOL_PASSWORD=""
+TEAM_ID=""
+OUTPUT_APP_PATH=""
+
+usage() {
+ cat >&2 <<USAGE
+usage: Scripts/build.sh [options] [Output.app]
+
+Builds a Release archive, exports a Developer ID app, submits it to Apple
+notarization, staples the ticket, and writes the final app to build/.
+
+Options:
+ --project PATH Xcode project. Defaults to CodexBar.xcodeproj.
+ --scheme NAME Xcode scheme. Defaults to CodexBar.
+ --configuration NAME Xcode configuration. Defaults to Release.
+ --build-dir DIR Build output directory. Defaults to build/.
+ Cleaned before each build.
+ --derived-data PATH DerivedData path. Defaults to build/DerivedData.
+ --archive-path PATH Archive path. Defaults to build/<scheme>.xcarchive.
+ --export-options PATH Export options plist. Generated when unset.
+ --team-id ID Apple Developer team ID. Defaults to the
+ DEVELOPMENT_TEAM build setting.
+ --allow-provisioning-updates Pass -allowProvisioningUpdates. Default.
+ --no-provisioning-updates Omit -allowProvisioningUpdates.
+ --notary-profile PROFILE Keychain profile for xcrun notarytool.
+ --apple-id APPLE_ID Apple ID fallback when no notary profile is set.
+ --notary-password PASSWORD App-specific password fallback. Prefer profile.
+ --skip-notarization Build/export without notarytool and stapling.
+ --skip-spctl-assess Skip final Gatekeeper spctl assessment.
+ --output-app PATH Final app path. Defaults to build/CodexBar.app.
+ -h, --help Show this help.
+
+Recommended credential setup:
+ xcrun notarytool store-credentials "codexbar-notary" --apple-id "<Apple ID>" --team-id "<Team ID>"
+ Scripts/build.sh --notary-profile codexbar-notary
+USAGE
+}
+
+parse_args() {
+ while [[ "$#" -gt 0 ]]; do
+ case "$1" in
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ --project)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --project requires a value" >&2
+ usage
+ exit 1
+ fi
+ XCODE_PROJECT="$2"
+ shift 2
+ ;;
+ --project=*)
+ XCODE_PROJECT="${1#*=}"
+ if [[ -z "${XCODE_PROJECT}" ]]; then
+ echo "error: --project requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --scheme)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --scheme requires a value" >&2
+ usage
+ exit 1
+ fi
+ XCODE_SCHEME="$2"
+ shift 2
+ ;;
+ --scheme=*)
+ XCODE_SCHEME="${1#*=}"
+ if [[ -z "${XCODE_SCHEME}" ]]; then
+ echo "error: --scheme requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --configuration)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --configuration requires a value" >&2
+ usage
+ exit 1
+ fi
+ CONFIGURATION="$2"
+ shift 2
+ ;;
+ --configuration=*)
+ CONFIGURATION="${1#*=}"
+ if [[ -z "${CONFIGURATION}" ]]; then
+ echo "error: --configuration requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --build-dir)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --build-dir requires a value" >&2
+ usage
+ exit 1
+ fi
+ BUILD_DIR="$2"
+ shift 2
+ ;;
+ --build-dir=*)
+ BUILD_DIR="${1#*=}"
+ if [[ -z "${BUILD_DIR}" ]]; then
+ echo "error: --build-dir requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --derived-data)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --derived-data requires a value" >&2
+ usage
+ exit 1
+ fi
+ DERIVED_DATA_PATH="$2"
+ shift 2
+ ;;
+ --derived-data=*)
+ DERIVED_DATA_PATH="${1#*=}"
+ if [[ -z "${DERIVED_DATA_PATH}" ]]; then
+ echo "error: --derived-data requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --archive-path)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --archive-path requires a value" >&2
+ usage
+ exit 1
+ fi
+ ARCHIVE_PATH="$2"
+ shift 2
+ ;;
+ --archive-path=*)
+ ARCHIVE_PATH="${1#*=}"
+ if [[ -z "${ARCHIVE_PATH}" ]]; then
+ echo "error: --archive-path requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --export-options)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --export-options requires a value" >&2
+ usage
+ exit 1
+ fi
+ EXPORT_OPTIONS_PLIST="$2"
+ shift 2
+ ;;
+ --export-options=*)
+ EXPORT_OPTIONS_PLIST="${1#*=}"
+ if [[ -z "${EXPORT_OPTIONS_PLIST}" ]]; then
+ echo "error: --export-options requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --team-id)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --team-id requires a value" >&2
+ usage
+ exit 1
+ fi
+ TEAM_ID="$2"
+ shift 2
+ ;;
+ --team-id=*)
+ TEAM_ID="${1#*=}"
+ if [[ -z "${TEAM_ID}" ]]; then
+ echo "error: --team-id requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --allow-provisioning-updates)
+ ALLOW_PROVISIONING_UPDATES="1"
+ shift
+ ;;
+ --no-provisioning-updates)
+ ALLOW_PROVISIONING_UPDATES="0"
+ shift
+ ;;
+ --notary-profile)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --notary-profile requires a value" >&2
+ usage
+ exit 1
+ fi
+ NOTARYTOOL_PROFILE="$2"
+ shift 2
+ ;;
+ --notary-profile=*)
+ NOTARYTOOL_PROFILE="${1#*=}"
+ if [[ -z "${NOTARYTOOL_PROFILE}" ]]; then
+ echo "error: --notary-profile requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --apple-id)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --apple-id requires a value" >&2
+ usage
+ exit 1
+ fi
+ APPLE_ID="$2"
+ shift 2
+ ;;
+ --apple-id=*)
+ APPLE_ID="${1#*=}"
+ if [[ -z "${APPLE_ID}" ]]; then
+ echo "error: --apple-id requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --notary-password|--app-specific-password)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: $1 requires a value" >&2
+ usage
+ exit 1
+ fi
+ NOTARYTOOL_PASSWORD="$2"
+ shift 2
+ ;;
+ --notary-password=*|--app-specific-password=*)
+ NOTARYTOOL_PASSWORD="${1#*=}"
+ if [[ -z "${NOTARYTOOL_PASSWORD}" ]]; then
+ echo "error: ${1%%=*} requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --skip-notarization)
+ SKIP_NOTARIZATION="1"
+ shift
+ ;;
+ --skip-spctl-assess)
+ SPCTL_ASSESS="0"
+ shift
+ ;;
+ --output-app)
+ if [[ "$#" -lt 2 || -z "$2" ]]; then
+ echo "error: --output-app requires a value" >&2
+ usage
+ exit 1
+ fi
+ if [[ -n "${OUTPUT_APP_PATH}" ]]; then
+ echo "error: multiple output app paths specified" >&2
+ usage
+ exit 1
+ fi
+ OUTPUT_APP_PATH="$2"
+ shift 2
+ ;;
+ --output-app=*)
+ if [[ -n "${OUTPUT_APP_PATH}" ]]; then
+ echo "error: multiple output app paths specified" >&2
+ usage
+ exit 1
+ fi
+ OUTPUT_APP_PATH="${1#*=}"
+ if [[ -z "${OUTPUT_APP_PATH}" ]]; then
+ echo "error: --output-app requires a value" >&2
+ usage
+ exit 1
+ fi
+ shift
+ ;;
+ --)
+ shift
+ break
+ ;;
+ -*)
+ echo "error: unknown option: $1" >&2
+ usage
+ exit 1
+ ;;
+ *)
+ if [[ -n "${OUTPUT_APP_PATH}" ]]; then
+ echo "error: multiple output app paths specified" >&2
+ usage
+ exit 1
+ fi
+ OUTPUT_APP_PATH="$1"
+ shift
+ ;;
+ esac
+ done
+
+ while [[ "$#" -gt 0 ]]; do
+ if [[ -n "${OUTPUT_APP_PATH}" ]]; then
+ echo "error: multiple output app paths specified" >&2
+ usage
+ exit 1
+ fi
+ OUTPUT_APP_PATH="$1"
+ shift
+ done
+}
+
+require_command() {
+ if ! command -v "$1" >/dev/null 2>&1; then
+ echo "error: missing command: $1" >&2
+ exit 1
+ fi
+}
+
+absolute_path() {
+ local path="$1"
+ if [[ "${path}" == /* ]]; then
+ printf '%s\n' "${path}"
+ else
+ printf '%s\n' "${PROJECT_DIR}/${path}"
+ fi
+}
+
+read_build_setting() {
+ local name="$1"
+ printf '%s\n' "${BUILD_SETTINGS}" |
+ awk -F= -v key="${name}" '
+ $1 ~ "^[[:space:]]*" key "[[:space:]]*$" {
+ value = $2
+ gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
+ print value
+ exit
+ }
+ '
+}
+
+safe_remove_path() {
+ local path="$1"
+ local parent=""
+ local resolved=""
+
+ if [[ ! -e "${path}" ]]; then
+ return
+ fi
+
+ parent="$(cd "$(dirname "${path}")" && pwd)"
+ resolved="${parent}/$(basename "${path}")"
+
+ case "${resolved}" in
+ "${BUILD_DIR}"/*|/private/tmp/*)
+ rm -rf "${resolved}"
+ ;;
+ *)
+ echo "error: refusing to remove path outside build or /private/tmp: ${resolved}" >&2
+ exit 1
+ ;;
+ esac
+}
+
+clean_build_dir() {
+ case "${BUILD_DIR}" in
+ *"/.."*|*"../"*|"..")
+ echo "error: refusing to clean build path containing '..': ${BUILD_DIR}" >&2
+ exit 1
+ ;;
+ "${PROJECT_DIR}"|"${PROJECT_DIR}/"|"${PROJECT_DIR}/.git"|\
+ "${PROJECT_DIR}/.github"|"${PROJECT_DIR}/CodexBar"|\
+ "${PROJECT_DIR}/Docs"|"${PROJECT_DIR}/Images"|\
+ "${PROJECT_DIR}/Scripts")
+ echo "error: refusing to clean protected project path: ${BUILD_DIR}" >&2
+ exit 1
+ ;;
+ "${PROJECT_DIR}"/*|/private/tmp/*)
+ ;;
+ *)
+ echo "error: refusing to clean build directory outside project or /private/tmp: ${BUILD_DIR}" >&2
+ exit 1
+ ;;
+ esac
+
+ if [[ -e "${BUILD_DIR}" && ! -d "${BUILD_DIR}" ]]; then
+ echo "error: build path exists but is not a directory: ${BUILD_DIR}" >&2
+ exit 1
+ fi
+
+ echo "==> Cleaning build directory ${BUILD_DIR}"
+ rm -rf "${BUILD_DIR}"
+ mkdir -p "${BUILD_DIR}"
+}
+
+write_export_options() {
+ local path="$1"
+
+ {
+ cat <<PLIST
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>method</key>
+ <string>developer-id</string>
+ <key>signingStyle</key>
+ <string>automatic</string>
+PLIST
+ if [[ -n "${TEAM_ID}" ]]; then
+ cat <<PLIST
+ <key>teamID</key>
+ <string>${TEAM_ID}</string>
+PLIST
+ fi
+ cat <<PLIST
+ <key>stripSwiftSymbols</key>
+ <true/>
+</dict>
+</plist>
+PLIST
+ } > "${path}"
+}
+
+parse_args "$@"
+
+XCODE_PROJECT="$(absolute_path "${XCODE_PROJECT}")"
+BUILD_DIR="$(absolute_path "${BUILD_DIR}")"
+
+if [[ -z "${DERIVED_DATA_PATH}" ]]; then
+ DERIVED_DATA_PATH="${BUILD_DIR}/DerivedData"
+else
+ DERIVED_DATA_PATH="$(absolute_path "${DERIVED_DATA_PATH}")"
+fi
+
+if [[ -z "${ARCHIVE_PATH}" ]]; then
+ ARCHIVE_PATH="${BUILD_DIR}/${XCODE_SCHEME}.xcarchive"
+else
+ ARCHIVE_PATH="$(absolute_path "${ARCHIVE_PATH}")"
+fi
+
+if [[ -n "${EXPORT_OPTIONS_PLIST}" ]]; then
+ EXPORT_OPTIONS_PLIST="$(absolute_path "${EXPORT_OPTIONS_PLIST}")"
+fi
+
+if [[ ! -d "${XCODE_PROJECT}" || "${XCODE_PROJECT}" != *.xcodeproj ]]; then
+ echo "error: invalid Xcode project: ${XCODE_PROJECT}" >&2
+ exit 1
+fi
+
+require_command xcodebuild
+require_command ditto
+require_command codesign
+
+if [[ "${SKIP_NOTARIZATION}" != "1" ]]; then
+ require_command xcrun
+fi
+
+mkdir -p "${BUILD_DIR}" "$(dirname "${ARCHIVE_PATH}")" "${DERIVED_DATA_PATH}"
+BUILD_DIR="$(cd "${BUILD_DIR}" && pwd)"
+DERIVED_DATA_PATH="$(cd "${DERIVED_DATA_PATH}" && pwd)"
+
+echo "==> Reading Xcode build settings"
+BUILD_SETTINGS="$(
+ xcodebuild \
+ -project "${XCODE_PROJECT}" \
+ -scheme "${XCODE_SCHEME}" \
+ -configuration "${CONFIGURATION}" \
+ -destination "generic/platform=macOS" \
+ -showBuildSettings
+)"
+
+PRODUCT_NAME="$(read_build_setting PRODUCT_NAME)"
+FULL_PRODUCT_NAME="$(read_build_setting FULL_PRODUCT_NAME)"
+DEVELOPMENT_TEAM="$(read_build_setting DEVELOPMENT_TEAM)"
+
+if [[ -z "${TEAM_ID}" ]]; then
+ TEAM_ID="${DEVELOPMENT_TEAM}"
+fi
+
+if [[ -z "${PRODUCT_NAME}" || "${PRODUCT_NAME}" == *'$('* ]]; then
+ PRODUCT_NAME="${XCODE_SCHEME}"
+fi
+
+if [[ -z "${FULL_PRODUCT_NAME}" || "${FULL_PRODUCT_NAME}" == *'$('* ]]; then
+ FULL_PRODUCT_NAME="${PRODUCT_NAME}.app"
+fi
+
+if [[ -z "${OUTPUT_APP_PATH}" ]]; then
+ OUTPUT_APP_PATH="${BUILD_DIR}/${FULL_PRODUCT_NAME}"
+else
+ OUTPUT_APP_PATH="$(absolute_path "${OUTPUT_APP_PATH}")"
+fi
+
+if [[ "${OUTPUT_APP_PATH}" != *.app ]]; then
+ echo "error: output path must end with .app: ${OUTPUT_APP_PATH}" >&2
+ exit 1
+fi
+
+case "${OUTPUT_APP_PATH}" in
+ "${BUILD_DIR}"/*) ;;
+ *)
+ echo "error: output app must be inside BUILD_DIR (${BUILD_DIR}): ${OUTPUT_APP_PATH}" >&2
+ exit 1
+ ;;
+esac
+
+TEMP_ROOT="$(mktemp -d "/private/tmp/${PRODUCT_NAME}BuildNotary.XXXXXX")"
+EXPORT_DIR="${TEMP_ROOT}/export"
+NOTARY_ZIP="${TEMP_ROOT}/${PRODUCT_NAME}-notary.zip"
+GENERATED_EXPORT_OPTIONS="${TEMP_ROOT}/ExportOptions.plist"
+
+cleanup() {
+ rm -rf "${TEMP_ROOT}"
+}
+trap cleanup EXIT
+
+if [[ -z "${EXPORT_OPTIONS_PLIST}" ]]; then
+ EXPORT_OPTIONS_PLIST="${GENERATED_EXPORT_OPTIONS}"
+ write_export_options "${EXPORT_OPTIONS_PLIST}"
+elif [[ ! -f "${EXPORT_OPTIONS_PLIST}" ]]; then
+ echo "error: export options plist not found: ${EXPORT_OPTIONS_PLIST}" >&2
+ exit 1
+fi
+
+PROVISIONING_FLAGS=()
+if [[ "${ALLOW_PROVISIONING_UPDATES}" == "1" ]]; then
+ PROVISIONING_FLAGS=(-allowProvisioningUpdates)
+fi
+
+NOTARY_AUTH_ARGS=()
+if [[ "${SKIP_NOTARIZATION}" != "1" ]]; then
+ if [[ -n "${NOTARYTOOL_PROFILE}" ]]; then
+ NOTARY_AUTH_ARGS=(--keychain-profile "${NOTARYTOOL_PROFILE}")
+ else
+ if [[ -z "${APPLE_ID}" || -z "${NOTARYTOOL_PASSWORD}" || -z "${TEAM_ID}" ]]; then
+ echo "error: set --notary-profile, or set --apple-id, --notary-password, and --team-id" >&2
+ usage
+ exit 1
+ fi
+ NOTARY_AUTH_ARGS=(--apple-id "${APPLE_ID}" --password "${NOTARYTOOL_PASSWORD}" --team-id "${TEAM_ID}")
+ fi
+fi
+
+clean_build_dir
+mkdir -p "$(dirname "${ARCHIVE_PATH}")" "${DERIVED_DATA_PATH}"
+
+echo "==> Archiving ${XCODE_SCHEME} (${CONFIGURATION})"
+xcodebuild \
+ -project "${XCODE_PROJECT}" \
+ -scheme "${XCODE_SCHEME}" \
+ -configuration "${CONFIGURATION}" \
+ -destination "generic/platform=macOS" \
+ -derivedDataPath "${DERIVED_DATA_PATH}" \
+ -archivePath "${ARCHIVE_PATH}" \
+ "${PROVISIONING_FLAGS[@]}" \
+ archive
+
+mkdir -p "${EXPORT_DIR}"
+
+echo "==> Exporting Developer ID app"
+xcodebuild \
+ -exportArchive \
+ -archivePath "${ARCHIVE_PATH}" \
+ -exportPath "${EXPORT_DIR}" \
+ -exportOptionsPlist "${EXPORT_OPTIONS_PLIST}" \
+ "${PROVISIONING_FLAGS[@]}"
+
+EXPORTED_APP_PATH="${EXPORT_DIR}/${FULL_PRODUCT_NAME}"
+if [[ ! -d "${EXPORTED_APP_PATH}" ]]; then
+ exported_apps=()
+ while IFS= read -r app; do
+ exported_apps+=("${app}")
+ done < <(find "${EXPORT_DIR}" -maxdepth 1 -type d -name "*.app" | sort)
+
+ if [[ "${#exported_apps[@]}" -ne 1 ]]; then
+ echo "error: unable to find a single exported app in ${EXPORT_DIR}" >&2
+ printf ' %s\n' "${exported_apps[@]}" >&2
+ exit 1
+ fi
+
+ EXPORTED_APP_PATH="${exported_apps[0]}"
+fi
+
+echo "==> Verifying exported app signature"
+codesign --verify --deep --strict --verbose=2 "${EXPORTED_APP_PATH}"
+
+if [[ "${SKIP_NOTARIZATION}" == "1" ]]; then
+ echo "warning: --skip-notarization set, notary submission and stapling were skipped" >&2
+else
+ echo "==> Preparing notarization archive"
+ ditto -c -k --keepParent "${EXPORTED_APP_PATH}" "${NOTARY_ZIP}"
+
+ echo "==> Submitting notarization"
+ xcrun notarytool submit "${NOTARY_ZIP}" --wait "${NOTARY_AUTH_ARGS[@]}"
+
+ echo "==> Stapling notarization ticket"
+ xcrun stapler staple "${EXPORTED_APP_PATH}"
+ xcrun stapler validate "${EXPORTED_APP_PATH}"
+fi
+
+safe_remove_path "${OUTPUT_APP_PATH}"
+
+echo "==> Copying app to ${OUTPUT_APP_PATH}"
+mkdir -p "$(dirname "${OUTPUT_APP_PATH}")"
+ditto "${EXPORTED_APP_PATH}" "${OUTPUT_APP_PATH}"
+
+echo "==> Verifying final app signature"
+codesign --verify --deep --strict --verbose=2 "${OUTPUT_APP_PATH}"
+
+if [[ "${SKIP_NOTARIZATION}" != "1" && "${SPCTL_ASSESS}" == "1" ]]; then
+ echo "==> Assessing final app with Gatekeeper"
+ spctl --assess --type execute --verbose=4 "${OUTPUT_APP_PATH}"
+fi
+
+echo "==> Built ${OUTPUT_APP_PATH}"
+echo "==> Next: Scripts/dmg.sh \"${OUTPUT_APP_PATH}\""
diff --git a/Scripts/dmg.sh b/Scripts/dmg.sh
index 84df30f..577cb56 100755
--- a/Scripts/dmg.sh
+++ b/Scripts/dmg.sh
@@ -3,24 +3,35 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
+BUILD_DIR="${BUILD_DIR:-${PROJECT_DIR}/build}"
APP_PATH="${1:-}"
OUTPUT_PATH="${2:-}"
+if [[ "${BUILD_DIR}" != /* ]]; then
+ BUILD_DIR="${PROJECT_DIR}/${BUILD_DIR}"
+fi
+
if [[ -z "${APP_PATH}" ]]; then
+ if [[ ! -d "${BUILD_DIR}" ]]; then
+ echo "error: build 目录不存在: ${BUILD_DIR}" >&2
+ echo "usage: Scripts/dmg.sh [App.app] [Output.dmg]" >&2
+ exit 1
+ fi
+
apps=()
while IFS= read -r app; do
apps+=("${app}")
- done < <(find "${PROJECT_DIR}" -maxdepth 1 -type d -name "*.app" | sort)
+ done < <(find "${BUILD_DIR}" -maxdepth 1 -type d -name "*.app" | sort)
if [[ "${#apps[@]}" -eq 0 ]]; then
- echo "error: 当前目录没有找到 .app" >&2
+ echo "error: build 目录没有找到 .app: ${BUILD_DIR}" >&2
echo "usage: Scripts/dmg.sh [App.app] [Output.dmg]" >&2
exit 1
fi
if [[ "${#apps[@]}" -gt 1 ]]; then
- echo "error: 当前目录找到多个 .app, 请指定要打包的 app 路径: " >&2
+ echo "error: build 目录找到多个 .app, 请指定要打包的 app 路径: " >&2
printf ' %s\n' "${apps[@]}" >&2
exit 1
fi
@@ -85,7 +96,7 @@ if [[ -z "${VERSION}" ]]; then
fi
if [[ -z "${OUTPUT_PATH}" ]]; then
- OUTPUT_PATH="${PROJECT_DIR}/${APP_NAME}-v${VERSION}.dmg"
+ OUTPUT_PATH="${BUILD_DIR}/${APP_NAME}-v${VERSION}.dmg"
elif [[ "${OUTPUT_PATH}" != /* ]]; then
OUTPUT_PATH="${PROJECT_DIR}/${OUTPUT_PATH}"
fi