示例工程 60-capstone-release-and-retrospective

60-capstone-release-and-retrospective/Sources/Ch60Kit/Release.swift

swift · 9.2 KB

import CryptoKit
import Foundation
import SwiftUI
 
/// 语义化版本:三段数字,可比较、可升级。
public struct SemanticVersion: Comparable, Hashable, Sendable, CustomStringConvertible {
    public let major: Int
    public let minor: Int
    public let patch: Int
 
    public init(_ text: String) {
        let parts = text.split(separator: ".").compactMap { Int($0) }
        major = parts.count > 0 ? parts[0] : 0
        minor = parts.count > 1 ? parts[1] : 0
        patch = parts.count > 2 ? parts[2] : 0
    }
 
    public init(major: Int, minor: Int, patch: Int) {
        self.major = major
        self.minor = minor
        self.patch = patch
    }
 
    public var description: String { "\(major).\(minor).\(patch)" }
 
    /// 构建号:major*10000 + minor*100 + patch,单调递增。
    public var buildNumber: Int { major * 10_000 + minor * 100 + patch }
 
    public func bumped(_ component: ReleaseComponent) -> SemanticVersion {
        switch component {
        case .major: SemanticVersion(major: major + 1, minor: 0, patch: 0)
        case .minor: SemanticVersion(major: major, minor: minor + 1, patch: 0)
        case .patch: SemanticVersion(major: major, minor: minor, patch: patch + 1)
        }
    }
 
    public static func < (lhs: SemanticVersion, rhs: SemanticVersion) -> Bool {
        (lhs.major, lhs.minor, lhs.patch) < (rhs.major, rhs.minor, rhs.patch)
    }
}
 
public enum ReleaseComponent: String, CaseIterable, Sendable {
    case major
    case minor
    case patch
}
 
/// 发布步骤。顺序固定:先清干净,再构建、签名、打包、公证、装订、校验、发布。
public enum ReleaseStep: String, CaseIterable, Sendable {
    case clean
    case build
    case sign
    case package
    case notarize
    case staple
    case checksum
    case publish
 
    public var order: Int {
        switch self {
        case .clean: 0
        case .build: 1
        case .sign: 2
        case .package: 3
        case .notarize: 4
        case .staple: 5
        case .checksum: 6
        case .publish: 7
        }
    }
 
    public var title: String {
        switch self {
        case .clean: "清理"
        case .build: "构建"
        case .sign: "签名"
        case .package: "打包"
        case .notarize: "公证"
        case .staple: "装订"
        case .checksum: "校验和"
        case .publish: "发布"
        }
    }
}
 
public struct ReleasePlan: Sendable {
    public let steps: [ReleaseStep]
 
    public init(steps: [ReleaseStep] = ReleaseStep.allCases) {
        self.steps = steps
    }
 
    public var isComplete: Bool { Set(steps) == Set(ReleaseStep.allCases) }
 
    public var isOrdered: Bool {
        let orders = steps.map(\.order)
        return orders == orders.sorted()
    }
 
    public var missing: [ReleaseStep] {
        ReleaseStep.allCases.filter { !steps.contains($0) }
    }
 
    public var isReady: Bool { isComplete && isOrdered }
}
 
/// 打包元数据与 Info.plist 生成。
public struct BundleMetadata: Sendable {
    public let name: String
    public let identifier: String
    public let version: SemanticVersion
    public let build: Int
 
    public init(name: String, identifier: String, version: SemanticVersion, build: Int) {
        self.name = name
        self.identifier = identifier
        self.version = version
        self.build = build
    }
 
    public var isValid: Bool {
        !name.isEmpty && identifier.contains(".") && build > 0
    }
 
    public func infoPlist() -> String {
        let entries: [(String, String)] = [
            ("CFBundleExecutable", name),
            ("CFBundleIdentifier", identifier),
            ("CFBundleName", name),
            ("CFBundlePackageType", "APPL"),
            ("CFBundleShortVersionString", version.description),
            ("CFBundleVersion", String(build)),
        ]
        let body = entries.map { "<key>\($0.0)</key><string>\($0.1)</string>" }
            .joined(separator: "\n")
        return """
            <?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>
            \(body)
            </dict></plist>
            """
    }
}
 
/// 校验和:写进 checksums.txt,供 `shasum -c` 验证。
public enum Checksums {
    public static func sha256(of data: Data) -> String {
        SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
    }
 
    public static func manifestLine(name: String, data: Data) -> String {
        "\(sha256(of: data))  \(name)"
    }
}
 
/// 发布说明:标题 + 条目。
public struct ReleaseNotes: Sendable {
    public let version: SemanticVersion
    public let items: [String]
 
    public init(version: SemanticVersion, items: [String]) {
        self.version = version
        self.items = items
    }
 
    public func rendered() -> String {
        (["# \(version)", ""] + items.map { "- \($0)" }).joined(separator: "\n")
    }
}
 
/// 全书取舍清单:每条一个主题与最终决定。
public struct RetrospectiveItem: Hashable, Sendable {
    public let topic: String
    public let decision: String
 
    public init(topic: String, decision: String) {
        self.topic = topic
        self.decision = decision
    }
}
 
public enum Retrospective {
    public static let items: [RetrospectiveItem] = [
        RetrospectiveItem(topic: "值语义", decision: "struct 优先,类只在需要身份或继承时使用"),
        RetrospectiveItem(topic: "并发", decision: "async/await + actor,主线程用 @MainActor 标注"),
        RetrospectiveItem(topic: "错误", decision: "可恢复错误用 throws,编程错误用 precondition"),
        RetrospectiveItem(topic: "持久化", decision: "SwiftData 起步,复杂查询再下推谓词"),
        RetrospectiveItem(topic: "界面", decision: "组合小视图,状态尽量下沉到模型"),
        RetrospectiveItem(topic: "发布", decision: "脚本化的固定顺序,先签名后打包再公证"),
    ]
}
 
/// 迁移速查表:一行动一个概念,四列对照。
public struct MigrationRow: Hashable, Sendable {
    public let concept: String
    public let c: String
    public let go: String
    public let java: String
    public let swift: String
 
    public init(concept: String, c: String, go: String, java: String, swift: String) {
        self.concept = concept
        self.c = c
        self.go = go
        self.java = java
        self.swift = swift
    }
}
 
public enum MigrationTable {
    public static let rows: [MigrationRow] = [
        MigrationRow(
            concept: "接口", c: "函数指针", go: "interface", java: "interface", swift: "protocol"),
        MigrationRow(
            concept: "泛型", c: "void*", go: "类型参数", java: "泛型", swift: "泛型 + 约束"),
        MigrationRow(
            concept: "错误", c: "返回码", go: "error", java: "异常", swift: "throws / Result"),
        MigrationRow(
            concept: "并发", c: "pthread", go: "goroutine", java: "Thread", swift: "Task / actor"),
        MigrationRow(
            concept: "内存", c: "malloc/free", go: "GC", java: "GC", swift: "ARC"),
        MigrationRow(
            concept: "字符串", c: "char*", go: "string", java: "String", swift: "String(Unicode)"),
        MigrationRow(
            concept: "集合", c: "数组 + 长度", go: "slice/map", java: "List/Map",
            swift: "Array/Dictionary"),
        MigrationRow(
            concept: "空值", c: "NULL", go: "nil + ok", java: "null", swift: "Optional"),
    ]
}
 
/// 外部命令参数构造(真实执行见 scripts/release.sh)。
public enum ReleaseCommands {
    public static func codesignArguments(appPath: String, identity: String) -> [String] {
        ["--force", "--deep", "--options", "runtime", "--sign", identity, appPath]
    }
 
    public static func notarizeArguments(dmgPath: String, profile: String) -> [String] {
        ["notarytool", "submit", dmgPath, "--keychain-profile", profile, "--wait"]
    }
 
    public static func stapleArguments(dmgPath: String) -> [String] {
        ["stapler", "staple", dmgPath]
    }
}
 
/// 截图:发布与回顾板。
public struct ReleaseBoard: View {
    public init() {}
 
    private let rows: [(String, String)] = [
        ("版本", "1.0.0(构建 10000)"),
        ("签名", "Developer ID + 硬化运行时"),
        ("打包", "DMG + sha256 校验和"),
        ("公证", "notarytool 提交 + stapler 装订"),
        ("更新", "JSON 源 + 版本比较"),
        ("回顾", "取舍清单 + 迁移速查表"),
    ]
 
    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: 42, alignment: .leading)
                    Text(row.1)
                        .font(.system(size: 11))
                        .foregroundStyle(.secondary)
                    Spacer()
                }
            }
        }
        .padding(18)
        .frame(width: 360, alignment: .leading)
        .background(Color.white)
    }
}