我可以用Swift包管理器做一个本地模块吗?

时间:2023-01-23 23:53:33

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包管理器可以从github编译代码,作为我项目的一个模块,但是我可以告诉包管理器编译本地存储在我的计算机上的代码吗?

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.

我的想法是我有一些代码,我想把它们从我的项目中分离出来,所以我将它保存在一个文件夹中,Swift编译器将构建它,这样代码就可以像其他模块一样被导入。

2 个解决方案

#1


12  

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.

您可以在包中引用本地目录。swift文件,但它必须是Git存储库。此外,初始化repo、提交和标记还不够;必须将存储库推到远程,以便快速构建能够正确运行。

According to the SwiftPM Usage Guide:

根据SwiftPM使用指南:

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.

包是Git存储库,带有语义版本,包含一个包。在它们的根上的swift文件。初始化包创建了一个包。swift文件,但是要使它成为一个可用的包,我们需要用至少一个版本标记初始化Git存储库。

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包管理器文档还声明“您可以为任何有效的Swift包指定一个URL(或本地路径)”,并提供一个示例包。带有本地文件引用的swift: .Package(url: ".. .. "/ StringExtensions”、“1.0.0”)。

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.

注意:我编辑了答案以澄清Swift包管理器可以引用本地路径,但是路径必须包含一个带有标记的有效Git存储库。我最初的测试项目指向一个独立的本地路径,该路径包含一个.git目录,因此它成功地使用swift构建。

#2


7  

With the Swift 4 version tools, there's different means to do this-- where you don't have to provide a tagged version:

使用Swift 4版本工具,有不同的方法可以做到这一点——在这里,您不必提供标记版本:

.package(url: "<path to repo>", .branch("master")),

See also: https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md

参见:https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md

#1


12  

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.

您可以在包中引用本地目录。swift文件,但它必须是Git存储库。此外,初始化repo、提交和标记还不够;必须将存储库推到远程,以便快速构建能够正确运行。

According to the SwiftPM Usage Guide:

根据SwiftPM使用指南:

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.

包是Git存储库,带有语义版本,包含一个包。在它们的根上的swift文件。初始化包创建了一个包。swift文件,但是要使它成为一个可用的包,我们需要用至少一个版本标记初始化Git存储库。

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包管理器文档还声明“您可以为任何有效的Swift包指定一个URL(或本地路径)”,并提供一个示例包。带有本地文件引用的swift: .Package(url: ".. .. "/ StringExtensions”、“1.0.0”)。

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.

注意:我编辑了答案以澄清Swift包管理器可以引用本地路径,但是路径必须包含一个带有标记的有效Git存储库。我最初的测试项目指向一个独立的本地路径,该路径包含一个.git目录,因此它成功地使用swift构建。

#2


7  

With the Swift 4 version tools, there's different means to do this-- where you don't have to provide a tagged version:

使用Swift 4版本工具,有不同的方法可以做到这一点——在这里,您不必提供标记版本:

.package(url: "<path to repo>", .branch("master")),

See also: https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md

参见:https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md