Skip to content

Commit 0e722c3

Browse files
committed
Add delegate callbacks for manifest loading.
1 parent 1703e07 commit 0e722c3

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Sources/SPMTestSupport/TestWorkspace.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,4 +750,12 @@ public final class TestWorkspaceDelegate: WorkspaceDelegate {
750750
public func willResolveDependencies(reason: WorkspaceResolveReason) {
751751
events.append("will resolve dependencies")
752752
}
753+
754+
public func willLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind) {
755+
events.append("will load manifest for \(packageKind) package: \(url)")
756+
}
757+
758+
public func didLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Diagnostic]) {
759+
events.append("did load manifest for \(packageKind) package: \(url)")
760+
}
753761
}

Sources/Workspace/Workspace.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -38,6 +38,12 @@ public enum WorkspaceResolveReason: Equatable {
3838
/// The delegate interface used by the workspace to report status information.
3939
public protocol WorkspaceDelegate: class {
4040

41+
/// The workspace is about to load a package manifest (which might be in the cache, or might need to be parsed). Note that this does not include speculative loading of manifests that may occr during dependency resolution; rather, it includes only the final manifest loading that happens after a particular package version has been checked out into a working directory.
42+
func willLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind)
43+
44+
/// The workspace has loaded a package manifest, either successfully or not. The manifest is nil if an error occurs, in which case there will also be at least one error in the list of diagnostics (there may be warnings even if a manifest is loaded successfully).
45+
func didLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Diagnostic])
46+
4147
/// The workspace has started fetching this repository.
4248
func fetchingWillBegin(repository: String)
4349

@@ -78,6 +84,8 @@ public protocol WorkspaceDelegate: class {
7884
}
7985

8086
public extension WorkspaceDelegate {
87+
func willLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind) {}
88+
func didLoadManifest(packagePath: AbsolutePath, url: String, version: Version?, packageKind: PackageReference.Kind, manifest: Manifest?, diagnostics: [Diagnostic]) {}
8189
func checkingOut(repository: String, atReference: String, to path: AbsolutePath) {}
8290
func repositoryWillUpdate(_ repository: String) {}
8391
func repositoryDidUpdate(_ repository: String) {}
@@ -1255,7 +1263,10 @@ extension Workspace {
12551263
packageKind: PackageReference.Kind,
12561264
diagnostics: DiagnosticsEngine
12571265
) -> Manifest? {
1258-
return diagnostics.with(location: PackageLocation.Local(packagePath: packagePath)) { diagnostics in
1266+
// Load the manifest, bracketed by the calls to the delegate callbacks. The delegate callback is only passed any diagnostics emited during the parsing of the manifest, but they are also forwarded up to the caller.
1267+
delegate?.willLoadManifest(packagePath: packagePath, url: url, version: version, packageKind: packageKind)
1268+
let manifestDiagnostics = DiagnosticsEngine(handlers: [{diagnostics.emit($0)}])
1269+
let manifest: Manifest? = diagnostics.with(location: PackageLocation.Local(packagePath: packagePath)) { diagnostics in
12591270
return diagnostics.wrap {
12601271
// Load the tools version for the package.
12611272
let toolsVersion = try toolsVersionLoader.load(
@@ -1277,6 +1288,8 @@ extension Workspace {
12771288
)
12781289
}
12791290
}
1291+
delegate?.didLoadManifest(packagePath: packagePath, url: url, version: version, packageKind: packageKind, manifest: manifest, diagnostics: manifestDiagnostics.diagnostics)
1292+
return manifest
12801293
}
12811294

12821295
fileprivate func updateBinaryArtifacts(

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
@@ -90,6 +90,18 @@ final class WorkspaceTests: XCTestCase {
9090
result.check(dependency: "quix", at: .checkout(.version("1.2.0")))
9191
}
9292

93+
// Check the load-package callbacks.
94+
XCTAssertMatch(workspace.delegate.events, [
95+
.equal("will load manifest for root package: /tmp/ws/roots/Foo"),
96+
.equal("did load manifest for root package: /tmp/ws/roots/Foo"),
97+
])
98+
XCTAssertMatch(workspace.delegate.events, [
99+
.equal("will load manifest for remote package: /tmp/ws/pkgs/Quix"),
100+
.equal("did load manifest for remote package: /tmp/ws/pkgs/Quix"),
101+
.equal("will load manifest for remote package: /tmp/ws/pkgs/Baz"),
102+
.equal("did load manifest for remote package: /tmp/ws/pkgs/Baz")
103+
])
104+
93105
// Close and reopen workspace.
94106
workspace.closeWorkspace()
95107
workspace.checkManagedDependencies() { result in
@@ -2720,7 +2732,7 @@ final class WorkspaceTests: XCTestCase {
27202732
// Check we don't have updating Foo event.
27212733
workspace.checkUpdate(roots: ["Root"]) { diagnostics in
27222734
XCTAssertNoDiagnostics(diagnostics)
2723-
XCTAssertEqual(workspace.delegate.events, ["Everything is already up-to-date"])
2735+
XCTAssertMatch(workspace.delegate.events, ["Everything is already up-to-date"])
27242736
}
27252737
}
27262738

0 commit comments

Comments
 (0)