问题描述
我正在尝试了解 Swift 包管理器清单 - Package.swift 在包装现有作品时,关于版本。例如,XMLDictioonary 是我想要将其公开给 SPM 的分支;到目前为止,我有:
// swift-tools-version:5.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "XMLDictionary",platforms: [
.macOS(.v10_13),.iOS(.v11),.tvOS(.v11),],products: [
// Products define the executables and libraries produced by a package,and make them visible to other packages.
.library(name: "XMLDictionary",targets: ["XMLDictionary"]),dependencies: [
// Dependencies declare other packages that this package depends on.
],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 which this package depends on.
.target(name: "XMLDictionary",dependencies: [],path: "XMLDictionary",publicHeadersPath: "."),]
)
只要我的版本规则指定 master,Xcode (12) 就支持哪个,但我希望它是 1.4.1,就像 upstream 一样。我可能会在未来的某个时候出现分歧。
但添加我认为应该是的
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/slashlos/XMLDictionary.git",from: "1.4.1"
)
Xcode SPM 添加失败
在此之前,本地构建失败:
error: cyclic dependency declaration found: XMLDictionary -> XMLDictionary
我认为这是在告诉我依赖规则是在它自身上的吗?
那么如何指定版本依赖?
解决方法
Package.swift 是您的框架的清单,它必须放在根目录中。你做的一切都是正确的。
如果你想在你的应用程序中导入你的框架,你应该指定 Github repo url 导入框架的位置,并最终指定版本号。该版本号由您在 Github 存储库中的标签获取。如果您设置为 1.4.1,SPM 将在您的存储库中检出代码在其被标记为 1.4.1 时(该标记对应于特定提交!)。但当时 Package.swift 不存在,因为您稍后添加了一些提交。
如果你想使用语义版本来导入你的框架,你应该在你最后一次提交时创建一个标签。因此,当 SPM 下载该标签时,它可以在其中找到您的 Package.swift 文件。