我可以使用Swift Package Manager制作本地模块吗?

问题描述:

我知道Swift软件包管理器可以将github的代码作为我的项目的模块进行编译,但是我可以告诉软件包管理器来编译存储在本地计算机上的代码吗?

I know the Swift package manager can compile code from github as a module for my project, but can I tell the package manager to compile code that is stored locally on my computer instead?

我的想法是我有一些要与项目其余部分分开的代码,因此我将其保存在一个文件夹中,Swift编译器将对其进行构建,以便可以像导入其他模块一样导入该代码。

The idea is I have some code that I want to separate off from the rest of my project, so I keep it in a folder and the Swift compiler will build it so that that code can be imported like any other module.

您可以在 Package.swift 中引用本地目录文件,但必须是Git存储库。另外,初始化存储库,提交和标记还不够。必须将存储库推送到远程位置,以便 swift build 正常运行。

You can reference a local directory in your Package.swift file, but it must be a Git repository. Also, initializing the repo, committing, and tagging is not sufficient; the repository must be pushed to a remote for swift build to function correctly.

根据 SwiftPM使用指南


软件包是Git存储库,带有语义版本标记,其根目录中包含Package.swift文件。初始化该软件包会创建一个Package.swift文件,但要使其成为可用的软件包,我们需要使用至少一个版本标签来初始化Git存储库。

Packages are Git repositories, tagged with semantic versions, containing a Package.swift file at their root. Initializing the package created a Package.swift file, but to make it a usable package we need to initialize a Git repository with at least one version tag.

Swift Package Manager文档也指出您可以为任何有效的Swift软件包指定URL(或本地路径);并提供示例 Package.swift 并带有本地文件引用: .Package(url: ../StringExtensions, 1.0.0& ;)

The Swift Package Manager Documentation also states that "you can specify a URL (or local path) to any valid Swift package" and provides an example Package.swift with a local file reference: .Package(url: "../StringExtensions", "1.0.0").

注意:我编辑了答案以阐明Swift Package Manager可以引用本地路径,但是路径必须包含带有标签的有效Git存储库。我最初的测试项目指向一个包含 .git 目录的相关本地路径,因此它成功地通过 swift build 进行了构建>。

Note: I edited the answer to clarify that Swift Package Manager can reference a local path, but the path must contain a valid Git repository with a tag. My original test project pointed to a dependent local path that contained a .git directory, and so it successfully built with swift build.