Skip to content

Commit 211986e

Browse files
committed
Use resolve() instead of loading the package graph where it makes sense
1 parent be4e429 commit 211986e

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,14 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
8282

8383
case .fetch:
8484
diagnostics.emit(data: FetchDeprecatedDiagnostic())
85-
let workspace = try getActiveWorkspace()
86-
workspace.resolve(root: try getWorkspaceRoot(), diagnostics: diagnostics)
85+
try resolve()
8786

8887
case .resolve:
8988
let resolveOptions = options.resolveOptions
90-
let workspace = try getActiveWorkspace()
91-
let root = try getWorkspaceRoot()
9289

9390
// If a package is provided, use that to resolve the dependencies.
9491
if let packageName = resolveOptions.packageName {
92+
let workspace = try getActiveWorkspace()
9593
return try workspace.resolve(
9694
packageName: packageName,
9795
root: getWorkspaceRoot(),
@@ -102,18 +100,16 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
102100
}
103101

104102
// Otherwise, run a normal resolve.
105-
workspace.resolve(root: root, diagnostics: diagnostics)
103+
try resolve()
106104

107105
case .edit:
108106
let packageName = options.editOptions.packageName!
109-
// Load the package graph.
110-
try loadPackageGraph()
111-
112-
// Get the current workspace.
107+
try resolve()
113108
let workspace = try getActiveWorkspace()
114109

115110
// Create revision object if provided by user.
116111
let revision = options.editOptions.revision.flatMap({ Revision(identifier: $0) })
112+
117113
// Put the dependency in edit mode.
118114
workspace.edit(
119115
packageName: packageName,
@@ -124,9 +120,7 @@ public class SwiftPackageTool: SwiftTool<PackageToolOptions> {
124120

125121
case .unedit:
126122
let packageName = options.editOptions.packageName!
127-
128-
// Load the package graph.
129-
try loadPackageGraph()
123+
try resolve()
130124
let workspace = try getActiveWorkspace()
131125

132126
try workspace.unedit(packageName: packageName, forceRemove: options.editOptions.shouldForceRemove)

Sources/Commands/SwiftTool.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,18 @@ public class SwiftTool<Options: ToolOptions> {
279279
fatalError("Must be implmented by subclasses")
280280
}
281281

282+
/// Resolve the dependencies.
283+
func resolve() throws {
284+
let workspace = try getActiveWorkspace()
285+
workspace.resolve(root: try getWorkspaceRoot(), diagnostics: diagnostics)
286+
287+
// Throw if there were errors when loading the graph.
288+
// The actual errors will be printed before exiting.
289+
guard !diagnostics.hasErrors else {
290+
throw Error.hasFatalDiagnostics
291+
}
292+
}
293+
282294
/// Fetch and load the complete package graph.
283295
@discardableResult
284296
func loadPackageGraph() throws -> PackageGraph {

0 commit comments

Comments
 (0)