Compare commits
11 Commits
3ce145b19e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
13b8597810
|
|||
|
a80bb11652
|
|||
|
dc92be5367
|
|||
|
2679f688d5
|
|||
|
c961342462
|
|||
|
fd54489747
|
|||
|
c1cbd96558
|
|||
|
9336409590
|
|||
|
a64703cf2f
|
|||
|
4ed863c561
|
|||
|
5485dd84d6
|
@@ -1,36 +0,0 @@
|
|||||||
on:
|
|
||||||
push:
|
|
||||||
branches:
|
|
||||||
- main
|
|
||||||
|
|
||||||
env:
|
|
||||||
GIT__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/
|
|
||||||
47
.gitea/workflows/publish.yaml
Normal file
47
.gitea/workflows/publish.yaml
Normal 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 }}
|
||||||
28
README.md
Normal file
28
README.md
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# shell-kt
|
||||||
|
|
||||||
|
`shell-kt` is a Kotlin Multiplatform library that enables the execution of shell commands.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
Add the following to your `build.gradle.kts`.
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation("com.infendro:shell:1.1.0")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
suspend fun main() {
|
||||||
|
val result = execute("echo", "Hello World!")
|
||||||
|
|
||||||
|
println(result.code) // 0
|
||||||
|
println(result.value) // Hello World!
|
||||||
|
}
|
||||||
|
```
|
||||||
@@ -1,54 +1,39 @@
|
|||||||
group = "com.infendro"
|
import com.infendro.plugin.infendro
|
||||||
version = "1.0.0"
|
import com.infendro.plugin.token
|
||||||
|
|
||||||
repositories {
|
group = "com.infendro"
|
||||||
mavenCentral()
|
version = "1.1.0"
|
||||||
}
|
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
|
alias(libs.plugins.infendro)
|
||||||
alias(libs.plugins.multiplatform)
|
alias(libs.plugins.multiplatform)
|
||||||
id("maven-publish")
|
id("maven-publish")
|
||||||
}
|
}
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
jvm()
|
jvm()
|
||||||
js {
|
|
||||||
nodejs()
|
|
||||||
}
|
|
||||||
linuxX64()
|
linuxX64()
|
||||||
|
linuxArm64()
|
||||||
|
mingwX64()
|
||||||
|
macosX64()
|
||||||
|
macosArm64()
|
||||||
|
js { nodejs() }
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
jsMain.dependencies {
|
jsMain.dependencies {
|
||||||
implementation(libs.node)
|
implementation(libs.node)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
compilerOptions {
|
||||||
repositories {
|
freeCompilerArgs.add("-opt-in=kotlinx.cinterop.ExperimentalForeignApi")
|
||||||
maven {
|
|
||||||
url = uri("https://git.infendro.com/api/packages/Infendro/maven")
|
|
||||||
authentication.create("header", HttpHeaderAuthentication::class)
|
|
||||||
credentials(HttpHeaderCredentials::class) {
|
|
||||||
val token = secret("git.token") ?: throw GradleException()
|
|
||||||
|
|
||||||
name = "Authorization"
|
|
||||||
value = "token $token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun secret(
|
publishing.repositories {
|
||||||
key: String,
|
infendro(token)
|
||||||
): String? {
|
|
||||||
val property = key
|
|
||||||
val environment = key
|
|
||||||
.uppercase()
|
|
||||||
.replace(".", "__")
|
|
||||||
|
|
||||||
val prop = properties[property] as String?
|
|
||||||
val env = System.getenv(environment)
|
|
||||||
|
|
||||||
return prop ?: env
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
[versions]
|
[versions]
|
||||||
kotlin = "2.1.20"
|
infendro = "1.1.0"
|
||||||
node = "2025.6.9-22.13.10"
|
kotlin = "2.3.0"
|
||||||
|
node = "2026.2.11-24.10.4"
|
||||||
|
|
||||||
[plugins]
|
[plugins]
|
||||||
|
infendro = { id = "com.infendro.plugin", version.ref = "infendro" }
|
||||||
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
|
||||||
|
|
||||||
[libraries]
|
[libraries]
|
||||||
|
|||||||
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Binary file not shown.
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,6 +1,6 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
|
||||||
networkTimeout=10000
|
networkTimeout=10000
|
||||||
validateDistributionUrl=true
|
validateDistributionUrl=true
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
|||||||
12
gradlew
vendored
12
gradlew
vendored
@@ -15,6 +15,8 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
@@ -55,7 +57,7 @@
|
|||||||
# Darwin, MinGW, and NonStop.
|
# Darwin, MinGW, and NonStop.
|
||||||
#
|
#
|
||||||
# (3) This script is generated from the Groovy template
|
# (3) This script is generated from the Groovy template
|
||||||
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
# within the Gradle project.
|
# within the Gradle project.
|
||||||
#
|
#
|
||||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
@@ -84,7 +86,7 @@ done
|
|||||||
# shellcheck disable=SC2034
|
# shellcheck disable=SC2034
|
||||||
APP_BASE_NAME=${0##*/}
|
APP_BASE_NAME=${0##*/}
|
||||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||||
|
|
||||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
MAX_FD=maximum
|
MAX_FD=maximum
|
||||||
@@ -112,7 +114,7 @@ case "$( uname )" in #(
|
|||||||
NONSTOP* ) nonstop=true ;;
|
NONSTOP* ) nonstop=true ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
CLASSPATH="\\\"\\\""
|
||||||
|
|
||||||
|
|
||||||
# Determine the Java command to use to start the JVM.
|
# Determine the Java command to use to start the JVM.
|
||||||
@@ -203,7 +205,7 @@ fi
|
|||||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
# Collect all arguments for the java command:
|
# Collect all arguments for the java command:
|
||||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
# and any embedded shellness will be escaped.
|
# and any embedded shellness will be escaped.
|
||||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
# treated as '${Hostname}' itself on the command line.
|
# treated as '${Hostname}' itself on the command line.
|
||||||
@@ -211,7 +213,7 @@ DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
|||||||
set -- \
|
set -- \
|
||||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
-classpath "$CLASSPATH" \
|
-classpath "$CLASSPATH" \
|
||||||
org.gradle.wrapper.GradleWrapperMain \
|
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||||
"$@"
|
"$@"
|
||||||
|
|
||||||
# Stop when "xargs" is not available.
|
# Stop when "xargs" is not available.
|
||||||
|
|||||||
26
gradlew.bat
vendored
26
gradlew.bat
vendored
@@ -13,6 +13,8 @@
|
|||||||
@rem See the License for the specific language governing permissions and
|
@rem See the License for the specific language governing permissions and
|
||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
@rem SPDX-License-Identifier: Apache-2.0
|
||||||
|
@rem
|
||||||
|
|
||||||
@if "%DEBUG%"=="" @echo off
|
@if "%DEBUG%"=="" @echo off
|
||||||
@rem ##########################################################################
|
@rem ##########################################################################
|
||||||
@@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
|
|||||||
%JAVA_EXE% -version >NUL 2>&1
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
if %ERRORLEVEL% equ 0 goto execute
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
@@ -57,22 +59,22 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
|||||||
|
|
||||||
if exist "%JAVA_EXE%" goto execute
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||||
echo.
|
echo. 1>&2
|
||||||
echo Please set the JAVA_HOME variable in your environment to match the
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
echo location of your Java installation.
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
goto fail
|
goto fail
|
||||||
|
|
||||||
:execute
|
:execute
|
||||||
@rem Setup the command line
|
@rem Setup the command line
|
||||||
|
|
||||||
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
set CLASSPATH=
|
||||||
|
|
||||||
|
|
||||||
@rem Execute Gradle
|
@rem Execute Gradle
|
||||||
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %*
|
||||||
|
|
||||||
:end
|
:end
|
||||||
@rem End local scope for the variables with windows NT shell
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
|||||||
@@ -1 +1,6 @@
|
|||||||
rootProject.name = "shell"
|
rootProject.name = "shell"
|
||||||
|
|
||||||
|
pluginManagement.repositories {
|
||||||
|
maven("https://git.infendro.com/api/packages/Infendro/maven")
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|||||||
@@ -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
|
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
package com.infendro.shell
|
package com.infendro.shell
|
||||||
|
|
||||||
import kotlin.coroutines.resume
|
|
||||||
import kotlin.coroutines.suspendCoroutine
|
|
||||||
import node.childProcess.ExecOptions
|
import node.childProcess.ExecOptions
|
||||||
import node.childProcess.exec
|
import node.childProcess.exec
|
||||||
|
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>,
|
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
|
|
||||||
continuation.resume(Shell.Result(code, value))
|
val result = Result.from(code, { stdout as String }, { stderr as String })
|
||||||
|
continuation.resume(result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +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>,
|
val process = ProcessBuilder(*command).also {
|
||||||
directory: String?,
|
if (directory != null) {
|
||||||
): Shell.Result {
|
val dir = File(directory)
|
||||||
val process = ProcessBuilder(*command)
|
it.directory(dir)
|
||||||
.also {
|
|
||||||
if (directory != null) {
|
|
||||||
it.directory(File(directory))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.start()
|
}.start()
|
||||||
val code = process.waitFor()
|
val code = process.waitFor()
|
||||||
val value = process
|
|
||||||
.let { if (code == 0) it.inputReader() else it.errorReader() }
|
val result = Result.from(code, { process.inputReader().readText() }, { process.errorReader().readText() })
|
||||||
.readText()
|
continuation.resume(result)
|
||||||
return Shell.Result(code, value)
|
|
||||||
}
|
}
|
||||||
|
|||||||
20
src/linuxMain/kotlin/com/infendro/shell/Shell.linux.kt
Normal file
20
src/linuxMain/kotlin/com/infendro/shell/Shell.linux.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/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
|
||||||
|
}
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
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<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)
|
|
||||||
}
|
|
||||||
|
|
||||||
@ExperimentalForeignApi
|
|
||||||
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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user