Skip to content

Commit 745ce76

Browse files
authored
update example to use async await APIs (#4263)
motivation: async / await APIs are great, use them as example! changes: * expose some of workspace public APIs as async/await ones (in addition to the callback ones) * update the example to use @main and call the async / await APIs
1 parent af668d0 commit 745ce76

File tree

4 files changed

+90
-49
lines changed

4 files changed

+90
-49
lines changed

Examples/package-info/Package.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
// swift-tools-version:5.1
1+
// swift-tools-version:5.5
22

33
import PackageDescription
44

55
let package = Package(
66
name: "package-info",
77
platforms: [
8-
.macOS(.v10_15),
8+
.macOS(.v12),
99
.iOS(.v13)
1010
],
1111
dependencies: [
1212
// This just points to the SwiftPM at the root of this repository.
13-
.package(path: "../../"),
13+
.package(name: "swift-package-manager", path: "../../"),
1414
// You will want to depend on a stable semantic version instead:
1515
// .package(url: "https://github.com/apple/swift-package-manager", .exact("0.4.0"))
1616
],
1717
targets: [
18-
.target(
18+
.executableTarget(
1919
name: "package-info",
20-
dependencies: ["SwiftPM"]),
20+
dependencies: [
21+
.product(name: "SwiftPM", package: "swift-package-manager")
22+
]
23+
),
2124
]
2225
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Basics
2+
import TSCBasic
3+
import Workspace
4+
5+
@main
6+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
7+
struct Example {
8+
static func main() async throws {
9+
// PREREQUISITES
10+
// ============
11+
12+
// We need a package to work with.
13+
// This computes the path of this package root based on the file location
14+
let packagePath = AbsolutePath(#file).parentDirectory.parentDirectory.parentDirectory
15+
16+
// LOADING
17+
// =======
18+
19+
// There are several levels of information available.
20+
// Each takes longer to load than the level above it, but provides more detail.
21+
22+
let observability = ObservabilitySystem({ print("\($0): \($1)") })
23+
24+
let workspace = try Workspace(forRootPackage: packagePath)
25+
26+
let manifest = try await workspace.loadRootManifest(at: packagePath, observabilityScope: observability.topScope)
27+
28+
let package = try await workspace.loadRootPackage(at: packagePath, observabilityScope: observability.topScope)
29+
30+
let graph = try workspace.loadPackageGraph(rootPath: packagePath, observabilityScope: observability.topScope)
31+
32+
// EXAMPLES
33+
// ========
34+
35+
// Manifest
36+
let products = manifest.products.map({ $0.name }).joined(separator: ", ")
37+
print("Products:", products)
38+
39+
let targets = manifest.targets.map({ $0.name }).joined(separator: ", ")
40+
print("Targets:", targets)
41+
42+
// Package
43+
let executables = package.targets.filter({ $0.type == .executable }).map({ $0.name })
44+
print("Executable targets:", executables)
45+
46+
// PackageGraph
47+
let numberOfFiles = graph.reachableTargets.reduce(0, { $0 + $1.sources.paths.count })
48+
print("Total number of source files (including dependencies):", numberOfFiles)
49+
}
50+
}

Examples/package-info/Sources/package-info/main.swift

Lines changed: 0 additions & 43 deletions
This file was deleted.

Sources/Workspace/Workspace.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,16 @@ extension Workspace {
11551155
)
11561156
}
11571157

1158+
/// Loads and returns manifests at the given paths.
1159+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
1160+
public func loadRootManifests(packages: [AbsolutePath], observabilityScope: ObservabilityScope) async throws -> [AbsolutePath: Manifest] {
1161+
return try await withCheckedThrowingContinuation{ continuation in
1162+
self.loadRootManifests(packages: packages, observabilityScope: observabilityScope) { result in
1163+
continuation.resume(with: result)
1164+
}
1165+
}
1166+
}
1167+
11581168
/// Loads and returns manifests at the given paths.
11591169
public func loadRootManifests(
11601170
packages: [AbsolutePath],
@@ -1196,11 +1206,21 @@ extension Workspace {
11961206
}
11971207
}
11981208

1209+
/// Loads and returns manifest at the given path.
1210+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
1211+
public func loadRootManifest(at path: AbsolutePath, observabilityScope: ObservabilityScope) async throws -> Manifest {
1212+
return try await withCheckedThrowingContinuation{ continuation in
1213+
self.loadRootManifest(at: path, observabilityScope: observabilityScope) { result in
1214+
continuation.resume(with: result)
1215+
}
1216+
}
1217+
}
1218+
11991219
/// Loads and returns manifest at the given path.
12001220
public func loadRootManifest(
12011221
at path: AbsolutePath,
12021222
observabilityScope: ObservabilityScope,
1203-
completion: @escaping(Result<Manifest, Error>) -> Void
1223+
completion: @escaping (Result<Manifest, Error>) -> Void
12041224
) {
12051225
self.loadRootManifests(packages: [path], observabilityScope: observabilityScope) { result in
12061226
completion(result.tryMap{
@@ -1236,6 +1256,17 @@ extension Workspace {
12361256
)
12371257
}
12381258

1259+
/// Loads root package
1260+
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
1261+
public func loadRootPackage(at path: AbsolutePath, observabilityScope: ObservabilityScope) async throws -> Package {
1262+
return try await withCheckedThrowingContinuation{ continuation in
1263+
self.loadRootPackage(at: path, observabilityScope: observabilityScope) { result in
1264+
continuation.resume(with: result)
1265+
}
1266+
}
1267+
}
1268+
1269+
/// Loads root package
12391270
public func loadRootPackage(
12401271
at path: AbsolutePath,
12411272
observabilityScope: ObservabilityScope,

0 commit comments

Comments
 (0)