我正在为用 Kotlin/Native 编写的命令行工具进行概念验证,因为它似乎是跨平台二进制文件的理想语言和运行时。
此命令行工具需要定期与操作系统用户 $PATH 或 shell 环境中的操作系统可执行文件、命令和/或 shell 函数进行交互。但是,我在 kotlin-native samples 中没有看到任何示例或任何可能指示如何执行以下操作的文档:
在 JVM-land 中,我们会使用
java.lang.ProcessBuilder
对于所有这些,但在 Kotlin/Native 中显然不可用。我找到了 cinterop/posix
platform.posix.system
函数,但这并不能让您访问进程的流。在我的网络研究中,我发现了一个非常好的 C tutorial这表明唯一干净的方法是使用
fork
和 dup2
等等,但我不清楚这是否或如何转化为 Kotlin/Native 代码。
最佳答案
我用 kotlin native 做了一些尝试,并成功地为 jvm、mac、unix 和(未经测试的)windows 执行了 posix exec 命令......
https://github.com/hoffipublic/minimal_kotlin_multiplatform
共同目标
fun String.executeCommand(
redirectStderr: Boolean = true
): String? = MppProcess.executeCommand(this, redirectStderr)
interface IMppProcess {
fun executeCommand(
command: String,
redirectStderr: Boolean = true
): String?
}
expect object MppProcess : IMppProcess {
override fun executeCommand(
command: String,
redirectStderr: Boolean
): String?
}
目标虚拟机actual object MppProcess : IMppProcess {
actual override fun executeCommand(
command: String,
redirectStderr: Boolean
): String? {
return runCatching {
ProcessBuilder(command.split(Regex("(?<!(\"|').{0,255}) | (?!.*\\1.*)")))
//.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.apply { if (redirectStderr) this.redirectError(ProcessBuilder.Redirect.PIPE) }
.start().apply { waitFor(60L, TimeUnit.SECONDS) }
.inputStream.bufferedReader().readText()
}.onFailure { it.printStackTrace() }.getOrNull()
}
}
目标 mac/unix/windowsimport kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.fgets
import platform.posix.pclose
import platform.posix.popen
actual object MppProcess : IMppProcess {
actual override fun executeCommand(
command: String,
redirectStderr: Boolean
): String? {
val commandToExecute = if (redirectStderr) "$command 2>&1" else command
val fp = popen(commandToExecute, "r") ?: error("Failed to run command: $command")
val stdout = buildString {
val buffer = ByteArray(4096)
while (true) {
val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
append(input.toKString())
}
}
val status = pclose(fp)
if (status != 0) {
error("Command `$command` failed with status $status${if (redirectStderr) ": $stdout" else ""}")
}
return stdout
}
}
现在才执行 ls ,但它有效。
关于kotlin - 使用 Kotlin/Native,如何执行外部操作系统可执行文件或 $PATH 命令并与其 stdin、stdout 和 stderr 流交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57190228/