Skip to content

Commit 15a34cf

Browse files
committed
refactor how plugins call the build system when using the CLI
motivation: consolidate use of build operation construction which is important for cancellation support changes: * use the general purpose function to create a build operation * small refactoring of how build plan is extracted after the build is complete
1 parent 05c4d01 commit 15a34cf

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

Sources/Commands/SwiftPackageTool.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,32 +1170,20 @@ final class PluginDelegate: PluginInvocationDelegate {
11701170

11711171
// Create a build operation. We have to disable the cache in order to get a build plan created.
11721172
let outputStream = BufferedOutputByteStream()
1173-
let buildOperation = BuildOperation(
1174-
buildParameters: buildParameters,
1173+
let buildOperation = try self.swiftTool.createBuildOperation(
1174+
explicitProduct: explicitProduct,
11751175
cacheBuildManifest: false,
1176-
packageGraphLoader: { try self.swiftTool.loadPackageGraph(explicitProduct: explicitProduct) },
1177-
pluginScriptRunner: try self.swiftTool.getPluginScriptRunner(),
1178-
pluginWorkDirectory: try self.swiftTool.getActiveWorkspace().location.pluginWorkingDirectory,
1179-
outputStream: outputStream,
1180-
logLevel: logLevel,
1181-
fileSystem: swiftTool.fileSystem,
1182-
observabilityScope: self.swiftTool.observabilityScope
1176+
customBuildParameters: buildParameters,
1177+
customOutputStream: outputStream,
1178+
customLogLevel: logLevel
11831179
)
11841180

1185-
// Save the instance so it can be canceled from the interrupt handler.
1186-
self.swiftTool.buildSystemRef.buildSystem = buildOperation
1187-
1188-
// Get or create the build description and plan the build.
1189-
let _ = try buildOperation.getBuildDescription()
1190-
let buildPlan = buildOperation.buildPlan!
1191-
11921181
// Run the build. This doesn't return until the build is complete.
1193-
var success = true
1194-
do {
1195-
try buildOperation.build(subset: buildSubset)
1196-
}
1197-
catch {
1198-
success = false
1182+
let success = buildOperation.buildIgnoringError(subset: buildSubset)
1183+
1184+
// Get the build plan used
1185+
guard let buildPlan = buildOperation.buildPlan else {
1186+
throw InternalError("invalid state, buildPlan is undefined")
11991187
}
12001188

12011189
// Create and return the build result record based on what the delegate collected and what's in the build plan.
@@ -1942,3 +1930,14 @@ private extension Basics.Diagnostic {
19421930
.error("missing required argument \(argument)")
19431931
}
19441932
}
1933+
1934+
extension BuildOperation {
1935+
fileprivate func buildIgnoringError(subset: BuildSubset) -> Bool {
1936+
do {
1937+
try self.build(subset: subset)
1938+
return true
1939+
} catch {
1940+
return false
1941+
}
1942+
}
1943+
}

Sources/Commands/SwiftTool.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ public class SwiftTool {
735735
customBuildParameters: BuildParameters? = .none,
736736
customPackageGraphLoader: (() throws -> PackageGraph)? = .none,
737737
customOutputStream: OutputByteStream? = .none,
738+
customLogLevel: Diagnostic.Severity? = .none,
738739
customObservabilityScope: ObservabilityScope? = .none
739740
) throws -> BuildOperation {
740741
let graphLoader = { try self.loadPackageGraph(explicitProduct: explicitProduct) }
@@ -748,7 +749,7 @@ public class SwiftTool {
748749
pluginScriptRunner: self.getPluginScriptRunner(),
749750
pluginWorkDirectory: try self.getActiveWorkspace().location.pluginWorkingDirectory,
750751
outputStream: customOutputStream ?? self.outputStream,
751-
logLevel: self.logLevel,
752+
logLevel: customLogLevel ?? self.logLevel,
752753
fileSystem: self.fileSystem,
753754
observabilityScope: customObservabilityScope ?? self.observabilityScope
754755
)

0 commit comments

Comments
 (0)