import CryptoKit import Foundation import SwiftUI /// 语义化版本的最小实现:数字段比较,支持不同长度。 public struct Version: Comparable, CustomStringConvertible, Sendable { public let components: [Int] public init(_ text: String) { components = text.split(separator: ".").map { Int($0) ?? 0 } } public var description: String { components.map(String.init).joined(separator: ".") } public static func == (lhs: Version, rhs: Version) -> Bool { !(lhs < rhs) && !(rhs < lhs) } public static func < (lhs: Version, rhs: Version) -> Bool { let count = max(lhs.components.count, rhs.components.count) for index in 0.. [UpdateEntry] { try JSONDecoder().decode([UpdateEntry].self, from: data) } public static func latest( _ entries: [UpdateEntry], newerThan current: Version, system: Version ) -> UpdateEntry? { entries .filter { Version($0.version) > current } .filter { system >= Version($0.minimumSystem) } .max { Version($0.version) < Version($1.version) } } } /// 发布物清单:文件名与校验和。 public struct ReleaseManifest: Sendable { public let version: String public let build: String public let dmgName: String public let sha256: String public init(version: String, build: String, dmgName: String, sha256: String) { self.version = version self.build = build self.dmgName = dmgName self.sha256 = sha256 } public var isValid: Bool { !version.isEmpty && !build.isEmpty && dmgName.hasSuffix(".dmg") && sha256.count == 64 } public var checksumLine: String { "\(sha256) \(dmgName)" } } public enum Checksums { public static func sha256(of data: Data) -> String { SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined() } } /// 打包命令:DMG 与校验和。 public enum Packaging { public static func createArguments(appPath: String, dmgPath: String, volumeName: String) -> [String] { [ "create", dmgPath, "-volname", volumeName, "-srcfolder", appPath, "-ov", "-format", "UDZO", ] } public static func verifyArguments(dmgPath: String) -> [String] { ["verify", dmgPath] } } /// 执行外部命令。 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) ) } } /// 构造一个最小 `.app` 作为打包素材。 public enum Fixture { public static func makeApp(at root: URL, name: String = "Demo") throws -> URL { let app = root.appendingPathComponent("\(name).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(name) ) let plist = """ CFBundleExecutable\(name) CFBundleIdentifierdev.xwrt.tutorial.pack CFBundleName\(name) CFBundlePackageTypeAPPL CFBundleShortVersionString1.0 CFBundleVersion1 """ try plist.write( to: app.appendingPathComponent("Contents/Info.plist"), atomically: true, encoding: .utf8 ) return app } } /// 截图:发布流程板。 public struct ReleaseBoard: View { public init() {} private let rows: [(String, String)] = [ ("构建", "Release + 签名 + 公证"), ("打包", "hdiutil 生成 UDZO DMG"), ("校验", "sha256 + hdiutil verify"), ("更新源", "JSON appcast + 版本比较"), ("下载", "校验和 + 原子替换"), ("回滚", "保留上一版与校验和"), ] public var body: some View { VStack(alignment: .leading, spacing: 9) { Text("打包与分发").font(.system(size: 17, weight: .semibold)) ForEach(rows, id: \.0) { row in HStack(spacing: 10) { Text(row.0) .font(.system(size: 10, design: .monospaced)) .frame(width: 70, alignment: .leading) Text(row.1) .font(.system(size: 11)) .foregroundStyle(.secondary) Spacer() } } } .padding(18) .frame(width: 340, alignment: .leading) .background(Color.white) } }