kotlin - 使用 Kotlin/Native,如何执行外部操作系统可执行文件或 $PATH 命令并与其 stdin、stdout 和 stderr 流交互?

标签 kotlin kotlin-native

我正在为用 Kotlin/Native 编写的命令行工具进行概念验证,因为它似乎是跨平台二进制文件的理想语言和运行时。

此命令行工具需要定期与操作系统用户 $PATH 或 shell 环境中的操作系统可执行文件、命令和/或 shell 函数进行交互。但是,我在 kotlin-native samples 中没有看到任何示例或任何可能指示如何执行以下操作的文档:

  • 执行操作系统可执行文件或 $PATH 命令
  • 获取执行的返回码(整数)
  • 确保执行进程的 stdin、stdout 和 stderr 文件描述符流可以分别表示为 OutputStream 和 InputStreams

  • 在 JVM-land 中,我们会使用 java.lang.ProcessBuilder对于所有这些,但在 Kotlin/Native 中显然不可用。

    我找到了 cinterop/posix platform.posix.system函数,但这并不能让您访问进程的流。

    在我的网络研究中,我发现了一个非常好的 C tutorial这表明唯一干净的方法是使用 forkdup2等等,但我不清楚这是否或如何转化为 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/windows
    import 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/

    相关文章:

    android - 为什么开发人员不直接将参数传递给 setOnCheckedChangeListener

    android - 如何理解和改进协程中的async-await?

    kotlin - generateCMake 似乎没有生成所有文件

    c - Kotlin native : C objects ownership and lifespan

    ios - iOS 上的 Kotlin Native 项目中的 libPhonenumber(来自 Google)

    kotlin - 如何将 Kotlin ByteArray 转换为 NsData,反之亦然

    generics - 如何在 kotlin 中使用泛型编写 lambda?

    java - 动态设置 textView 的省略号和最大行在 recyclerview 项目中不能正常工作

    Kotlin/Native - 如何使用 C 库和 Klib 文件?

    kotlin - Kotlin/Native 中的 .freeze() 是什么?