[Gradle, JS]Process error output and rethrow errors and warns to console

^KT-43869 fixed
This commit is contained in:
Ilya Goncharov
2020-12-10 15:43:41 +03:00
parent 55b0775565
commit 0a3f3bef51
3 changed files with 39 additions and 23 deletions
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.gradle.utils.clearAnsiColor
import java.text.ParseException
class TeamCityMessageCommonClient(
internal val clientType: LogType,
internal val log: Logger
) : ServiceMessageParserCallback {
@@ -43,9 +44,12 @@ class TeamCityMessageCommonClient(
afterMessage = true
}
internal fun testFailedMessage(): String {
return errors
.joinToString("\n")
internal fun testFailedMessage(): String? {
return if (errors.isNotEmpty())
errors
.joinToString("\n")
else
null
}
private fun printMessage(text: String, type: LogType?) {
@@ -74,6 +78,10 @@ class TeamCityMessageCommonClient(
}
override fun regularText(text: String) {
printMessage(text, LogType.DEBUG)
if (clientType == LogType.ERROR || clientType == LogType.WARN) {
printMessage(text, clientType)
} else {
printMessage(text, LogType.DEBUG)
}
}
}
@@ -71,19 +71,21 @@ internal fun Project.execWithProgress(description: String, readStdErr: Boolean =
internal fun Project.execWithErrorLogger(
description: String,
body: (ExecAction, ProgressLogger) -> TeamCityMessageCommonClient
body: (ExecAction, ProgressLogger) -> Pair<TeamCityMessageCommonClient, TeamCityMessageCommonClient>
): ExecResult {
this as ProjectInternal
val exec = services.get(ExecActionFactory::class.java).newExecAction()
return project!!.operation(description) {
progress(description)
val client = body(exec, this)
val (standardClient, errorClient) = body(exec, this)
exec.isIgnoreExitValue = true
val result = exec.execute()
if (result.exitValue != 0) {
error(
client.testFailedMessage()
errorClient.testFailedMessage()
?: standardClient.testFailedMessage()
?: "Error occurred. See log for details."
)
}
result
@@ -9,6 +9,7 @@ import org.gradle.internal.logging.progress.ProgressLogger
import org.gradle.process.ExecSpec
import org.gradle.process.internal.ExecHandle
import org.gradle.process.internal.ExecHandleFactory
import org.jetbrains.kotlin.gradle.internal.LogType
import org.jetbrains.kotlin.gradle.internal.TeamCityMessageCommonClient
import org.jetbrains.kotlin.gradle.internal.execWithErrorLogger
import org.jetbrains.kotlin.gradle.internal.testing.TCServiceMessageOutputStreamHandler
@@ -25,29 +26,29 @@ internal data class KotlinWebpackRunner(
val config: KotlinWebpackConfig
) {
fun execute() = npmProject.project.execWithErrorLogger("webpack") { execAction, progressLogger ->
val client = configureClient(progressLogger)
client.apply {
configureExec(
execAction,
client
)
}
configureExec(
execAction,
progressLogger
)
}
fun start(): ExecHandle {
val execFactory = execHandleFactory.newExec()
configureExec(
execFactory,
configureClient(null)
null
)
val exec = execFactory.build()
exec.start()
return exec
}
private fun configureClient(progressLogger: ProgressLogger?): TeamCityMessageCommonClient {
private fun configureClient(
clientType: LogType,
progressLogger: ProgressLogger?
): TeamCityMessageCommonClient {
val logger = npmProject.project.logger
return TeamCityMessageCommonClient(logger)
return TeamCityMessageCommonClient(clientType, logger)
.apply {
if (progressLogger != null) {
this.progressLogger = progressLogger
@@ -57,21 +58,24 @@ internal data class KotlinWebpackRunner(
private fun configureExec(
execFactory: ExecSpec,
client: TeamCityMessageCommonClient
) {
progressLogger: ProgressLogger?
): Pair<TeamCityMessageCommonClient, TeamCityMessageCommonClient> {
check(config.entry?.isFile == true) {
"${this}: Entry file not existed \"${config.entry}\""
}
val standardClient = configureClient(LogType.LOG, progressLogger)
execFactory.standardOutput = TCServiceMessageOutputStreamHandler(
client = client,
client = standardClient,
onException = { },
logger = client.log
logger = standardClient.log
)
val errorClient = configureClient(LogType.ERROR, progressLogger)
execFactory.errorOutput = TCServiceMessageOutputStreamHandler(
client = client,
client = errorClient,
onException = { },
logger = client.log
logger = errorClient.log
)
config.save(configFile)
@@ -89,5 +93,7 @@ internal data class KotlinWebpackRunner(
nodeArgs,
args
)
return standardClient to errorClient
}
}