Skip to content

Commit e2c2f0a

Browse files
heckjbripeticca
andauthored
Migrate remaining content from documentation/Usage.md into DocC articles (#8734)
Migrate remaining content from documentation/Usage.md into DocC articles ### Motivation: Migrate existing documentation from plain markdown into DocC and slightly update content when outdated. ### Modifications: Added the following new articles: - Sources/PackageManagerDocs/Documentation.docc/BundlingResources.md - Sources/PackageManagerDocs/Documentation.docc/CreatingSwiftPackage.md - Sources/PackageManagerDocs/Documentation.docc/Dependencies/EditingDependencyPackage.md - Sources/PackageManagerDocs/Documentation.docc/ReleasingPublishingAPackage.md - Sources/PackageManagerDocs/Documentation.docc/SettingSwiftToolsVersion.md - Sources/PackageManagerDocs/Documentation.docc/SwiftVersionSpecificPackaging.md - Sources/PackageManagerDocs/Documentation.docc/UsingBuildConfigurations.md - Sources/PackageManagerDocs/Documentation.docc/UsingShellCompletion.md Updated the following articles to accommodate the new articles: - Sources/PackageManagerDocs/Documentation.docc/Documentation.md - Sources/PackageManagerDocs/Documentation.docc/AddingDependencies.md - Sources/PackageManagerDocs/Documentation.docc/ResolvingPackageVersions.md resolves #8593 --------- Co-authored-by: Bri Peticca <[email protected]>
1 parent fab8843 commit e2c2f0a

11 files changed

+468
-3
lines changed

Sources/PackageManagerDocs/Documentation.docc/AddingDependencies.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ For more information on creating a binary target, see [Creating a multiplatform
8080
- <doc:ResolvingDependencyFailures>
8181
- <doc:AddingSystemLibraryDependency>
8282
- <doc:ExampleSystemLibraryPkgConfig>
83+
- <doc:EditingDependencyPackage>
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Bundling resources with a Swift package
2+
3+
Add resource files to your Swift package and access them in your code.
4+
5+
## Overview
6+
7+
If you declare `// swift-tools-version: 5.3` or later in your `Package.swift` file, you can bundle resources alongside your source code in Swift packages.
8+
For example, Swift packages can contain asset catalogs, test fixtures, and so on.
9+
10+
### Add resource files
11+
12+
Package manager treats non-source files found in the target's sources directory as assets, scoped to that target.
13+
For example, any resources for the `MyLibrary` target reside by default in `Sources/MyLibrary`.
14+
To easily distinguish resources from source files, create and use a subfolder for the resources.
15+
For example, put all resource files into a directory named `Resources`, resulting in all of your resource files residing at `Sources/MyLibrary/Resources`.
16+
17+
### Explicitly declare or exclude resources
18+
19+
To add a resource that the compiler doesn't handle automatically, explicitly declare it as a resource in your package manifest.
20+
If you're building your package with Xcode, it automatically handles a number of kinds of resources.
21+
22+
For example, to include a file `text.txt` as a resource, add the file into `Sources/MyLibrary/Resources`.
23+
Then explicitly declare it as a package resource by adding the name of the file to the list of resources for your target:
24+
25+
```swift
26+
targets: [
27+
.target(
28+
name: "MyLibrary",
29+
resources: [
30+
.process("Resources/text.txt")]
31+
),
32+
]
33+
```
34+
35+
The example above uses [process(_:localization:)](https://developer.apple.com/documentation/PackageDescription/Resource/process(_:localization:)) to identify the resource.
36+
When you explicitly declare a resource, choose a rule to determine how Swift treats the resource file.
37+
The options include:
38+
39+
- term Process rule: For most use cases, use [process(_:localization:)](https://developer.apple.com/documentation/PackageDescription/Resource/process(_:localization:)). This requests the compiler to apply any processing known for the type of resource, according to the platform you’re building the package for.
40+
For example, Xcode may optimize image files for a platform that supports such optimizations.
41+
If you apply the process rule to a directory’s path, Xcode applies the rule recursively to all files within the directory.
42+
If no special processing is available for a resource, the compiler copies the resource as is to the resource bundle’s top-level directory.
43+
44+
- term Copy rule: Some Swift packages may require a resource file to remain untouched or to retain a certain directory structure for resources.
45+
Use the [copy(_:)](https://developer.apple.com/documentation/PackageDescription/Resource/copy(_:)) function to apply this rule and copy the resource as is to the top level of the resource bundle.
46+
If you pass a directory path to the copy rule, the compiler retains the directory’s structure.
47+
48+
If a file resides inside a target’s folder and you don’t want it to be a package resource, pass it to the target initializer’s `exclude` parameter.
49+
For example, if you have a file called `instructions.md` in the sources directory, meant only for local use and not intended to be bundled, use `exclude`:
50+
51+
```swift
52+
targets: [
53+
.target(
54+
name: "MyLibrary",
55+
exclude:["instructions.md"]
56+
),
57+
]
58+
```
59+
60+
In general, avoid placing files that aren’t resources in a target's source folder.
61+
If that's not feasible, avoid excluding every file individually, place all files you want to exclude in a directory, and add the directory path to the array of excluded files.
62+
Swift Package Manager warns you about files it doesn't recognize in a target's `Sources` directory.
63+
64+
### Access a resource in code
65+
66+
If a target includes resources, the compiler creates a resource bundle and an internal static extension on [Bundle](https://developer.apple.com/documentation/Foundation/Bundle) to access it for each module.
67+
Use the extension to locate package resources.
68+
For example, use the following to retrieve the URL to a property list you bundle with your package:
69+
70+
```swift
71+
let settingsURL = Bundle.module.url(forResource: "settings", withExtension: "plist")
72+
```
73+
74+
> Important: Always use `Bundle.module` to access resources.
75+
> A package shouldn’t make assumptions about the exact location of a resource.
76+
77+
If you want to make a package resource available to apps that depend on your Swift package, declare a public constant for it.
78+
For example, use the following to expose a property list file to apps that use your Swift package:
79+
80+
```swift
81+
let settingsURL = Bundle.module.url(forResource: "settings", withExtension: "plist")
82+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Creating a Swift package
2+
3+
Bundle executable or shareable code into a standalone Swift package.
4+
5+
## Overview
6+
7+
A Swift package is a directory that contains sources, dependencies, and has a `Package.swift` manifest file at its root.
8+
Swift packages can provide libraries, executables, tests, plugins, and macros.
9+
The package manifest defines what is in the package, which is itself written in Swift.
10+
The [API reference for PackageDescription](https://developer.apple.com/documentation/packagedescription) defines the types, properties, and functions that go into assembling a package manifest.
11+
12+
### Creating a Library Package
13+
14+
Swift package manager supports creating packages using <doc:PackageInit>.
15+
By default, the package manager creates a package structure focused on providing a library.
16+
For example, you can create a directory and run the command `swift package init` to create a package:
17+
18+
```bash
19+
$ mkdir MyPackage
20+
$ cd MyPackage
21+
$ swift package init
22+
```
23+
24+
The structure provided follows package manager conventions, and provides a fully operational example.
25+
In addition to the package manifest, Swift sources are collected by target name under the `Sources` directory, and tests collected, also by target name, under the `Tests` directory:
26+
27+
```
28+
├── Package.swift
29+
├── Sources
30+
│   └── MyPackage
31+
│   └── MyPackage.swift
32+
└── Tests
33+
└── MyPackageTests
34+
└── MyPackageTests.swift
35+
```
36+
37+
You can immediately use both of <doc:SwiftBuild> and <doc:SwiftTest>:
38+
39+
```bash
40+
$ swift build
41+
$ swift test
42+
```
43+
44+
### Creating an Executable Package
45+
46+
Swift Package Manager can also create a new package with a simplified structure focused on creating executables.
47+
For example, create a directory and run the `init` command with the option `--type executable` to get a package that provides a "Hello World" executable:
48+
49+
```bash
50+
$ mkdir MyExecutable
51+
$ cd MyExecutable
52+
$ swift package init --type executable
53+
$ swift run
54+
Hello, World!
55+
```
56+
57+
There is an additional option for creating a command-line executable based on the `swift-argument-parser`, convenient for parsing command line arguments and structuring commands.
58+
Use `tool` for the `type` option in <doc:PackageInit>.
59+
Like the `executable` template, it is fully operational and also prints "Hello World".
60+
61+
### Creating a Macro Package
62+
63+
Swift Package Manager can generate boilerplate for custom macros:
64+
65+
```bash
66+
$ mkdir MyMacro
67+
$ cd MyMacro
68+
$ swift package init --type macro
69+
$ swift build
70+
$ swift run
71+
The value 42 was produced by the code "a + b"
72+
```
73+
74+
This creates a package with:
75+
76+
- A `.macro` type target with its required dependencies on [swift-syntax](https://github.com/swiftlang/swift-syntax),
77+
- A library `.target` containing the macro's code.
78+
- An `.executableTarget` for running the macro.
79+
- A `.testTarget` for test the macro implementation.
80+
81+
The sample macro, `StringifyMacro`, is documented in the Swift Evolution proposal for [Expression Macros](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0382-expression-macros.md)
82+
and the WWDC [Write Swift macros](https://developer.apple.com/videos/play/wwdc2023/10166) video.
83+
For further documentation, see macros in [The Swift Programming Language](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros/) book.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Editing a remote dependency used in a Swift package
2+
3+
Temporarily switch a remote dependency to local in order to edit the dependency.
4+
5+
## Overview
6+
7+
Swift package manager supports editing dependencies, when your work requires making a change to one of your dependencies (for example, to fix a bug, or add a new API).
8+
The package manager moves the dependency into a location under the `Packages/` directory where it can be edited.
9+
10+
For the packages which are in the editable state, `swift build` uses the exact sources in this directory to build, regardless of their state, Git repository status, tags, or the tag desired by dependency resolution.
11+
In other words, this _just builds_ against the sources that are present.
12+
When an editable package is present, it is used to satisfy all instances of that package in the dependency graph.
13+
It is possible to edit all, some, or none of the packages in a dependency graph, without restriction.
14+
15+
Editable packages are best used to do experimentation with dependency code, or to create and submit a patch in the dependency owner's repository (upstream).
16+
There are two ways to put a package in editable state, using <doc:PackageEdit>.
17+
The first example creates a branch called `bugFix` from the currently resolved version and puts the dependency `PlayingCard` in the `Packages/` directory:
18+
19+
```bash
20+
$ swift package edit PlayingCard --branch bugFix
21+
```
22+
23+
The second is similar, except that the Package Manager leaves the dependency at a detached HEAD at the commit you specified.
24+
25+
```bash
26+
$ swift package edit PlayingCard --revision 969c6a9
27+
```
28+
29+
> Note: If the branch or revision option is not provided, the Package Manager uses the currently resolved version on a detached HEAD.
30+
31+
Once a package is in an editable state, you can navigate to the directory `Packages/PlayingCard` to make changes, build and then push the changes or open a pull request to the upstream repository.
32+
33+
You can end editing a package with <doc:PackageUnedit>.
34+
35+
36+
```bash
37+
$ swift package unedit PlayingCard
38+
```
39+
40+
This removes the edited dependency from `Packages/` and restores the originally resolved version.
41+
42+
This command fails if you have uncommitted changes or changes which are not pushed to the remote repository.
43+
If you want to discard these changes and unedit, use the `--force` option:
44+
45+
```bash
46+
$ swift package unedit PlayingCard --force
47+
```
48+
49+
### Top of Tree Development
50+
51+
This feature allows overriding a dependency with a local checkout on the filesystem.
52+
This checkout is completely unmanaged by the package manager and is used as-is.
53+
The only requirement is that the package name in the overridden checkout shouldn't change.
54+
This is useful when developing multiple packages in tandem, or when working on packages alongside an
55+
application.
56+
57+
The command to attach (or create) a local checkout is:
58+
59+
```bash
60+
$ swift package edit <package name> \
61+
--path <path/to/dependency>
62+
```
63+
64+
For example, if `PlayingCard` depends on `swift-collections` and you have a checkout of `swift-collections` at
65+
`/workspace/swift-collections`:
66+
67+
```bash
68+
$ swift package edit swift-collections \
69+
--path /workspace/swift-collections
70+
```
71+
72+
A checkout of `swift-collections` is created if it doesn't exist at the path you specified.
73+
If a checkout exists, package manager validates the package name at the given path and attaches to it.
74+
75+
The package manager also creates a symlink in the `Packages/` directory to the checkout path.
76+
77+
Use <doc:PackageUnedit> command to stop using the local checkout:
78+
79+
```bash
80+
$ swift package unedit swift-collections
81+
```

Sources/PackageManagerDocs/Documentation.docc/Documentation.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ The Swift Package Manager lets you share your code as a package, depend on and u
2020

2121
### Guides
2222

23+
- <doc:CreatingSwiftPackage>
24+
- <doc:SettingSwiftToolsVersion>
2325
- <doc:AddingDependencies>
2426
- <doc:ResolvingPackageVersions>
2527
- <doc:CreatingCLanguageTargets>
28+
- <doc:UsingBuildConfigurations>
29+
- <doc:SwiftVersionSpecificPackaging>
30+
- <doc:BundlingResources>
31+
- <doc:ReleasingPublishingAPackage>
32+
- <doc:UsingShellCompletion>
2633
- <doc:SwiftPMAsALibrary>
2734
- <doc:ModuleAliasing>
2835

@@ -46,5 +53,3 @@ The Swift Package Manager lets you share your code as a package, depend on and u
4653
- <doc:SwiftPackageRegistryCommands>
4754
- <doc:SwiftPackageCollectionCommands>
4855
- <doc:SwiftRun>
49-
50-
### Design
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Releasing and publishing a Swift package
2+
3+
Share a specific version of your package.
4+
5+
## Overview
6+
7+
Swift Package Manager expects a package to be remotely shared as a single Git repository, with a tag that conforms to a full semantic version, and a `Package.swift` manifest in the root of the repository.
8+
9+
<!-- TODO: need a reference to sharing a dependency through a swift registry -->
10+
11+
To publish a package that is hosted in a Git repository, create and push a semantic version tag.
12+
Swift package manager expects the tag to be a full semantic version, that includes major, minor, and patch versions in the tag.
13+
14+
The following commands illustrate adding a tag `1.0.0` and pushing those tags to the remote repository:
15+
16+
```bash
17+
$ git tag 1.0.0
18+
$ git push origin --tags
19+
```
20+
21+
> Warning: A tag in the form of `1.0` isn't recognized by Swift Package Manager as a complete semantic version.
22+
> Include all three integers reflecting the major, minor, and patch version information.
23+
24+
With the tag in place, other packages can depend on the package you tagged through your source repository.
25+
An example of a published package can be found at [github.com/apple/example-package-playingcard](https://github.com/apple/example-package-playingcard/) with multiple releases available.
26+
27+
To read more about adding a dependency to your package, read <doc:AddingDependencies>.

Sources/PackageManagerDocs/Documentation.docc/ResolvingPackageVersions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Resolving and updating package versions
1+
# Resolving and updating dependencies
22

33
Coordinate and constrain dependencies for your package.
44

@@ -30,3 +30,4 @@ If the `Package.resolved` file does exist, any command that requires dependencie
3030

3131
The `Package.resolved` doesn't constrain upstream dependencies of the package.
3232
For example, if your package presents a library and has `Package.resolved` checked in, those versions are ignored by the package that depends on your library, and the latest eligible versions are chosen.
33+
For more information on constraining dependency versions, see <doc:AddingDependencies>.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Setting the Swift tools version
2+
3+
Define the minimum version of the swift compiler required for your package.
4+
5+
## Overview
6+
7+
The tools version declares the minimum version of the Swift compiler required to
8+
use the package, as well as how parse the `Package.swift` manifest.
9+
When you create a new package using <doc:PackageInit>, the minimum version is set automatically to the current version.
10+
The version is specified on the first line of the manifest with the comment `// swift-tools-version:` and the version for the Swift compiler.
11+
The version is a semantic version, with the exception that a patch version is inferred to be `0` if you don't specify it.
12+
13+
For example, the following line asserts the package requires the Swift compiler version 6.1 or later:
14+
15+
```swift
16+
// swift-tools-version:6.1
17+
```
18+
19+
### Resolving dependencies with different tools versions
20+
21+
When resolving package dependencies, if the tools version of a dependency is greater than the version in use, that version of the dependency is ineligible and dependency resolution continues with evaluating the next-best version.
22+
23+
If no tools version of a dependency, which otherwise meets the package version requirements, supports the version of the Swift tools in use, the package manager presents a dependency resolution error.
24+
For more information on providing package manifests for specific Swift versions, see <doc:SwiftVersionSpecificPackaging>.
25+
26+
### Adjusting the tools version.
27+
28+
Edit the `Package.swift` to adjust the version, or use <doc:PackageToolsVersion> to report or set the the tools version.

0 commit comments

Comments
 (0)