示例工程 48-code-signing-notarization
48-code-signing-notarization/Sources/Ch48Kit/Signing.swift
import Foundation
import SwiftUI
/// 签名与公证的命令计划:只构造参数,不执行,便于测试。
public struct SigningPlan: Sendable {
public let appPath: String
public let identity: String
public let entitlementsPath: String?
public let notaryProfile: String
public init(
appPath: String,
identity: String = "-",
entitlementsPath: String? = nil,
notaryProfile: String = "xwrt-notary"
) {
self.appPath = appPath
self.identity = identity
self.entitlementsPath = entitlementsPath
self.notaryProfile = notaryProfile
}
public var signArguments: [String] {
var arguments = [
"--force",
"--options", "runtime",
"--sign", identity,
]
if let entitlementsPath {
arguments += ["--entitlements", entitlementsPath]
}
arguments.append(appPath)
return arguments
}
public var verifyArguments: [String] {
["--verify", "--deep", "--strict", "--verbose=2", appPath]
}
public var assessArguments: [String] {
["--assess", "--type", "execute", "--verbose=4", appPath]
}
public var notaryArguments: [String] {
[
"notarytool", "submit", "\(appPath).zip",
"--keychain-profile", notaryProfile,
"--wait",
]
}
public var stapleArguments: [String] {
["stapler", "staple", appPath]
}
public func problems() -> [String] {
var issues: [String] = []
if appPath.isEmpty {
issues.append("缺少 App 路径")
}
if identity.isEmpty {
issues.append("缺少签名身份")
}
if notaryProfile.isEmpty {
issues.append("缺少公证 profile")
}
return issues
}
}
/// 执行外部命令并返回退出码与输出,便于演示真实签名结果。
public enum Shell {
public struct Result: Sendable {
public let status: Int32
public let output: String
}
public static func run(_ executable: String, _ arguments: [String]) -> Result {
let process = Process()
process.executableURL = URL(fileURLWithPath: executable)
process.arguments = arguments
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
do {
try process.run()
process.waitUntilExit()
} catch {
return Result(status: -1, output: "\(error)")
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
return Result(
status: process.terminationStatus,
output: String(decoding: data, as: UTF8.self)
.trimmingCharacters(in: .whitespacesAndNewlines)
)
}
}
/// 构造一个可签名的临时 `.app`:复制 `/bin/echo` 作为可执行文件。
public enum Fixture {
public static func makeApp(at root: URL, bundleID: String = "dev.xwrt.sigtest") throws -> URL {
let app = root.appendingPathComponent("Sig.app")
let macos = app.appendingPathComponent("Contents/MacOS")
try FileManager.default.createDirectory(at: macos, withIntermediateDirectories: true)
try FileManager.default.copyItem(
at: URL(fileURLWithPath: "/bin/echo"),
to: macos.appendingPathComponent("App")
)
let 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>CFBundleExecutable</key><string>App</string>
<key>CFBundleIdentifier</key><string>\(bundleID)</string>
<key>CFBundleName</key><string>Sig</string>
<key>CFBundlePackageType</key><string>APPL</string>
<key>CFBundleShortVersionString</key><string>1.0</string>
<key>CFBundleVersion</key><string>1</string>
</dict></plist>
"""
try plist.write(
to: app.appendingPathComponent("Contents/Info.plist"),
atomically: true,
encoding: .utf8
)
return app
}
}
/// 截图:签名与公证流程。
public struct SigningBoard: View {
public init() {}
private let steps: [(String, String)] = [
("codesign", "ad-hoc / Developer ID 签名"),
("verify", "--deep --strict 校验"),
("spctl", "Gatekeeper 评估"),
("notarytool", "上传公证(需账号)"),
("stapler", "装订票据"),
]
public var body: some View {
VStack(alignment: .leading, spacing: 9) {
Text("签名与公证").font(.system(size: 17, weight: .semibold))
ForEach(steps, id: \.0) { step in
HStack(spacing: 10) {
Text(step.0)
.font(.system(size: 10, design: .monospaced))
.frame(width: 100, alignment: .leading)
Text(step.1)
.font(.system(size: 11))
.foregroundStyle(.secondary)
Spacer()
}
}
}
.padding(18)
.frame(width: 360, alignment: .leading)
.background(Color.white)
}
}