mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better stream management in RunCommand.
This commit is contained in:
parent
de6fcd2c45
commit
cd71653010
1 changed files with 10 additions and 3 deletions
|
@ -26,7 +26,8 @@ open class RunCommand(val command: String) {
|
|||
|
||||
val pb = ProcessBuilder(allArgs)
|
||||
pb.directory(directory)
|
||||
log(2, "Running command: " + allArgs.joinToString(" ") + "\n Current directory: $directory")
|
||||
log(2, "Running command: " + allArgs.joinToString(" ") +
|
||||
"\n Current directory: ${directory.absolutePath}")
|
||||
val process = pb.start()
|
||||
pb.environment().let { pbEnv ->
|
||||
env.forEach {
|
||||
|
@ -38,16 +39,22 @@ open class RunCommand(val command: String) {
|
|||
if (callSucceeded) {
|
||||
successCallback(fromStream(process.inputStream))
|
||||
} else {
|
||||
errorCallback(listOf("$command failed") + fromStream(process.errorStream))
|
||||
val stream = if (process.errorStream.available() > 0) process.errorStream
|
||||
else if (process.inputStream.available() > 0) process.inputStream
|
||||
else null
|
||||
val errorString =
|
||||
if (stream != null) fromStream(stream).joinToString("\n")
|
||||
else "<no output>"
|
||||
errorCallback(listOf("$command failed") + errorString)
|
||||
}
|
||||
return if (callSucceeded) 0 else 1
|
||||
|
||||
}
|
||||
|
||||
private fun fromStream(ins: InputStream) : List<String> {
|
||||
val result = arrayListOf<String>()
|
||||
val br = BufferedReader(InputStreamReader(ins))
|
||||
var line = br.readLine()
|
||||
|
||||
while (line != null) {
|
||||
result.add(line)
|
||||
line = br.readLine()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue