如何在 swift 包管理器中使目标产品平台特定
How can you make the target products platform specific in swift package manager
我有以下通用架构。一个应用程序,它为我的所有模型数据导入一个 Swift 包,然后根据平台与 Firestore 一起使用或将 REST 定向到 firestore。我将 REST 用于 WatchOS,因为 firestore-swift 仍然不支持 WatchOS。
我的 Swift 包依赖于 Firestore,这不是问题。但是,我只希望目标部分中的目标可用于 iOS 和 MacOS,而不是包含在 WatchOS 中。我可以在包清单文件中指定吗?
这是我正在尝试做的事情的总图。
我的包裹清单如下所示。请注意底部的两条注释掉的行,只有当目标是 iOS 或 MacOS 时我才想包含它们。
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "EMobileLibrary",
platforms: [.iOS(.v15), .watchOS(.v8), .macOS(.v12)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "EMobileLibrary",
targets: ["EMobileLibrary"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "AppDevWithSwiftLibrary", url: "https://github.com/directoryname/AppDevWithSwiftLibrary", from: "1.11.6"),
.package(
name: "Firebase",
url: "https://github.com/firebase/firebase-ios-sdk.git",
.upToNextMajor(from: "8.10.0")
),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "EMobileLibrary",
dependencies: [
.product(name: "AppDevWithSwiftLibrary", package: "AppDevWithSwiftLibrary"),
// .product(name: "FirebaseFirestore", package: "Firebase"),
// .product(name: "FirebaseFirestoreSwift-Beta", package: "Firebase")
]
),
.testTarget(
name: "EMobileLibraryTests",
dependencies: ["EMobileLibrary"]),
]
)
定义一个具有平台依赖性的虚拟包装器目标。
这是来自 https://github.com/firebase/firebase-ios-sdk/blob/master/Package.swift 的示例摘录,其中将 FirebaseDynamicLinks 定义为仅在 iOS 上可用,但不适用于 tvOS、macOS 或 watchOS:
.library(
name: "FirebaseDynamicLinks",
targets: ["FirebaseDynamicLinksTarget"]
),
.
.
.
.target(
name: "FirebaseDynamicLinksTarget",
dependencies: [.target(name: "FirebaseDynamicLinks",
condition: .when(platforms: [.iOS]))],
path: "SwiftPM-PlatformExclude/FirebaseDynamicLinksWrap"
),
.target(
name: "FirebaseDynamicLinks",
....
我有以下通用架构。一个应用程序,它为我的所有模型数据导入一个 Swift 包,然后根据平台与 Firestore 一起使用或将 REST 定向到 firestore。我将 REST 用于 WatchOS,因为 firestore-swift 仍然不支持 WatchOS。
我的 Swift 包依赖于 Firestore,这不是问题。但是,我只希望目标部分中的目标可用于 iOS 和 MacOS,而不是包含在 WatchOS 中。我可以在包清单文件中指定吗?
这是我正在尝试做的事情的总图。
我的包裹清单如下所示。请注意底部的两条注释掉的行,只有当目标是 iOS 或 MacOS 时我才想包含它们。
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "EMobileLibrary",
platforms: [.iOS(.v15), .watchOS(.v8), .macOS(.v12)],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "EMobileLibrary",
targets: ["EMobileLibrary"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(name: "AppDevWithSwiftLibrary", url: "https://github.com/directoryname/AppDevWithSwiftLibrary", from: "1.11.6"),
.package(
name: "Firebase",
url: "https://github.com/firebase/firebase-ios-sdk.git",
.upToNextMajor(from: "8.10.0")
),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "EMobileLibrary",
dependencies: [
.product(name: "AppDevWithSwiftLibrary", package: "AppDevWithSwiftLibrary"),
// .product(name: "FirebaseFirestore", package: "Firebase"),
// .product(name: "FirebaseFirestoreSwift-Beta", package: "Firebase")
]
),
.testTarget(
name: "EMobileLibraryTests",
dependencies: ["EMobileLibrary"]),
]
)
定义一个具有平台依赖性的虚拟包装器目标。
这是来自 https://github.com/firebase/firebase-ios-sdk/blob/master/Package.swift 的示例摘录,其中将 FirebaseDynamicLinks 定义为仅在 iOS 上可用,但不适用于 tvOS、macOS 或 watchOS:
.library(
name: "FirebaseDynamicLinks",
targets: ["FirebaseDynamicLinksTarget"]
),
.
.
.
.target(
name: "FirebaseDynamicLinksTarget",
dependencies: [.target(name: "FirebaseDynamicLinks",
condition: .when(platforms: [.iOS]))],
path: "SwiftPM-PlatformExclude/FirebaseDynamicLinksWrap"
),
.target(
name: "FirebaseDynamicLinks",
....