-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Similar to `Util.sh`, but with Kotlin and coroutines
- Loading branch information
Showing
1 changed file
with
92 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
jacodb-ets/src/main/kotlin/org/jacodb/ets/utils/ProcessUtil.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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2022 UnitTestBot contributors (utbot.org) | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jacodb.ets.utils | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
import java.io.Reader | ||
|
||
object ProcessUtil { | ||
fun run(command: List<String>, input: String? = null): String { | ||
val reader = input?.reader() ?: "".reader() | ||
return run(command, reader) | ||
} | ||
|
||
fun run(command: List<String>, input: Reader): String { | ||
val process = ProcessBuilder(command).start() | ||
return communicate(process, input) | ||
} | ||
|
||
private fun communicate(process: Process, input: Reader): String { | ||
val stdout = StringBuilder() | ||
val stderr = StringBuilder() | ||
|
||
val scope = CoroutineScope(Dispatchers.IO) | ||
|
||
// Handle process input | ||
val stdinJob = scope.launch { | ||
process.outputWriter().use { writer -> | ||
input.copyTo(writer) | ||
} | ||
} | ||
|
||
// Launch output capture coroutines | ||
val stdoutJob = scope.launch { | ||
process.inputReader().useLines { lines -> | ||
lines.forEach { stdout.appendLine(it) } | ||
} | ||
} | ||
val stderrJob = scope.launch { | ||
process.errorReader().useLines { lines -> | ||
lines.forEach { stderr.appendLine(it) } | ||
} | ||
} | ||
|
||
// Wait for completion | ||
val exitCode = process.waitFor() | ||
runBlocking { | ||
stdinJob.join() | ||
stdoutJob.join() | ||
stderrJob.join() | ||
} | ||
|
||
if (exitCode != 0) { | ||
throw ProcessException( | ||
"Process failed with exit code $exitCode", | ||
exitCode, | ||
stderr.toString() | ||
) | ||
} | ||
|
||
return stdout.toString() | ||
} | ||
|
||
class ProcessException( | ||
message: String, | ||
val exitCode: Int, | ||
val errorOutput: String, | ||
) : RuntimeException(message) | ||
} | ||
|
||
fun main() { | ||
// Note: `ls -l /bin/` has big enough output to demonstrate the necessity | ||
// of separate output capture threads/coroutines. | ||
val output = ProcessUtil.run(listOf("ls", "-l", "/bin/")) | ||
println(output) | ||
} | ||