我有一个 mono cocoapods 存储库,它托管我们所有的库。我正在添加对 Apple SPM 的支持。一切都在 package.swift 配置上进行。但是,我尝试具有与 Firebase SPM 包相同的行为。
行为是当有人想要添加我们的包时,所有包都是可选的,并且在 Xcode 中将“添加到目标”设置为无,以提供对用户想要导入的内容的完全控制(参见图片)。知道我错过了什么吗?
我尝试过使用目标包装器,一个没有依赖项的虚拟空包作为主要产品,但似乎没有任何效果
这是一个混淆的 package.swift
// swift-tools-version: 5.9
import PackageDescription
let package = Package(
name: "MyPackages",
platforms: [.iOS(.v13)],
products: [
.library(
name: "MyPackageA",
targets: ["MyPackageA"]
),
.library(
name: "MyPackageB",
targets: ["MyPackageB"]
)
],
dependencies: [
],
targets: [
.target(
name: "MyPackageA",
dependencies: ["MyCorePackageBinary", "MyPackageABinary"],
path: "MyPackageAPath"
),
.target(
name: "MyPackageB",
dependencies: ["MyCorePackageBinary", "MyPackageBBinary"],
path: "MyPackageBPath"
),
.binaryTarget(
name: "MyCorePackageBinary",
path: "MyPath/MyCorePackageBinary.xcframework"
),
.binaryTarget(
name: "MyPackageABinary",
path: "MyPathA/MyPackageABinary.xcframework"
),
.binaryTarget(
name: "MyPackageBBinary",
path: "MyPathB/MyPackageBBinary.xcframework"
)
]
)
要复制 Firebase 的 Swift 包管理器 (SPM) 设置中的行为(其中所有包均显示为可选,并且在 Xcode 中将“添加到目标”设置为无),您需要仔细构建 Package.swift 文件。这涉及定义顶级包而不指定依赖项,从而自动将它们添加到用户的项目中。
以下是如何调整 Package.swift 来实现此行为: 主要变化:
Avoid Default Dependencies: Ensure your top-level package doesn’t directly depend on the libraries. This way, nothing is imported automatically unless the user explicitly adds it to their target.
Modular Library Products: Each library (like MyPackageA and MyPackageB) must be defined as a standalone product, without requiring a parent dependency.
这是调整后的Package.swift文件: // swift-tools-版本:5.9 导入包描述
让包=包( 名称:“我的包裹”, 平台:[.iOS(.v13)], 产品: [ // 每个库都是自己的产品,让用户可以控制要添加的内容。 。图书馆( 名称:“我的包A”, 目标:[“MyPackageA”] ), 。图书馆( 名称:“我的包B”, 目标:[“MyPackageB”] ) ], 依赖项:[ // 主包中没有默认依赖项。 ], 目标:[ // 定义库的目标,保持它们独立。 。目标( 名称:“我的包A”, 依赖项:[“MyCorePackageBinary”,“MyPackageABinary”], 路径:“MyPackageAPath” ), 。目标( 名称:“我的包B”, 依赖项:[“MyCorePackageBinary”,“MyPackageBBinary”], 路径:“MyPackageBPath” ), // 核心功能和各个库的二进制目标。 .binaryTarget( 名称:“MyCorePackageBinary”, 路径:“MyPath/MyCorePackageBinary.xcframework” ), .binaryTarget( 名称:“MyPackageABinary”, 路径:“MyPathA/MyPackageABinary.xcframework” ), .binaryTarget( 名称:“MyPackageBBinary”, 路径:“MyPathB/MyPackageBBinary.xcframework” ) ] ) 说明:
Separate Products: Each library (MyPackageA and MyPackageB) is defined as a separate product. This ensures they don’t get bundled into a single target by default.
Binary Targets: You’ve already defined binary targets (MyCorePackageBinary, MyPackageABinary, etc.), which are included in the respective library targets. This keeps them modular.
No Implicit Dependencies: By not defining a top-level library product that bundles the dependencies, users are given complete control over what they import.
在 Xcode 中测试:
Open Xcode and add your SPM repository.
Notice that each library (MyPackageA and MyPackageB) will appear as optional, with "Add to Target" set to none by default.
The user can manually add only the libraries they need to their targets.
此结构反映了 Firebase 的 SPM 设置,为用户提供完整的模块化和控制。