Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PluginAPI: Optionally animate build progress in console #8383

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ struct diagnostics_stub: CommandPlugin {
// SwiftPM does not add a prefix to these logs.
let result = try packageManager.build(
.product("placeholder"),
parameters: .init(echoLogs: arguments.contains("echologs"))
parameters: .init(
echoLogs: arguments.contains("echologs"),
progressToConsole: arguments.contains("progresstoconsole")
)
)

// To verify that logs are also returned correctly to the plugin,
Expand Down
9 changes: 8 additions & 1 deletion Sources/Build/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ package struct LLBuildSystemConfiguration {

let logLevel: Basics.Diagnostic.Severity
let outputStream: OutputByteStream
let progressOutputStream: OutputByteStream

let observabilityScope: ObservabilityScope

Expand All @@ -66,6 +67,7 @@ package struct LLBuildSystemConfiguration {
fileSystem: any Basics.FileSystem,
logLevel: Basics.Diagnostic.Severity,
outputStream: OutputByteStream,
progressOutputStream: OutputByteStream,
observabilityScope: ObservabilityScope
) {
self.toolsBuildParameters = toolsBuildParameters
Expand All @@ -78,6 +80,7 @@ package struct LLBuildSystemConfiguration {
self.fileSystem = fileSystem
self.logLevel = logLevel
self.outputStream = outputStream
self.progressOutputStream = progressOutputStream
self.observabilityScope = observabilityScope
}

Expand Down Expand Up @@ -208,6 +211,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
additionalFileRules: [FileRuleDescription],
pkgConfigDirectories: [AbsolutePath],
outputStream: OutputByteStream,
progressOutputStream: OutputByteStream? = nil,
logLevel: Basics.Diagnostic.Severity,
fileSystem: Basics.FileSystem,
observabilityScope: ObservabilityScope
Expand All @@ -223,6 +227,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
additionalFileRules: additionalFileRules,
pkgConfigDirectories: pkgConfigDirectories,
outputStream: outputStream,
progressOutputStream: progressOutputStream ?? outputStream,
logLevel: logLevel,
fileSystem: fileSystem,
observabilityScope: observabilityScope
Expand All @@ -240,6 +245,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
additionalFileRules: [FileRuleDescription],
pkgConfigDirectories: [AbsolutePath],
outputStream: OutputByteStream,
progressOutputStream: OutputByteStream? = nil,
logLevel: Basics.Diagnostic.Severity,
fileSystem: Basics.FileSystem,
observabilityScope: ObservabilityScope
Expand All @@ -259,6 +265,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
fileSystem: fileSystem,
logLevel: logLevel,
outputStream: outputStream,
progressOutputStream: progressOutputStream ?? outputStream,
observabilityScope: observabilityScope.makeChildScope(description: "Build Operation")
)

Expand Down Expand Up @@ -795,7 +802,7 @@ public final class BuildOperation: PackageStructureDelegate, SPMBuildCore.BuildS
) throws -> (buildSystem: SPMLLBuild.BuildSystem, tracker: LLBuildProgressTracker) {
// Figure out which progress bar we have to use during the build.
let progressAnimation = ProgressAnimation.ninja(
stream: config.outputStream,
stream: config.progressOutputStream,
verbose: config.logLevel.isVerbose
)
let buildExecutionContext = BuildExecutionContext(
Expand Down
2 changes: 2 additions & 0 deletions Sources/Commands/Utilities/PluginDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ final class PluginDelegate: PluginInvocationDelegate {
if parameters.echoLogs {
outputStream.addStream(swiftCommandState.outputStream)
}
let progressOutputStream = parameters.progressToConsole ? swiftCommandState.outputStream : outputStream

let buildSystem = try await swiftCommandState.createBuildSystem(
explicitBuildSystem: .native,
Expand All @@ -173,6 +174,7 @@ final class PluginDelegate: PluginInvocationDelegate {
cacheBuildManifest: false,
productsBuildParameters: buildParameters,
outputStream: outputStream,
progressOutputStream: progressOutputStream,
logLevel: logLevel
)

Expand Down
4 changes: 4 additions & 0 deletions Sources/CoreCommands/BuildSystemSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private struct NativeBuildSystemFactory: BuildSystemFactory {
toolsBuildParameters: BuildParameters?,
packageGraphLoader: (() async throws -> ModulesGraph)?,
outputStream: OutputByteStream?,
progressOutputStream: OutputByteStream?,
logLevel: Diagnostic.Severity?,
observabilityScope: ObservabilityScope?
) async throws -> any BuildSystem {
Expand Down Expand Up @@ -64,6 +65,7 @@ private struct NativeBuildSystemFactory: BuildSystemFactory {
additionalFileRules: FileRuleDescription.swiftpmFileTypes,
pkgConfigDirectories: self.swiftCommandState.options.locations.pkgConfigDirectories,
outputStream: outputStream ?? self.swiftCommandState.outputStream,
progressOutputStream: progressOutputStream ?? self.swiftCommandState.outputStream,
logLevel: logLevel ?? self.swiftCommandState.logLevel,
fileSystem: self.swiftCommandState.fileSystem,
observabilityScope: observabilityScope ?? self.swiftCommandState.observabilityScope)
Expand All @@ -81,6 +83,7 @@ private struct XcodeBuildSystemFactory: BuildSystemFactory {
toolsBuildParameters: BuildParameters?,
packageGraphLoader: (() async throws -> ModulesGraph)?,
outputStream: OutputByteStream?,
progressOutputStream: OutputByteStream?,
logLevel: Diagnostic.Severity?,
observabilityScope: ObservabilityScope?
) throws -> any BuildSystem {
Expand Down Expand Up @@ -111,6 +114,7 @@ private struct SwiftBuildSystemFactory: BuildSystemFactory {
toolsBuildParameters: BuildParameters?,
packageGraphLoader: (() async throws -> ModulesGraph)?,
outputStream: OutputByteStream?,
progressOutputStream: OutputByteStream?,
logLevel: Diagnostic.Severity?,
observabilityScope: ObservabilityScope?
) throws -> any BuildSystem {
Expand Down
2 changes: 2 additions & 0 deletions Sources/CoreCommands/SwiftCommandState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ public final class SwiftCommandState {
toolsBuildParameters: BuildParameters? = .none,
packageGraphLoader: (() async throws -> ModulesGraph)? = .none,
outputStream: OutputByteStream? = .none,
progressOutputStream: OutputByteStream? = .none,
logLevel: Basics.Diagnostic.Severity? = nil,
observabilityScope: ObservabilityScope? = .none
) async throws -> BuildSystem {
Expand All @@ -773,6 +774,7 @@ public final class SwiftCommandState {
toolsBuildParameters: toolsBuildParameters,
packageGraphLoader: packageGraphLoader,
outputStream: outputStream,
progressOutputStream: progressOutputStream,
logLevel: logLevel ?? self.logLevel,
observabilityScope: observabilityScope
)
Expand Down
11 changes: 10 additions & 1 deletion Sources/PackagePlugin/PackageManagerProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public struct PackageManager {
/// Whether to print build logs to the console
public var echoLogs: Bool

/// Whether to print build progress to the console
///
/// If this is set to true, `BuildResult.logText` will not contain any
/// progress information.
public var progressToConsole: Bool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new public API and will therefore need a Swift Evolution proposal API availability annotation.

Can you please start a pitch on the Swift Forums to propose this new API?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


/// Additional flags to pass to all C compiler invocations.
public var otherCFlags: [String] = []

Expand All @@ -77,11 +83,13 @@ public struct PackageManager {
public init(
configuration: BuildConfiguration = .debug,
logging: BuildLogVerbosity = .concise,
echoLogs: Bool = false
echoLogs: Bool = false,
progressToConsole: Bool = false
) {
self.configuration = configuration
self.logging = logging
self.echoLogs = echoLogs
self.progressToConsole = progressToConsole
}
}

Expand Down Expand Up @@ -331,6 +339,7 @@ extension PluginToHostMessage.BuildParameters {
self.configuration = .init(parameters.configuration)
self.logging = .init(parameters.logging)
self.echoLogs = parameters.echoLogs
self.progressToConsole = parameters.progressToConsole
self.otherCFlags = parameters.otherCFlags
self.otherCxxFlags = parameters.otherCxxFlags
self.otherSwiftcFlags = parameters.otherSwiftcFlags
Expand Down
1 change: 1 addition & 0 deletions Sources/PackagePlugin/PluginMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ enum PluginToHostMessage: Codable {
case concise, verbose, debug
}
var echoLogs: Bool
var progressToConsole: Bool
var otherCFlags: [String]
var otherCxxFlags: [String]
var otherSwiftcFlags: [String]
Expand Down
3 changes: 3 additions & 0 deletions Sources/SPMBuildCore/BuildSystem/BuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public protocol BuildSystemFactory {
toolsBuildParameters: BuildParameters?,
packageGraphLoader: (() async throws -> ModulesGraph)?,
outputStream: OutputByteStream?,
progressOutputStream: OutputByteStream?,
logLevel: Diagnostic.Severity?,
observabilityScope: ObservabilityScope?
) async throws -> any BuildSystem
Expand Down Expand Up @@ -149,6 +150,7 @@ public struct BuildSystemProvider {
toolsBuildParameters: BuildParameters? = .none,
packageGraphLoader: (() async throws -> ModulesGraph)? = .none,
outputStream: OutputByteStream? = .none,
progressOutputStream: OutputByteStream? = .none,
logLevel: Diagnostic.Severity? = .none,
observabilityScope: ObservabilityScope? = .none
) async throws -> any BuildSystem {
Expand All @@ -163,6 +165,7 @@ public struct BuildSystemProvider {
toolsBuildParameters: toolsBuildParameters,
packageGraphLoader: packageGraphLoader,
outputStream: outputStream,
progressOutputStream: progressOutputStream,
logLevel: logLevel,
observabilityScope: observabilityScope
)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SPMBuildCore/Plugins/PluginInvocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ public struct PluginInvocationBuildParameters {
case concise, verbose, debug
}
public var echoLogs: Bool
public var progressToConsole: Bool
public var otherCFlags: [String]
public var otherCxxFlags: [String]
public var otherSwiftcFlags: [String]
Expand Down Expand Up @@ -1078,6 +1079,7 @@ fileprivate extension PluginInvocationBuildParameters {
self.configuration = .init(parameters.configuration)
self.logging = .init(parameters.logging)
self.echoLogs = parameters.echoLogs
self.progressToConsole = parameters.progressToConsole
self.otherCFlags = parameters.otherCFlags
self.otherCxxFlags = parameters.otherCxxFlags
self.otherSwiftcFlags = parameters.otherSwiftcFlags
Expand Down
11 changes: 11 additions & 0 deletions Tests/CommandsTests/PackageCommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2648,6 +2648,8 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
// Echoed logs have no prefix
let containsLogecho = StringPattern.contains("Building for debugging...\n")

let containsProgressLog = StringPattern.contains("Compiling placeholder")

// These tests involve building a target, so each test must run with a fresh copy of the fixture
// otherwise the logs may be different in subsequent tests.

Expand Down Expand Up @@ -2682,7 +2684,16 @@ class PackageCommandTestCase: CommandsBuildProviderTestCase {
try await fixture(name: "Miscellaneous/Plugins/CommandPluginTestStub") { fixturePath in
let (stdout, stderr) = try await self.execute(["print-diagnostics", "build", "printlogs", "echologs"], packagePath: fixturePath, env: ["SWIFT_DRIVER_SWIFTSCAN_LIB" : "/this/is/a/bad/path"])
XCTAssertMatch(stdout, containsLogtext)
XCTAssertMatch(stdout, containsProgressLog)
XCTAssertMatch(stderr, containsLogecho)
XCTAssertMatch(stderr, containsProgressLog)
}

try await fixture(name: "Miscellaneous/Plugins/CommandPluginTestStub") { fixturePath in
let (stdout, stderr) = try await self.execute(["print-diagnostics", "build", "printlogs", "progresstoconsole"], packagePath: fixturePath, env: ["SWIFT_DRIVER_SWIFTSCAN_LIB" : "/this/is/a/bad/path"])
XCTAssertMatch(stdout, containsLogtext)
XCTAssertNoMatch(stdout, containsProgressLog)
XCTAssertMatch(stderr, containsProgressLog)
}
}

Expand Down