diff --git a/build.gradle.kts b/build.gradle.kts index 7656cd8..a035e3e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -26,6 +26,10 @@ kotlin { implementation(libs.node) } } + + compilerOptions { + freeCompilerArgs.add("-opt-in=kotlinx.cinterop.ExperimentalForeignApi") + } } publishing.repositories { diff --git a/src/jsMain/kotlin/com/infendro/shell/Shell.js.kt b/src/jsMain/kotlin/com/infendro/shell/Shell.js.kt index 290477b..d5b7826 100644 --- a/src/jsMain/kotlin/com/infendro/shell/Shell.js.kt +++ b/src/jsMain/kotlin/com/infendro/shell/Shell.js.kt @@ -1,9 +1,9 @@ package com.infendro.shell -import kotlin.coroutines.resume -import kotlin.coroutines.suspendCoroutine import node.childProcess.ExecOptions import node.childProcess.exec +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine internal actual suspend fun execute( command: Array, @@ -13,7 +13,7 @@ internal actual suspend fun execute( val options = ExecOptions.invoke(cwd = directory) exec(command, options) { error, stdout, stderr -> val code = if (error == null) 0 else error.code!!.toInt() - val value = if (error == null) stdout else stderr + val value = (if (error == null) stdout else stderr) as String continuation.resume(Shell.Result(code, value)) } } diff --git a/src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt b/src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt index c713ebc..e47b3d6 100644 --- a/src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt +++ b/src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt @@ -6,16 +6,12 @@ internal actual suspend fun execute( command: Array, directory: String?, ): Shell.Result { - val process = ProcessBuilder(*command) - .also { - if (directory != null) { - it.directory(File(directory)) - } + val process = ProcessBuilder(*command).also { + if (directory != null) { + it.directory(File(directory)) } - .start() + }.start() val code = process.waitFor() - val value = process - .let { if (code == 0) it.inputReader() else it.errorReader() } - .readText() + val value = (if (code == 0) process.inputReader() else process.errorReader()).readText() return Shell.Result(code, value) } diff --git a/src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt b/src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt index 315cf82..c3371d3 100644 --- a/src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt +++ b/src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt @@ -1,12 +1,10 @@ package com.infendro.shell import kotlinx.cinterop.CPointer -import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.refTo import kotlinx.cinterop.toKString import platform.posix.* -@ExperimentalForeignApi internal actual suspend fun execute( command: Array, directory: String?, @@ -21,7 +19,6 @@ internal actual suspend fun execute( return Shell.Result(code, value) } -@ExperimentalForeignApi private fun CPointer.readLines(): List { val lines = mutableListOf() val buffer = ByteArray(4096)