示例工程 57-ios-quality-gates

57-ios-quality-gates/Sources/Ch57Kit/Quality.swift

swift · 3.8 KB

import Foundation
import SwiftUI
 
public struct SimulatorDevice: Hashable, Sendable {
    public let name: String
    public let runtime: String
    public let available: Bool
 
    public init(name: String, runtime: String, available: Bool = true) {
        self.name = name
        self.runtime = runtime
        self.available = available
    }
}
 
public enum DeviceMatrix {
    public static let required: [SimulatorDevice] = [
        SimulatorDevice(name: "iPhone 17", runtime: "iOS 26.0"),
        SimulatorDevice(name: "iPhone 17 Pro Max", runtime: "iOS 26.0"),
        SimulatorDevice(name: "iPad Pro M5", runtime: "iOS 26.0"),
    ]
 
    public static var available: [SimulatorDevice] {
        required.filter(\.available)
    }
 
    public static var destinations: [String] {
        available.map { "platform=iOS Simulator,name=\($0.name),OS=26.0" }
    }
}
 
public struct QualityGate: Hashable, Sendable {
    public let name: String
    public let passed: Bool
    public let detail: String
 
    public init(name: String, passed: Bool, detail: String) {
        self.name = name
        self.passed = passed
        self.detail = detail
    }
}
 
public enum GateRunner {
    public static func summarize(_ gates: [QualityGate]) -> (passed: Int, failed: Int) {
        let passed = gates.filter(\.passed).count
        return (passed, gates.count - passed)
    }
 
    public static func allPassed(_ gates: [QualityGate]) -> Bool {
        gates.allSatisfy(\.passed)
    }
}
 
public struct TestCommand {
    public let project: String
    public let scheme: String
    public let destinations: [String]
    public let resultBundle: String
 
    public init(project: String, scheme: String, destinations: [String], resultBundle: String) {
        self.project = project
        self.scheme = scheme
        self.destinations = destinations
        self.resultBundle = resultBundle
    }
 
    public var arguments: [String] {
        var args = ["-project", project, "-scheme", scheme]
        for destination in destinations {
            args += ["-destination", destination]
        }
        args += ["-resultBundlePath", resultBundle, "test"]
        return args
    }
 
    public var isValid: Bool {
        !project.isEmpty && !scheme.isEmpty && !destinations.isEmpty
    }
}
 
public struct SizeReport: Sendable {
    public let downloadMB: Double
    public let installMB: Double
    public let budgetMB: Double
 
    public init(downloadMB: Double, installMB: Double, budgetMB: Double = 200) {
        self.downloadMB = downloadMB
        self.installMB = installMB
        self.budgetMB = budgetMB
    }
 
    public var withinBudget: Bool {
        installMB <= budgetMB
    }
 
    public var ratio: Double {
        budgetMB == 0 ? 0 : installMB / budgetMB
    }
}
 
public struct QualityBoard: View {
    public init() {}
 
    private let rows: [(String, String)] = [
        ("矩阵", "iPhone 17 / Pro Max / iPad"),
        ("测试", "xcodebuild test + result bundle"),
        ("性能", "启动、滚动、内存"),
        ("体积", "App Thinning Size Report"),
        ("无障碍", "Inspector + Dynamic Type"),
        ("发布", "签名、截图、隐私清单"),
    ]
 
    public var body: some View {
        VStack(alignment: .leading, spacing: 9) {
            Text("iOS 质量门禁").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: 60, alignment: .leading)
                    Text(row.1)
                        .font(.system(size: 11))
                        .foregroundStyle(.secondary)
                    Spacer()
                }
            }
        }
        .padding(18)
        .frame(width: 340, alignment: .leading)
        .background(Color.white)
    }
}