XCBuildKit

XCBuildKit 是一个强大的 Swift 包,它为 Xcode 构建命令提供了一个清晰的、类型安全的封装。它使您能够使用现代 Swift API 以编程方式管理 Xcode 构建。

Swift License: MIT

特性

安装

使用 Swift Package Manager 将 XCBuildKit 添加到您的项目中

dependencies: [
    .package(url: "https://github.com/engali94/XCBuildKit.git", from: "0.0.1")
]

快速开始

import XCBuildKit

// Initialize the build system
let xcodeBuild = XcodeBuild()

// Configure build options using the convenience factory method
let options = XcodeBuildOptions.forBuild(
    project: "MyApp",
    scheme: "MyApp",
    configuration: "Release",
    sdk: .iPhoneOS,
    destination: .iOSSimulator(device: "iPhone 14")
)

// Execute the build and handle real-time output
for await state in xcodeBuild.execute(.build, options: options) {
    switch state {
    case .inProgress(let message):
        print("Building: \(message)")
    case .completed:
        print("Build completed successfully!")
    case .error(let message):
        print("Build failed: \(message)")
    }
}

高级用法

运行测试

// Configure test options
let testOptions = XcodeBuildOptions.forTesting(
    project: "MyApp",
    scheme: "MyApp",
    destination: .iOSSimulator(device: "iPhone 14"),
    testPlan: "MyTestPlan",
    testConfiguration: nil,
    testLanguage: nil,
    testRegion: nil,
    skipTesting: [],
    onlyTesting: ["MyAppTests/LoginTests"],
    testTargets: [],
    enableCodeCoverage: true
)

// Execute tests with real-time feedback
for await state in xcodeBuild.execute(.test(testPlan: "MyTestPlan"), options: testOptions) {
    switch state {
    case .inProgress(let message):
        print("Testing: \(message)")
    case .completed:
        print("Tests completed successfully!")
    case .error(let message):
        print("Tests failed: \(message)")
    }
}

创建归档

// Configure archive options
let archivePath = try ArchivePath("path/to/output.xcarchive")
let archiveOptions = XcodeBuildOptions.forArchive(
    project: "MyApp",
    scheme: "MyApp",
    configuration: "Release",
    archivePath: archivePath,
    allowProvisioningUpdates: true
)

// Create archive
for await state in xcodeBuild.execute(.archive, options: archiveOptions) {
    switch state {
    case .inProgress(let message):
        print("Archiving: \(message)")
    case .completed:
        print("Archive created successfully!")
    case .error(let message):
        print("Archive failed: \(message)")
    }
}

导出归档

let exportOptions = try ExportOptions(
    archivePath: "path/to/archive.xcarchive",
    exportPath: "path/to/export",
    optionsPlist: "path/to/options.plist"
)

let options = XcodeBuildOptions.forExport(
    project: "MyApp",
    scheme: "MyApp",
    exportOptions: exportOptions,
    allowProvisioningUpdates: true
)

for await state in xcodeBuild.execute(.exportArchive, options: options) {
    switch state {
    case .inProgress(let message):
        print("Exporting: \(message)")
    case .completed:
        print("Export completed successfully!")
    case .error(let message):
        print("Export failed: \(message)")
    }
}

错误处理

XCBuildKit 通过 BuildState 枚举提供详细的错误信息

for await state in xcodeBuild.execute(.build, options: options) {
    switch state {
    case .error(let message):
        if message.contains("No provisioning profile") {
            // Handle provisioning profile errors
        } else if message.contains("Code signing") {
            // Handle code signing errors
        } else {
            // Handle other build errors
        }
    default:
        break
    }
}

要求

许可证

本项目采用 MIT 许可证 - 详情请参见 LICENSE 文件。

贡献

欢迎贡献! 请随时提交 Pull Request。