Swift Package Manager:两个相似的目标,仅依赖项不同

Ric*_*hiy 5 dependencies ios swift swift-package-manager swift-package

我正在将作为 Xcode 项目构建和测试的库迁移到 Swift Package Manager。该项目包含 3 个目标:库本身、具有不同依赖关系的相同库以及测试(应用程序)目标。

到目前为止,移植库很容易。

现在我有兴趣构建具有不同依赖项(模拟)的相同库,可以对其进行测试。

实际上,Package.swift 文件如下所示:

// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "TargetLibrary",
    platforms: [
        .iOS(.v14), .macOS(.v10_15)
    ],
    products: [
        .library(
            name: "TargetLibrary",
            targets: ["TargetLibrary"]
        ),
    ],
    dependencies: [
        .package(
            url: "ssh://git@github.com/DependentLib",
            .upToNextMinor(from: "1.3.18")
        ),
    ],
    targets: [
        .target(
            name: "TargetLibrary",
            dependencies: [
                // Using the "main" dependency for the "TargetLibrary"
                .product(name: "DependentLib", package: "DependentLib"),
            ],
            path: "src",
            sources: [
                "sourcefiles"
            ],
            publicHeadersPath: "SwiftPackage/include",
            cxxSettings: [
                .headerSearchPath("lib"),
            ]
        ),
        
        // This target is virtually identical, to the previous one,
        // the only difference is that it is using a different dependency library
        // from the same package
        .target(
            name: "TargetLibraryWithMock",
            dependencies: [
                // Using the "mock" dependency for this library
                .product(name: "DependentLibMock", package: "DependentLib"),
            ],
            path: "src",
            sources: [
                "sourcefiles"
            ],
            publicHeadersPath: "SwiftPackage/include",
            cxxSettings: [
                .headerSearchPath("lib"),
            ]
        ),
        
        // Now testing the "TargetLibraryWithMock"
        .testTarget()....
        
    ],
    cxxLanguageStandard: .cxx14
)


Run Code Online (Sandbox Code Playgroud)

当然,现在,如果我只是复制两个目标,我会收到目标重叠源错误

所以,问题是:如何构建两个仅因它们包含的依赖项而不同的目标?

看起来目标依赖条件可能会有所帮助

            dependencies: [
                .target(name: "DependentLib", condition: .when(configuration: .build)),
                .target(name: "DependentLibMock", condition: .when(configuration: .test)),
            ]
Run Code Online (Sandbox Code Playgroud)

但现在只存在特定于平台的条件。有什么办法可以解决这个问题?

所需结果的图形表示:

在此输入图像描述