Compare commits
3 Commits
dc92be5367
...
develop
| Author | SHA1 | Date | |
|---|---|---|---|
|
db7a05f88b
|
|||
|
13b8597810
|
|||
|
a80bb11652
|
@@ -1,6 +1,6 @@
|
|||||||
# shell-kt
|
# shell.kt
|
||||||
|
|
||||||
`shell-kt` is a Kotlin Multiplatform library that enables the execution of shell commands.
|
`shell.kt` is a Kotlin Multiplatform library that enables the execution of shell commands.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package com.infendro.shell
|
package com.infendro.shell
|
||||||
|
|
||||||
import kotlinx.cinterop.CPointer
|
import platform.posix.chdir
|
||||||
import kotlinx.cinterop.refTo
|
import platform.posix.pclose
|
||||||
import kotlinx.cinterop.toKString
|
import platform.posix.popen
|
||||||
import platform.posix.*
|
|
||||||
import kotlin.coroutines.resume
|
import kotlin.coroutines.resume
|
||||||
import kotlin.coroutines.suspendCoroutine
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
@@ -19,13 +18,3 @@ actual suspend fun execute(vararg command: String, directory: String?): Result =
|
|||||||
val result = Result.from(code, value)
|
val result = Result.from(code, value)
|
||||||
continuation.resume(result)
|
continuation.resume(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun CPointer<FILE>.readLines(): List<String> {
|
|
||||||
val lines = mutableListOf<String>()
|
|
||||||
val buffer = ByteArray(4096)
|
|
||||||
while (true) {
|
|
||||||
val line = fgets(buffer.refTo(0), buffer.size, this) ?: break
|
|
||||||
lines.add(line.toKString())
|
|
||||||
}
|
|
||||||
return lines
|
|
||||||
}
|
|
||||||
20
src/macosMain/kotlin/com/infendro/shell/Shell.macos.kt
Normal file
20
src/macosMain/kotlin/com/infendro/shell/Shell.macos.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.infendro.shell
|
||||||
|
|
||||||
|
import platform.posix.chdir
|
||||||
|
import platform.posix.pclose
|
||||||
|
import platform.posix.popen
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
|
actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
|
||||||
|
val command = command.joinToString(" ") { """"$it"""" }
|
||||||
|
if (directory != null) {
|
||||||
|
chdir(directory)
|
||||||
|
}
|
||||||
|
val stream = popen(command, "r")!!
|
||||||
|
val value = stream.readLines().joinToString("")
|
||||||
|
val code = pclose(stream)
|
||||||
|
|
||||||
|
val result = Result.from(code, value)
|
||||||
|
continuation.resume(result)
|
||||||
|
}
|
||||||
20
src/mingwMain/kotlin/shell/Shell.mingw.kt
Normal file
20
src/mingwMain/kotlin/shell/Shell.mingw.kt
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package com.infendro.shell
|
||||||
|
|
||||||
|
import platform.posix._pclose
|
||||||
|
import platform.posix._popen
|
||||||
|
import platform.posix.chdir
|
||||||
|
import kotlin.coroutines.resume
|
||||||
|
import kotlin.coroutines.suspendCoroutine
|
||||||
|
|
||||||
|
actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
|
||||||
|
val command = command.joinToString(" ") { """"$it"""" }
|
||||||
|
if (directory != null) {
|
||||||
|
chdir(directory)
|
||||||
|
}
|
||||||
|
val stream = _popen(command, "r")!!
|
||||||
|
val value = stream.readLines().joinToString("")
|
||||||
|
val code = _pclose(stream)
|
||||||
|
|
||||||
|
val result = Result.from(code, value)
|
||||||
|
continuation.resume(result)
|
||||||
|
}
|
||||||
17
src/nativeMain/kotlin/com.infendro.shell/C.kt
Normal file
17
src/nativeMain/kotlin/com.infendro.shell/C.kt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.infendro.shell
|
||||||
|
|
||||||
|
import kotlinx.cinterop.CPointer
|
||||||
|
import kotlinx.cinterop.refTo
|
||||||
|
import kotlinx.cinterop.toKString
|
||||||
|
import platform.posix.FILE
|
||||||
|
import platform.posix.fgets
|
||||||
|
|
||||||
|
internal fun CPointer<FILE>.readLines(): List<String> {
|
||||||
|
val lines = mutableListOf<String>()
|
||||||
|
val buffer = ByteArray(4096)
|
||||||
|
while (true) {
|
||||||
|
val line = fgets(buffer.refTo(0), buffer.size, this) ?: break
|
||||||
|
lines.add(line.toKString())
|
||||||
|
}
|
||||||
|
return lines
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user