-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add MultiplatformSystem utils (#169)
- Loading branch information
Showing
23 changed files
with
206 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...pleNonDesktopMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.appleNonDesktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.github.ajalt.mordant.internal | ||
|
||
internal actual fun hasFileSystem(): Boolean = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
mordant/src/commonMain/kotlin/com/github/ajalt/mordant/platform/MultiplatformSystem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.github.ajalt.mordant.platform | ||
|
||
import com.github.ajalt.mordant.internal.exitProcessMpp | ||
import com.github.ajalt.mordant.internal.getEnv | ||
import com.github.ajalt.mordant.internal.readFileIfExists | ||
|
||
/** | ||
* Utility functions useful for multiplatform command line apps interacting with the system. | ||
*/ | ||
object MultiplatformSystem { | ||
/** | ||
* Get the value of an environment variable. | ||
* | ||
* If the variable is not set, this function returns `null`. | ||
*/ | ||
fun readEnvironmentVariable(key: String): String? { | ||
return getEnv(key) | ||
} | ||
|
||
/** | ||
* Immediately exit the process with the given [status] code. | ||
* | ||
* On browsers, where it's not possible to exit the process, this function is a no-op. | ||
*/ | ||
fun exitProcess(status: Int) { | ||
exitProcessMpp(status) | ||
} | ||
|
||
/** | ||
* Read the contents of a file as a UTF-8 string. | ||
* | ||
* @return The file contents decoded as a UTF-8 string, or `null` if the file could not be read. | ||
*/ | ||
fun readFileAsUtf8(path: String): String? { | ||
return try { | ||
readFileIfExists(path) | ||
} catch (e: Throwable) { | ||
null | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
mordant/src/commonTest/kotlin/com/github/ajalt/mordant/platform/MultiplatformSystemTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.ajalt.mordant.platform | ||
|
||
import com.github.ajalt.mordant.internal.hasFileSystem | ||
import io.kotest.matchers.nulls.shouldNotBeNull | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.string.shouldNotBeEmpty | ||
import kotlin.test.Test | ||
|
||
|
||
class MultiplatformSystemTest { | ||
@Test | ||
fun readEnvironmentVariable() { | ||
// The kotlin.test plugin doesn't provide a way to set environment variables that works on | ||
// all targets, so just pick a common one that should exist everywhere. | ||
val actual = MultiplatformSystem.readEnvironmentVariable("PATH") | ||
if (!hasFileSystem()) return | ||
actual.shouldNotBeNull().shouldNotBeEmpty() | ||
} | ||
|
||
@Test | ||
fun readFileAsUtf8() { | ||
val actual = MultiplatformSystem.readFileAsUtf8( | ||
// Most targets have a cwd of $moduleDir | ||
"src/commonTest/resources/multiplatform_system_test.txt" | ||
) ?: MultiplatformSystem.readFileAsUtf8( | ||
// js targets have a cwd of $projectDir/build/js/packages/mordant-mordant-test | ||
"../../../../mordant/src/commonTest/resources/multiplatform_system_test.txt" | ||
) | ||
if (!hasFileSystem()) return | ||
actual?.trim() shouldBe "pass" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../resources/META-INF/native-image/com.github.ajalt.mordant/mordant/native-image.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Args = --initialize-at-build-time=com.github.ajalt.mordant.internal.MppImplKt | ||
Args = --initialize-at-build-time=com.github.ajalt.mordant.internal.MppInternal_jvmKt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
mordant/src/macosMain/kotlin/com/github/ajalt/mordant/internal/MppInternal.macos.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.github.ajalt.mordant.internal | ||
|
||
internal actual fun hasFileSystem(): Boolean = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.