Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
BppleMan committed Dec 28, 2023
2 parents 6d24428 + 99c7cac commit f49c4e5
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ko_fi: bppleman
2 changes: 2 additions & 0 deletions .github/workflows/gradle-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ name: Kommand Publish

on:
push:
branches:
- main
tags:
- "*"
workflow_dispatch:
Expand Down
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ https://github.com/kgit2/kommand/blob/af7c721f774550d0d5f72758f101074c12fae134/k

https://github.com/kgit2/kommand/blob/af7c721f774550d0d5f72758f101074c12fae134/kommand-examples/example3/src/commonMain/kotlin/com/kgit2/kommand/Main.kt#L1-L12

### 超时检测

https://github.com/kgit2/kommand/blob/7367c60db7b3475be0de17474dbcec3d518894ba/kommand-examples/timeout/src/appleMain/kotlin/com/kgit2/kommand/Main.kt#L1-L60

完成示例 [kommand-examples/timeout](kommand-examples/timeout).

依赖:

- `required` [kotlinx-coroutines](https://github.com/Kotlin/kotlinx.coroutines)

## 自行编译

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
[![Kommand Test](https://github.com/kgit2/kommand/actions/workflows/gradle.yml/badge.svg?branch=test)](https://github.com/kgit2/kommand/actions/workflows/gradle.yml)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.kgit2/kommand/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.kgit2/kommand)


[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/B0B2SNCVJ)

![logo](https://raw.githubusercontent.com/floater-git/Artist/main/kommand/logo.png)

# Kommand
Expand Down Expand Up @@ -81,6 +84,15 @@ https://github.com/kgit2/kommand/blob/af7c721f774550d0d5f72758f101074c12fae134/k

https://github.com/kgit2/kommand/blob/af7c721f774550d0d5f72758f101074c12fae134/kommand-examples/example3/src/commonMain/kotlin/com/kgit2/kommand/Main.kt#L1-L12

### Timeout Detection

https://github.com/kgit2/kommand/blob/7367c60db7b3475be0de17474dbcec3d518894ba/kommand-examples/timeout/src/appleMain/kotlin/com/kgit2/kommand/Main.kt#L1-L60

Full example check [kommand-examples/timeout](kommand-examples/timeout).

Dependency:

- `required` [kotlinx-coroutines](https://github.com/Kotlin/kotlinx.coroutines)

## Build by yourself

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "com.kgit2"
version = "2.0.1"
version = "2.0.2"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macosArm64Leaks:
./gradlew :cleanMacosArm64Test :macosArm64Test
leaks -atExit -- build/bin/macosArm64/debugTest/test.kexe

autoPublish: build publishToSonatype macosArm64Leaks closeSonatype releaseSonatype
autoPublish: macosArm64Leaks build publishToSonatype closeSonatype releaseSonatype

teamcity:
#-v <path to logs directory>:/opt/teamcity/logs
Expand Down
79 changes: 79 additions & 0 deletions kommand-examples/timeout/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import org.gradle.nativeplatform.platform.internal.DefaultNativePlatform

val currentPlatform: Platform = when {
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MACOS_X64
DefaultNativePlatform.getCurrentOperatingSystem().isMacOsX && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.MACOS_ARM64
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.LINUX_X64
DefaultNativePlatform.getCurrentOperatingSystem().isLinux && DefaultNativePlatform.getCurrentArchitecture().isArm64 -> Platform.LINUX_ARM64
DefaultNativePlatform.getCurrentOperatingSystem().isWindows && DefaultNativePlatform.getCurrentArchitecture().isAmd64 -> Platform.MINGW_X64
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}

plugins {
kotlin("multiplatform")
}

repositories {
mavenCentral()
gradlePluginPortal()
}

kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "17"
}
withJava()
testRuns["test"].executionTask.configure {
useJUnitPlatform()
}
}

val nativeTarget = when (currentPlatform) {
Platform.MACOS_X64 -> macosX64()
Platform.MACOS_ARM64 -> macosArm64()
Platform.LINUX_X64 -> linuxX64()
Platform.LINUX_ARM64 -> linuxArm64()
Platform.MINGW_X64 -> mingwX64()
}

nativeTarget.apply {
binaries {
executable {
entryPoint = "com.kgit2.kommand.main"
}
}
}

applyDefaultHierarchyTemplate()

sourceSets {
// add opt-in
all {
languageSettings.optIn("kotlinx.cinterop.UnsafeNumber")
languageSettings.optIn("kotlinx.cinterop.ExperimentalForeignApi")
languageSettings.optIn("kotlin.experimental.ExperimentalNativeApi")
languageSettings.optIn("kotlin.native.runtime.NativeRuntimeApi")
languageSettings.optIn("kotlin.ExperimentalStdlibApi")
}

commonMain {
dependencies {
implementation(rootProject)
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0-RC2")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.5.0")
}
}
}
}

enum class Platform(
val archName: String
) {
MACOS_X64("x86_64-apple-darwin"),
MACOS_ARM64("aarch64-apple-darwin"),
LINUX_X64("x86_64-unknown-linux-gnu"),
LINUX_ARM64("aarch64-unknown-linux-gnu"),
MINGW_X64("x86_64-pc-windows-gnu"),
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.kgit2.kommand

import com.kgit2.kommand.process.Command
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import kotlinx.datetime.Clock

fun main() = runBlocking(Dispatchers.Default) {
// Sleep with regular
val start = Clock.System.now()
val status = Command("sleep").arg("5").status()
println("status: $status elapsed: ${Clock.System.now() - start}")

// Sleep with timeout detection and timeout determination
val start2 = Clock.System.now()
val child = Command("sleep").arg("5").spawn()
val childJob = async(Dispatchers.IO) {
runCatching {
child.wait()
}.onFailure {
println("child result: $it")
}.getOrNull()
}
runCatching {
withTimeout(3000) {
childJob.await()
}
}.onSuccess {
println("status: $it elapsed: ${Clock.System.now() - start2}")
}.onFailure {
child.kill()
println("status: $it elapsed: ${Clock.System.now() - start2}")
}

// Sleep with timeout detection and determination that it will not timeout
val start3 = Clock.System.now()
val child2 = Command("sleep").arg("2").spawn()
val childJob2 = async(Dispatchers.IO) {
runCatching {
child2.wait()
}.onFailure {
println("child result: $it")
}.getOrNull()
}
runCatching {
withTimeout(3000) {
childJob2.await()
}
}.onSuccess {
println("status: $it elapsed: ${Clock.System.now() - start3}")
}.onFailure {
child2.kill()
println("status: $it elapsed: ${Clock.System.now() - start3}")
}

Unit
}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ include(":kommand-examples")
include(":kommand-examples:example1")
include(":kommand-examples:example2")
include(":kommand-examples:example3")
include(":kommand-examples:timeout")

0 comments on commit f49c4e5

Please sign in to comment.