blob: 06b8b1e3cdc3bed0ebaafa257aae8f0fb8eda306 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
/*
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
package org.jetbrains.dokka.it
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlin.concurrent.thread
public class ProcessResult(
public val exitCode: Int,
public val output: String
)
public fun Process.awaitProcessResult(): ProcessResult = runBlocking {
val exitCode = async { awaitExitCode() }
val output = async { awaitOutput() }
ProcessResult(
exitCode.await(),
output.await()
)
}
private suspend fun Process.awaitExitCode(): Int {
val deferred = CompletableDeferred<Int>()
thread {
try {
deferred.complete(this.waitFor())
} catch (e: Throwable) {
deferred.completeExceptionally(e)
}
}
return deferred.await()
}
private suspend fun Process.awaitOutput(): String {
val deferred = CompletableDeferred<String>()
thread {
try {
var string = ""
this.inputStream.bufferedReader().forEachLine { line ->
println(line)
string += line + System.lineSeparator()
}
deferred.complete(string)
} catch (e: Throwable) {
deferred.completeExceptionally(e)
}
}
return deferred.await()
}
|