Skip to content

Commit

Permalink
Linux support
Browse files Browse the repository at this point in the history
- Create actuals
- Switch to kotlinx.io as it supports arm
  • Loading branch information
schlaubi authored and schlaubi committed Jan 24, 2024
1 parent 0741141 commit 14a3f4a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 20 deletions.
5 changes: 3 additions & 2 deletions buildSrc/src/main/kotlin/kord-native-module.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ plugins {
}

kotlin {
// There are issues with compiling the linux variant on linux
// Please use WSL if you need to work on the linux port
if(!Os.isFamily(Os.FAMILY_WINDOWS)) {
linuxX64()
// Waiting for Ktor
// https://youtrack.jetbrains.com/issue/KTOR-872
// https://youtrack.jetbrains.com/issue/KTOR-6173
//linuxArm64()
}


mingwX64()

macosArm64()
Expand Down
2 changes: 1 addition & 1 deletion common/src/linuxMain/kotlin/HttpEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import io.ktor.client.engine.curl.*

/** @suppress */
@KordInternal
public actual object HttpEngine : HttpClientEngineFactory<HttpClientEngineConfig> by Curl
public actual fun httpEngine(): HttpClientEngineFactory<HttpClientEngineConfig> = Curl
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ kotlinpoet = "1.15.3" # https://github.com/square/kotlinpoet
junit5 = "5.10.1" # https://github.com/junit-team/junit5
mockk = "1.13.8" # https://github.com/mockk/mockk
slf4j = "2.0.9" # https://www.slf4j.org
okio = "3.5.0"
kotlinx-io = "0.3.0" # https://github.com/Kotlin/kotlinx-io/

# plugins
dokka = "1.9.10" # https://github.com/Kotlin/dokka
Expand Down Expand Up @@ -83,7 +83,7 @@ kotlin-test-junit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5", versi
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit5" }
mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j" }
okio = { module = "com.squareup.okio:okio", version.ref = "okio" }
kotlinx-io = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" }

# actually plugins, not libraries, but used is 'buildSrc/build.gradle.kts' as implementation dependencies:
# https://docs.gradle.org/current/userguide/custom_plugins.html#applying_external_plugins_in_precompiled_script_plugins
Expand Down
2 changes: 1 addition & 1 deletion test-kit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ kotlin {
}
nativeMain {
dependencies {
api(libs.okio)
api(libs.kotlinx.io)
}
}
}
Expand Down
22 changes: 8 additions & 14 deletions test-kit/src/nativeMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ package dev.kord.test
import io.ktor.utils.io.*
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.toKString
import okio.BufferedSource
import okio.FileSystem
import okio.IOException
import okio.Path.Companion.toPath
import kotlinx.io.*
import kotlinx.io.files.Path
import kotlinx.io.files.SystemFileSystem
import platform.posix.getenv
import kotlin.experimental.ExperimentalNativeApi
import kotlin.native.Platform
Expand All @@ -27,22 +26,17 @@ actual object Platform {
@OptIn(ExperimentalForeignApi::class)
actual fun getEnv(name: String) = getenv(name)?.toKString()

private fun actutalPath(path: String) =
"src/commonTest/resources/$path".toPath()

actual suspend fun file(project: String, path: String): String = read(path, BufferedSource::readUtf8)
actual suspend fun file(project: String, path: String): String = read(path, Source::readString)

actual suspend fun readFile(project: String, path: String): ByteReadChannel =
read(path) { ByteReadChannel(readByteArray()) }

private inline fun <T> read(path: String, readerAction: BufferedSource.() -> T): T {
val actualPath = actutalPath(path)
private inline fun <T> read(path: String, readerAction: Source.() -> T): T {
val actualPath = Path(path)
return try {
FileSystem.SYSTEM.read(actualPath, readerAction)
SystemFileSystem.source(actualPath).buffered().readerAction()
} catch (e: Throwable) {
val pwd = FileSystem.SYSTEM.canonicalize(".".toPath())
val absolutePath = pwd / actualPath
throw FileNotFoundException(absolutePath.toString(), e)
throw FileNotFoundException(actualPath.toString(), e)
}
}

Expand Down

0 comments on commit 14a3f4a

Please sign in to comment.