如何使用提供两个库的本地 Swift Package?

Qua*_*eur 5 code-structure swift swift-package-manager swiftpm

我有一个本地 swift 包Foo,它提供了两个不同的库FooFooB。现在我想在另一个本地包Bar 中使用它们。我只能通过路径声明来获取整个包。有没有办法命名/指定应使用哪个库?我想在我的Bar包中使用FooB

let package = Package(
    name: "Foo",
    products: [
        .library(name: "Foo", targets: ["Foo"]),
        .library(name: "FooB", targets: ["FooB"])
    ]

...)
Run Code Online (Sandbox Code Playgroud)
let package = Package(
    name: "Bar",
   dependencies: [
        .package(path: "Foo"),
        .package(path: "FooB") // this one does not work
    ],

...)
Run Code Online (Sandbox Code Playgroud)
// inside package Bar

import Foo
import FooB // this is throwing "no such module 'FooB'"
Run Code Online (Sandbox Code Playgroud)

bsh*_*ley 1

这是一个具体的例子:

\n

\n

正在播放Foo, IGListKit Package.swift

\n

摘录:

\n
products: [\n    .library(name: "IGListDiffKit",\n             type: .static,\n             targets: ["IGListDiffKit"]),\n    .library(name: "IGListKit",\n             type: .static,\n             targets: ["IGListKit"]),\n
Run Code Online (Sandbox Code Playgroud)\n

\xe2\x80\xa6

\n
targets: [\n    .target(\n        name: "IGListDiffKit",\n        path: "spm/Sources/IGListDiffKit"\n    ),\n    .target(\n        name: "IGListKit",\n        dependencies: ["IGListDiffKit"],\n        path: "spm/Sources/IGListKit"\n    ),\n
Run Code Online (Sandbox Code Playgroud)\n

酒吧

\n

就 的整体而言Bar

\n
import PackageDescription\n\nlet package = Package(\n    name: "HistoryList",\n    platforms: [.iOS(.v13)],\n    products: [\n        .library(\n            name: "HistoryList",\n            targets: ["HistoryList"]),\n    ],\n    dependencies: [\n        .package(name: "IGList", url: "https://github.com/Instagram/IGListKit", from: "4.0.0"),\n    ],\n    targets: [\n        .target(\n            name: "HistoryList",\n            dependencies: [\n                .product(name: "IGListDiffKit", package: "IGList"),\n                .product(name: "IGListKit", package: "IGList")\n            ]\n        ),\n    ]\n)\n
Run Code Online (Sandbox Code Playgroud)\n

修剪包

\n

导入后, Package DependenciesFoo部分中的Package.swift仅说明:

\n
let package = Package(\n    name: "IGListKit",\n    products: [\n        .library(name: "IGListKit", targets: ["IGListKit"])\n    ],\n    targets: [\n        .target(\n            name: "IGListKit",\n            path: "Source"\n        )\n    ]\n)\n
Run Code Online (Sandbox Code Playgroud)\n

Xcode / SPM 似乎正在修剪导入的包,以仅包含该包中的三个库之一。(最初,第二个库的代码丢失了,我的语法导致它被拉出,但第一个库没有看到。它们现在都在那里,但只看到一个。)

\n

我无法确定一种语法来使其同时关闭两者。

\n

当前错误

\n
product \'IGListDiffKit\' required by package \'ios-malauzai-history-list\' target \'HistoryList\' not found in package \'IGList\'.\n
Run Code Online (Sandbox Code Playgroud)\n

Xcode 15.0 测试版 (15A5160n)

\n

额外的想法

\n

package(name:url:_:)(以及name的属性Package.Dependency)已被弃用,但您需要使用包依赖项的名称在特定目标依赖项中使用。

\n

这让我相信应该使用一种更新的语法。

\n

解决

\n

Instagram 不完全支持 IGListKit 的 Swift Package Manager

\n

(有比 IGListKit 更好的解决方案,但这是在遗留代码中。目前,4.0.0标签和存储库的 HEAD 之间有 191 次提交;距离发布已经 3 年多了。)

\n

本例中的问题是,最新的标签(截至 2023 年 6 月)是标签4.0.0,文件的版本Package.swift实际上是我收到的缩写版本。

\n

当我更新时一切正常,并简化了描述以查看添加支持的提交。(本来可以使用main分支,但这会不断变化。)

\n
\xe2\x80\xa6\n    dependencies: [\n        .package(url: "https://github.com/Instagram/IGListKit", revision: "c9e045c9425a160871a7915260d07a6c273b548d")\n    ],\n    targets: [\n        .target(\n            name: "HistoryList",\n            dependencies: [\n                .product(name: "IGListKit", package: "IGListKit"),\n                .product(name: "IGListDiffKit", package: "IGListKit")\n            ]\n        ),\n\xe2\x80\xa6\n
Run Code Online (Sandbox Code Playgroud)\n