macOS SwiftPM App Packaging(无 Xcode)用于在完全不依赖 Xcode 工程的情况下,为 macOS 应用建立 SwiftPM 项目骨架,并完成构建、打包、签名、公证和可选发布。它提供模板 assets/templates/bootstrap/ 与 references/packaging.md、references/release.md 作为打包与发布参考。
用途与适用场景 当用户需要基于 SwiftPM 而非 Xcode 工程构建 macOS 应用时;当需要为 SwiftPM 应用提供打包、签名、公证或 appcast 指导时,可使用该技能。
主要能力 提供两步工作流:第一步,引导项目文件夹:复制 assets/templates/bootstrap/ 到新仓库,在 Package.swift、Sources/MyApp/ 和 version.env 中重命名 MyApp,自定义 APP_NAME、BUNDLE_ID 与版本;第二步,构建、打包并运行已引导的应用:将 assets/templates/ 中的脚本复制到仓库(例如 Scripts/),用 swift build 和 swift test 构建与测试,用 Scripts/package_app.sh 打包,用 Scripts/compile_and_run.sh(首选)或 Scripts/launch.sh 运行,可选用 Scripts/sign-and-notarize.sh 与 Scripts/make_appcast.sh 进行发布,并可选创建 git tag、上传 zip/appcast 到 GitHub release 并发布。
使用方式与端到端示例 最短路径示例:
1. 复制并重命名骨架
cp -R assets/templates/bootstrap/ ~/Projects/MyApp cd ~/Projects/MyApp sed -i '' 's/MyApp/HelloApp/g' Package.swift version.env
2. 复制脚本
cp assets/templates/package_app.sh Scripts/ cp assets/templates/compile_and_run.sh Scripts/ chmod +x Scripts/*.sh
3. 构建并启动
swift build Scripts/compile_and_run.sh
验证检查点 打包后(Scripts/package_app.sh):确认 .app bundle 结构完整,可用 ls -R build/HelloApp.app/Contents;检查二进制存在且可执行,可用 file build/HelloApp.app/Contents/MacOS/HelloApp。 签名后(Scripts/sign-and-notarize.sh 或 ad-hoc 开发签名):检查签名与 entitlements,可用 codesign -dv --verbose=4 build/HelloApp.app;本地验证 Gatekeeper,可用 spctl --assess --type execute --verbose build/HelloApp.app。 公证并装订后:确认装订票据已附加,可用 stapler validate build/HelloApp.app;再次运行 Gatekeeper 确认公证被识别,可用 spctl --assess --type execute --verbose build/HelloApp.app。
常见公证失败与恢复 “The software asset has already been uploaded”:同一版本重复提交,需在 version.env 中提升 BUILD_NUMBER 后重新打包。 “Package Invalid: Invalid Code Signing Entitlements”:.entitlements 文件中的 entitlements 与 provisioning 不匹配,需对照 Apple 允许集合审查并移除不支持的键。 “The executable does not have the hardened runtime enabled”:codesign 调用缺少 --options runtime,需编辑 sign-and-notarize.sh 为所有 codesign 调用添加 --options runtime。 公证挂起/无状态邮件:可能是 xcrun notarytool 网络或凭据问题,可运行 xcrun notarytool history 检查状态,若 App Store Connect API key 过期则重新导出。 公证成功但 stapler validate 失败:票据尚未传播,等待约 60 秒后重新运行 xcrun stapler staple。
模板说明 assets/templates/package_app.sh:构建二进制、创建 .app bundle、复制资源并签名。 assets/templates/compile_and_run.sh:开发循环,终止运行中的应用、打包并启动。 assets/templates/build_icon.sh:从 Icon Composer 文件生成 .icns(需安装 Xcode)。 assets/templates/sign-and-notarize.sh:公证、装订并压缩发布构建。 assets/templates/make_appcast.sh:生成 Sparkle appcast 更新条目。 assets/templates/setup_dev_signing.sh:创建稳定的开发代码签名身份。 assets/templates/launch.sh:打包 .app 的简单启动器。 assets/templates/version.env:打包脚本使用的示例版本文件。 assets/templates/bootstrap/:最小 SwiftPM macOS 应用骨架(Package.swift、Sources/、version.env)。
注意事项 保持 entitlements 与签名配置显式;编辑模板脚本而非重新实现。若不使用 Sparkle 更新,可移除 Sparkle 相关步骤。Sparkle 依赖 bundle build number(CFBundleVersion),因此每次更新必须提升 version.env 中的 BUILD_NUMBER。对于菜单栏应用,打包时设置 MENU_BAR_APP=1 以在 Info.plist 中输出 LSUIElement。
限制 仅在任务与上述范围明确匹配时使用该技能。不要将输出视为针对具体环境的验证、测试或专家审查的替代。若缺少必要输入、权限、安全边界或成功标准,应停止并请求澄清。