问题
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?
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.
回答1:
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.
According to the SwiftPM Usage Guide:
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.
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")
.
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
.
回答2:
With the Swift 4 version tools, there's different means to do this-- where you don't have to provide a tagged version:
.package(url: "<path to repo>", .branch("master")),
See also: https://github.com/apple/swift-package-manager/blob/master/Documentation/PackageDescriptionV4.md
回答3:
You can also use
.package(path: "path to folder")
if you don't want the overhead of an additional git repo.
来源:https://stackoverflow.com/questions/40775726/can-i-make-a-local-module-with-the-swift-package-manager