Files
Ydisks-Xianyu-Helper/packaging/macos/build-pkg.sh
T
Christ 80baba46ff fix: 修复 macOS runtime 执行权限与架构包上传
- 在 pkgbuild 前将 Playwright Node、Chromium 及其他 Mach-O 文件统一设置为 0755,避免安装后普通用户启动服务时出现 permission denied。

- 将 macOS arm64 与 amd64 安装包分别上传为独立 Artifact,避免用户下载包含另一架构的无用文件。
2026-08-12 02:17:38 +08:00

139 lines
5.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
set -eu
VERSION="${1:?version is required}"
DIST_DIR="${2:?dist directory is required}"
ARCH="${3:?architecture is required (arm64 or amd64)}"
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)"
PROJECT_ROOT="$(CDPATH= cd -- "$SCRIPT_DIR/../.." && pwd)"
BIN_DIR="$DIST_DIR/$ARCH"
ROOT_DIR="$DIST_DIR/pkgroot-$ARCH"
APP_DIR="$ROOT_DIR/Applications/Ydisks闲鱼助手"
APP="$APP_DIR/Ydisks闲鱼助手.app"
PACKAGE_PATH="$DIST_DIR/Ydisks-Xianyu-Helper-$VERSION-$ARCH.pkg"
UNSIGNED_PACKAGE_PATH="$DIST_DIR/.Ydisks-Xianyu-Helper-$VERSION-$ARCH.unsigned.pkg"
case "$ARCH" in
arm64|amd64) ;;
*) echo "不支持的 macOS 架构:$ARCH" >&2; exit 1 ;;
esac
if [ ! -x "$BIN_DIR/xianyu-server" ] || [ ! -x "$BIN_DIR/xianyu-tray" ]; then
echo "缺少 $ARCH 架构的桌面二进制:$BIN_DIR" >&2
exit 1
fi
if [ ! -d "$DIST_DIR/playwright-runtime/$ARCH/playwright-driver" ] || \
[ ! -d "$DIST_DIR/playwright-runtime/$ARCH/playwright-browsers" ]; then
echo "缺少 $ARCH 架构的 Playwright runtime:$DIST_DIR/playwright-runtime/$ARCH" >&2
exit 1
fi
rm -rf "$ROOT_DIR"
mkdir -p "$APP/Contents/MacOS" "$APP/Contents/Helpers" "$APP/Contents/Resources"
cp "$BIN_DIR/xianyu-server" "$APP/Contents/Helpers/xianyu-server"
cp "$BIN_DIR/xianyu-tray" "$APP/Contents/MacOS/Ydisks闲鱼助手"
cp "$SCRIPT_DIR/uninstall.command" "$APP_DIR/卸载 Ydisks闲鱼助手.command"
cp "$SCRIPT_DIR/com.ydisks.xianyu-helper.server.plist.template" "$APP/Contents/Resources/"
cp "$SCRIPT_DIR/com.ydisks.xianyu-helper.tray.plist.template" "$APP/Contents/Resources/"
mkdir -p "$APP/Contents/Resources/playwright-runtime/$ARCH"
cp -R "$DIST_DIR/playwright-runtime/$ARCH/." "$APP/Contents/Resources/playwright-runtime/$ARCH/"
cp "$PROJECT_ROOT/icon/macos/Assets.car" "$APP/Contents/Resources/Assets.car"
cp "$PROJECT_ROOT/icon/macos/icon.icns" "$APP/Contents/Resources/icon.icns"
sed "s/__VERSION__/$VERSION/g" "$SCRIPT_DIR/Info.plist" > "$APP/Contents/Info.plist"
chmod 0755 "$APP/Contents/MacOS/Ydisks闲鱼助手" "$APP/Contents/Helpers/xianyu-server" "$APP_DIR/卸载 Ydisks闲鱼助手.command"
# pkgbuild 会保留 runtime 文件的原始权限;Playwright 下载的 Node 和
# Chromium Mach-O 可能只有所有者可执行。安装包由 root 安装后,普通用户
# 启动 LaunchAgent 时会因 permission denied 无法执行,因此统一授予执行权限。
runtime_files="$(mktemp)"
trap 'rm -f "$runtime_files"' EXIT
find "$APP/Contents/Resources/playwright-runtime" -type f -print > "$runtime_files"
while IFS= read -r runtime_file; do
if file "$runtime_file" | grep -q 'Mach-O'; then
chmod 0755 "$runtime_file"
fi
done < "$runtime_files"
if [ -n "${MACOS_SIGNING_IDENTITY:-}" ]; then
sign_code() {
target="$1"
if [ -n "${MACOS_SIGNING_KEYCHAIN:-}" ]; then
codesign --force --sign "$MACOS_SIGNING_IDENTITY" \
--keychain "$MACOS_SIGNING_KEYCHAIN" --timestamp=none "$target"
else
codesign --force --sign "$MACOS_SIGNING_IDENTITY" \
--timestamp=none "$target"
fi
}
sign_nested_bundle() {
nested_bundle="$1"
echo "Signing nested bundle: $nested_bundle"
if [ -n "${MACOS_SIGNING_KEYCHAIN:-}" ]; then
codesign --force --deep --sign "$MACOS_SIGNING_IDENTITY" \
--keychain "$MACOS_SIGNING_KEYCHAIN" --timestamp=none "$nested_bundle"
else
codesign --force --deep --sign "$MACOS_SIGNING_IDENTITY" \
--timestamp=none "$nested_bundle"
fi
}
# Playwright runtime 中包含独立 Node 和 Chromium.app。bundle 主程序不能
# 提前作为单个 Mach-O 签名,否则 codesign 会先校验尚未签名的内层 Framework。
# 因此这里只签 bundle 外的独立 Mach-O,再按目录深度递归签名所有 bundle。
signing_list="$runtime_files"
find "$APP/Contents/Resources/playwright-runtime" -type f -print > "$signing_list"
while IFS= read -r executable; do
case "$executable" in
*.app/*|*.framework/*|*.xpc/*) continue ;;
esac
if file "$executable" | grep -q 'Mach-O'; then
sign_code "$executable"
fi
done < "$signing_list"
find "$APP/Contents/Resources/playwright-runtime" -depth -type d \
\( -name '*.framework' -o -name '*.xpc' -o -name '*.app' \) -print > "$signing_list"
while IFS= read -r nested_bundle; do
sign_nested_bundle "$nested_bundle"
done < "$signing_list"
# macOS 代码签名必须从内部组件开始,最后再签名 App 包本身。
sign_code "$APP/Contents/Helpers/xianyu-server"
sign_code "$APP/Contents/MacOS/Ydisks闲鱼助手"
sign_code "$APP"
codesign --verify --deep --strict --verbose=2 "$APP"
fi
if [ -n "${MACOS_SIGNING_IDENTITY:-}" ] && [ -z "${MACOS_INSTALLER_SIGNING_IDENTITY:-}" ]; then
echo 'MACOS_INSTALLER_SIGNING_IDENTITY 未设置,不能生成已签名 macOS 安装包' >&2
exit 1
fi
rm -f "$PACKAGE_PATH" "$UNSIGNED_PACKAGE_PATH"
pkgbuild \
--root "$ROOT_DIR" \
--component-plist "$SCRIPT_DIR/component.plist" \
--scripts "$SCRIPT_DIR/scripts" \
--identifier com.ydisks.xianyu-helper \
--version "$VERSION" \
--install-location / \
"$UNSIGNED_PACKAGE_PATH"
if [ -n "${MACOS_INSTALLER_SIGNING_IDENTITY:-}" ]; then
if [ -n "${MACOS_SIGNING_KEYCHAIN:-}" ]; then
productsign --sign "$MACOS_INSTALLER_SIGNING_IDENTITY" \
--keychain "$MACOS_SIGNING_KEYCHAIN" \
"$UNSIGNED_PACKAGE_PATH" "$PACKAGE_PATH"
else
productsign --sign "$MACOS_INSTALLER_SIGNING_IDENTITY" \
"$UNSIGNED_PACKAGE_PATH" "$PACKAGE_PATH"
fi
pkgutil --check-signature "$PACKAGE_PATH"
rm -f "$UNSIGNED_PACKAGE_PATH"
else
mv "$UNSIGNED_PACKAGE_PATH" "$PACKAGE_PATH"
fi