-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add color to build output && push originId into server responses
- Loading branch information
1 parent
0107ce5
commit 0755b37
Showing
14 changed files
with
228 additions
and
203 deletions.
There are no files selected for viewing
33 changes: 17 additions & 16 deletions
33
bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner/BazelProcess.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner | ||
|
||
import java.time.Duration | ||
import org.apache.logging.log4j.LogManager | ||
import org.jetbrains.bsp.bazel.bazelrunner.outputs.AsyncOutputProcessor | ||
import org.jetbrains.bsp.bazel.commons.Format | ||
import org.jetbrains.bsp.bazel.commons.Stopwatch | ||
import org.jetbrains.bsp.bazel.logger.BspClientLogger | ||
import java.time.Duration | ||
|
||
class BazelProcess internal constructor( | ||
private val process: Process, | ||
private val logger: BspClientLogger | ||
private val logger: BspClientLogger, | ||
private val originId: String? | ||
) { | ||
|
||
fun waitAndGetResult(): BazelProcessResult { | ||
val outputProcessor = AsyncOutputProcessor(process, logger::message, LOGGER::info) | ||
val stopwatch = Stopwatch.start() | ||
fun waitAndGetResult(): BazelProcessResult { | ||
val outputProcessor = AsyncOutputProcessor(process, { logger.message(it, originId) }, LOGGER::info) | ||
val stopwatch = Stopwatch.start() | ||
|
||
val exitCode = outputProcessor.waitForExit() | ||
val duration = stopwatch.stop() | ||
logCompletion(exitCode, duration) | ||
return BazelProcessResult(outputProcessor.stdoutCollector, outputProcessor.stderrCollector, exitCode) | ||
} | ||
val exitCode = outputProcessor.waitForExit() | ||
val duration = stopwatch.stop() | ||
logCompletion(exitCode, duration) | ||
return BazelProcessResult(outputProcessor.stdoutCollector, outputProcessor.stderrCollector, exitCode) | ||
} | ||
|
||
private fun logCompletion(exitCode: Int, duration: Duration) { | ||
logger.message("Command completed in %s (exit code %d)", Format.duration(duration), exitCode) | ||
} | ||
private fun logCompletion(exitCode: Int, duration: Duration) { | ||
logger.message("Command completed in %s (exit code %d)".format(Format.duration(duration), exitCode), originId) | ||
} | ||
|
||
companion object { | ||
private val LOGGER = LogManager.getLogger(BazelProcess::class.java) | ||
} | ||
companion object { | ||
private val LOGGER = LogManager.getLogger(BazelProcess::class.java) | ||
} | ||
} |
82 changes: 46 additions & 36 deletions
82
bazelrunner/src/main/java/org/jetbrains/bsp/bazel/bazelrunner/BazelRunner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,71 @@ | ||
package org.jetbrains.bsp.bazel.bazelrunner | ||
|
||
import java.nio.file.Path | ||
import org.apache.logging.log4j.LogManager | ||
import org.jetbrains.bsp.bazel.logger.BspClientLogger | ||
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContext | ||
import org.jetbrains.bsp.bazel.workspacecontext.WorkspaceContextProvider | ||
import java.nio.file.Path | ||
|
||
class BazelRunner private constructor( | ||
private val workspaceContextProvider: WorkspaceContextProvider, | ||
private val bspClientLogger: BspClientLogger, | ||
val workspaceRoot: Path?, | ||
) { | ||
|
||
companion object { | ||
private val LOGGER = LogManager.getLogger(BazelRunner::class.java) | ||
companion object { | ||
private val LOGGER = LogManager.getLogger(BazelRunner::class.java) | ||
|
||
@JvmStatic | ||
fun of( | ||
workspaceContextProvider: WorkspaceContextProvider, | ||
bspClientLogger: BspClientLogger, | ||
workspaceRoot: Path?, | ||
): BazelRunner { | ||
return BazelRunner(workspaceContextProvider, bspClientLogger, workspaceRoot) | ||
@JvmStatic | ||
fun of( | ||
workspaceContextProvider: WorkspaceContextProvider, | ||
bspClientLogger: BspClientLogger, | ||
workspaceRoot: Path?, | ||
): BazelRunner { | ||
return BazelRunner(workspaceContextProvider, bspClientLogger, workspaceRoot) | ||
} | ||
} | ||
} | ||
|
||
private var besBackendPort: Int? = null | ||
private var besBackendPort: Int? = null | ||
|
||
fun commandBuilder(): BazelRunnerCommandBuilder = BazelRunnerCommandBuilder(this) | ||
fun commandBuilder(): BazelRunnerCommandBuilder = BazelRunnerCommandBuilder(this) | ||
|
||
fun runBazelCommandBes(command: String, flags: List<String>, arguments: List<String>): BazelProcess { | ||
fun besFlags() = listOf("--bes_backend=grpc://localhost:${besBackendPort!!}") | ||
fun runBazelCommandBes( | ||
command: String, | ||
flags: List<String>, | ||
arguments: List<String>, | ||
originId: String? | ||
): BazelProcess { | ||
fun besFlags() = listOf("--bes_backend=grpc://localhost:${besBackendPort!!}") | ||
|
||
return runBazelCommand(command, flags = besFlags() + flags, arguments) | ||
} | ||
return runBazelCommand(command, flags = besFlags() + flags, arguments, originId) | ||
} | ||
|
||
fun runBazelCommand(command: String, flags: List<String>, arguments: List<String>): BazelProcess { | ||
val workspaceContext = workspaceContextProvider.currentWorkspaceContext() | ||
fun runBazelCommand( | ||
command: String, | ||
flags: List<String>, | ||
arguments: List<String>, | ||
originId: String? | ||
): BazelProcess { | ||
val workspaceContext = workspaceContextProvider.currentWorkspaceContext() | ||
|
||
val processArgs = listOf(bazel(workspaceContext), command) + buildFlags(workspaceContext) + flags + arguments | ||
logInvocation(processArgs) | ||
val processBuilder = ProcessBuilder(processArgs) | ||
workspaceRoot?.let { processBuilder.directory(it.toFile()) } | ||
val process = processBuilder.start() | ||
return BazelProcess(process, bspClientLogger) | ||
} | ||
val processArgs = listOf(bazel(workspaceContext), command) + buildFlags(workspaceContext) + flags + arguments | ||
logInvocation(processArgs, originId) | ||
val processBuilder = ProcessBuilder(processArgs) | ||
workspaceRoot?.let { processBuilder.directory(it.toFile()) } | ||
val process = processBuilder.start() | ||
return BazelProcess(process, bspClientLogger, originId) | ||
} | ||
|
||
private fun logInvocation(processArgs: List<String>) { | ||
"Invoking: ${processArgs.joinToString(" ")}" | ||
.also { LOGGER.info(it) } | ||
.also { bspClientLogger.message(it) } | ||
} | ||
private fun logInvocation(processArgs: List<String>, originId: String?) { | ||
"Invoking: ${processArgs.joinToString(" ")}" | ||
.also { LOGGER.info(it) } | ||
.also { bspClientLogger.message(it, originId) } | ||
} | ||
|
||
private fun bazel(workspaceContext: WorkspaceContext): String = workspaceContext.bazelPath.value.toString() | ||
private fun buildFlags(workspaceContext: WorkspaceContext): List<String> = workspaceContext.buildFlags.values | ||
private fun bazel(workspaceContext: WorkspaceContext): String = workspaceContext.bazelPath.value.toString() | ||
private fun buildFlags(workspaceContext: WorkspaceContext): List<String> = workspaceContext.buildFlags.values | ||
|
||
fun setBesBackendPort(port: Int) { | ||
besBackendPort = port | ||
} | ||
fun setBesBackendPort(port: Int) { | ||
besBackendPort = port | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.