Compare commits

..

6 Commits

Author SHA1 Message Date
db7a05f88b Update README.md 2026-04-16 16:12:18 +00:00
13b8597810 Merge pull request '1.1.0 Fix' (#3) from develop into main
All checks were successful
/ publish (push) Successful in 3m23s
Reviewed-on: Infendro/shell-kt#3
2026-02-16 16:54:29 +00:00
a80bb11652 Fix native implementation 2026-02-16 17:53:50 +01:00
dc92be5367 Merge pull request '1.1.0 release' (#2) from develop into main
Some checks failed
/ publish (push) Failing after 1m14s
Reviewed-on: Infendro/shell-kt#2
2026-02-15 13:13:31 +00:00
2679f688d5 split ci 2026-02-15 14:06:35 +01:00
c961342462 improve ci 2026-01-13 18:02:47 +01:00
13 changed files with 167 additions and 124 deletions

View File

@@ -1,36 +0,0 @@
on:
push:
branches:
- main
env:
INFENDRO__TOKEN: ${{ secrets.TOKEN }}
jobs:
publish:
runs-on: linux
steps:
- uses: actions/checkout@v4
- uses: actions/cache/restore@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/
- uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- run: ./gradlew publish
- uses: actions/cache/save@v4
with:
key: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
~/.konan/

View File

@@ -0,0 +1,47 @@
on:
push:
branches: [ main ]
jobs:
publish:
runs-on: linux
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Java
uses: actions/setup-java@v4
with:
java-version: '24'
distribution: 'temurin'
- name: Cache Gradle
uses: actions/cache@v4
with:
key: gradle-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: gradle
path: |
~/.gradle/caches/
~/.gradle/wrapper/
- name: Cache Konan
uses: actions/cache@v4
with:
key: konan-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: konan
path: |
~/.konan/
- name: Cache Node
uses: actions/cache@v4
with:
key: node-${{ hashFiles('**/libs.versions.toml') }}
restore-keys: node
path: |
~/.gradle/nodejs
~/.gradle/yarn
- name: Publish
run: ./gradlew publish
env:
INFENDRO__TOKEN: ${{ secrets.TOKEN }}

View File

@@ -1,15 +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.
## Targets
| Target | Status |
|------------------|---------------|
| JVM | Supported |
| JavaScript | Supported |
| Native (Linux) | Supported |
| Native (Windows) | Not Supported |
## Installation ## Installation
@@ -21,21 +12,15 @@ repositories {
} }
dependencies { dependencies {
implementation("com.infendro:shell:1.0.0") implementation("com.infendro:shell:1.1.0")
} }
``` ```
## Usage ## Usage
```kotlin ```kotlin
import com.infendro.shell.Shell
suspend fun main() { suspend fun main() {
// the command to execute val result = execute("echo", "Hello World!")
val command = arrayOf("echo", "Hello World!")
// execute the command
val result = Shell.run(command)
println(result.code) // 0 println(result.code) // 0
println(result.value) // Hello World! println(result.value) // Hello World!

View File

@@ -2,7 +2,7 @@ import com.infendro.plugin.infendro
import com.infendro.plugin.token import com.infendro.plugin.token
group = "com.infendro" group = "com.infendro"
version = "1.0.0" version = "1.1.0"
plugins { plugins {
alias(libs.plugins.infendro) alias(libs.plugins.infendro)
@@ -12,10 +12,12 @@ plugins {
kotlin { kotlin {
jvm() jvm()
js {
nodejs()
}
linuxX64() linuxX64()
linuxArm64()
mingwX64()
macosX64()
macosArm64()
js { nodejs() }
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -1,7 +1,7 @@
[versions] [versions]
infendro = "1.0.0" infendro = "1.1.0"
kotlin = "2.2.21" kotlin = "2.3.0"
node = "2025.12.5-24.7.1" node = "2026.2.11-24.10.4"
[plugins] [plugins]
infendro = { id = "com.infendro.plugin", version.ref = "infendro" } infendro = { id = "com.infendro.plugin", version.ref = "infendro" }

View File

@@ -1,20 +1,20 @@
package com.infendro.shell package com.infendro.shell
object Shell { data class Result(
suspend fun run( val code: Int,
command: Array<String>, val value: String,
directory: String? = null, ) {
): Result { val success: Boolean
return execute(command, directory) get() = code == 0
}
class Result( internal companion object {
val code: Int, fun from(code: Int, value: String) = Result(code, value)
val value: String,
) fun from(code: Int, success: () -> String, error: () -> String) = when (code) {
0 -> Result(code, success())
else -> Result(code, error())
}
}
} }
internal expect suspend fun execute( expect suspend fun execute(vararg command: String, directory: String? = null): Result
command: Array<String>,
directory: String?,
): Shell.Result

View File

@@ -5,15 +5,13 @@ import node.childProcess.exec
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
internal actual suspend fun execute( actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
command: Array<String>, val command = command.joinToString(" ") { """"$it"""" }
directory: String?, val options = ExecOptions(cwd = directory)
): Shell.Result = suspendCoroutine { continuation ->
val command = command.joinToString(" ") { "\"$it\"" }
val options = ExecOptions.invoke(cwd = directory)
exec(command, options) { error, stdout, stderr -> exec(command, options) { error, stdout, stderr ->
val code = if (error == null) 0 else error.code!!.toInt() val code = error?.code?.toInt() ?: 0
val value = (if (error == null) stdout else stderr) as String
continuation.resume(Shell.Result(code, value)) val result = Result.from(code, { stdout as String }, { stderr as String })
continuation.resume(result)
} }
} }

View File

@@ -1,17 +1,18 @@
package com.infendro.shell package com.infendro.shell
import java.io.File import java.io.File
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
internal actual suspend fun execute( actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
command: Array<String>,
directory: String?,
): Shell.Result {
val process = ProcessBuilder(*command).also { val process = ProcessBuilder(*command).also {
if (directory != null) { if (directory != null) {
it.directory(File(directory)) val dir = File(directory)
it.directory(dir)
} }
}.start() }.start()
val code = process.waitFor() val code = process.waitFor()
val value = (if (code == 0) process.inputReader() else process.errorReader()).readText()
return Shell.Result(code, value) val result = Result.from(code, { process.inputReader().readText() }, { process.errorReader().readText() })
continuation.resume(result)
} }

View 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)
}

View 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)
}

View 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)
}

View 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
}

View File

@@ -1,31 +0,0 @@
package com.infendro.shell
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.*
internal actual suspend fun execute(
command: Array<String>,
directory: String?,
): Shell.Result {
val command = command.joinToString(" ") { "\"$it\"" }
if (directory != null) {
chdir(directory)
}
val stream = popen(command, "r")!!
val value = stream.readLines().joinToString("")
val code = pclose(stream)
return Shell.Result(code, value)
}
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)
if (line == null) break
lines.add(line.toKString())
}
return lines
}