Simply put: a package is a git repository with semantically versioned tags,
that contains Swift sources and a Package.swift
manifest file at its root.
If you are building an app with several modules, at some point you may decide to make that module into an external package. Doing this makes that code available as a dependable library that others may use.
Doing so with the package manager is relatively simple:
- Create a new repository on GitHub
- In a terminal, step into the module directory
git init
git remote add origin [github-URL]
git add .
git commit --message="…"
git tag 1.0.0
git push origin master --tags
Now delete the subdirectory,
and amend your Package.swift
so that its package
declaration includes:
let package = Package(
dependencies: [
.Package(url: "…", versions: Version(1,0,0)..<Version(2,0,0)),
]
)
Now type swift build
If you are developing an app that consumes a package and you need to work on that package simultaneously then you have several options:
-
Edit the sources the package manager clones
The sources are cloned visibly into
./Packages
to facilitate this. -
Alter your
Package.swift
so it refers to a local clone of the packageThis can be tedious however as you will need to force an update every time you make a change, including updating the version tag.
Both options are currently non-ideal since it is easy to commit code that will break for other
members of your team, for example, if you change the sources for Foo
and then commit a change to
your app that uses those new changes but you have not committed those changes to Foo
then you have
caused dependency hell for your co-workers.
It is our intention to provide tooling to prevent such situations, but for now please be aware of the caveats.
You may be working with code that builds both as a package and not. For example, you may be packaging a project that also builds with Xcode.
In these cases, you can use the build configuration SWIFT_PACKAGE
to conditionally compile code for Swift packages.
#if SWIFT_PACKAGE
import Foundation
#endif