From a5f8fa453b4e85ec6e2317899816bb09b310df6e Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Wed, 30 Aug 2023 22:28:06 +0200 Subject: [PATCH] Don't run jextract on non-Windows --- .github/workflows/ci.yml | 10 ----- app/desktop/build.gradle.kts | 39 ++++++++++++++---- .../kotlin/AuthorizationServer.kt | 0 .../kotlin/ConfigBasedAppContext.kt | 0 .../{main => commonMain}/kotlin/ConfigFile.kt | 0 .../src/{main => commonMain}/kotlin/Main.kt | 4 +- .../src/commonMain/kotlin/NativeUtil.kt | 17 ++++++++ .../resources/logback.xml | 0 .../{main => commonMain}/resources/logo.png | Bin app/desktop/src/main/kotlin/NativeUtil.kt | 37 ----------------- app/desktop/src/main/kotlin/URIUtil.kt | 17 -------- .../src/nonWindowsMain/kotlin/NativeUtil.kt | 16 +++++++ .../src/windowsMain/kotlin/NativeUtil.kt | 27 ++++++++++++ 13 files changed, 93 insertions(+), 74 deletions(-) rename app/desktop/src/{main => commonMain}/kotlin/AuthorizationServer.kt (100%) rename app/desktop/src/{main => commonMain}/kotlin/ConfigBasedAppContext.kt (100%) rename app/desktop/src/{main => commonMain}/kotlin/ConfigFile.kt (100%) rename app/desktop/src/{main => commonMain}/kotlin/Main.kt (97%) create mode 100644 app/desktop/src/commonMain/kotlin/NativeUtil.kt rename app/desktop/src/{main => commonMain}/resources/logback.xml (100%) rename app/desktop/src/{main => commonMain}/resources/logo.png (100%) delete mode 100644 app/desktop/src/main/kotlin/NativeUtil.kt delete mode 100644 app/desktop/src/main/kotlin/URIUtil.kt create mode 100644 app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt create mode 100644 app/desktop/src/windowsMain/kotlin/NativeUtil.kt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ca967b3..97d3072 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -55,16 +55,6 @@ jobs: run: | Invoke-WebRequest https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_windows-x64_bin.tar.gz -OutFile jextract.tar.gz - tar xzvf jextract.tar.gz - - name: Setup jextract - if: matrix.os != 'windows-latest' - run: | - if [[ "${{ matrix.os }}" == "macos-latest" ]]; then - curl -L https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_macos-x64_bin.tar.gz -o jextract.tar.gz - else - curl -L https://download.java.net/java/early_access/jextract/1/openjdk-20-jextract+1-2_linux-x64_bin.tar.gz -o jextract.tar.gz - fi - tar xzvf jextract.tar.gz - name: Setup MacOS signing if: matrix.os == 'macos-latest' diff --git a/app/desktop/build.gradle.kts b/app/desktop/build.gradle.kts index 99b38f6..7baba06 100644 --- a/app/desktop/build.gradle.kts +++ b/app/desktop/build.gradle.kts @@ -2,20 +2,43 @@ import org.jetbrains.compose.desktop.application.dsl.TargetFormat import org.jetbrains.kotlin.org.jline.utils.OSUtils plugins { - kotlin("jvm") + kotlin("multiplatform") kotlin("plugin.serialization") id("org.jetbrains.compose") } -dependencies { - implementation(projects.app.shared) - implementation(projects.app.desktop.uwpHelper) - implementation(compose.desktop.currentOs) - implementation(libs.logback) - implementation(libs.ktor.server.netty) - implementation(libs.ktor.server.cors) +kotlin { + jvm() + + sourceSets { + val windowsMain by creating { + dependsOn(commonMain.get()) + dependencies { + implementation(projects.app.desktop.uwpHelper) + } + } + val nonWindowsMain by creating { + dependsOn(commonMain.get()) + } + + named("jvmMain") { + if (OSUtils.IS_WINDOWS) { + dependsOn(windowsMain) + } else { + dependsOn(nonWindowsMain) + } + dependencies { + implementation(projects.app.shared) + implementation(project.dependencies.compose.desktop.currentOs) + implementation(libs.logback) + implementation(libs.ktor.server.netty) + implementation(libs.ktor.server.cors) + } + } + } } + compose.desktop { application { mainClass = "dev.schlaubi.tonbrett.app.desktop.MainKt" diff --git a/app/desktop/src/main/kotlin/AuthorizationServer.kt b/app/desktop/src/commonMain/kotlin/AuthorizationServer.kt similarity index 100% rename from app/desktop/src/main/kotlin/AuthorizationServer.kt rename to app/desktop/src/commonMain/kotlin/AuthorizationServer.kt diff --git a/app/desktop/src/main/kotlin/ConfigBasedAppContext.kt b/app/desktop/src/commonMain/kotlin/ConfigBasedAppContext.kt similarity index 100% rename from app/desktop/src/main/kotlin/ConfigBasedAppContext.kt rename to app/desktop/src/commonMain/kotlin/ConfigBasedAppContext.kt diff --git a/app/desktop/src/main/kotlin/ConfigFile.kt b/app/desktop/src/commonMain/kotlin/ConfigFile.kt similarity index 100% rename from app/desktop/src/main/kotlin/ConfigFile.kt rename to app/desktop/src/commonMain/kotlin/ConfigFile.kt diff --git a/app/desktop/src/main/kotlin/Main.kt b/app/desktop/src/commonMain/kotlin/Main.kt similarity index 97% rename from app/desktop/src/main/kotlin/Main.kt rename to app/desktop/src/commonMain/kotlin/Main.kt index dd3d95c..b663c3a 100644 --- a/app/desktop/src/main/kotlin/Main.kt +++ b/app/desktop/src/commonMain/kotlin/Main.kt @@ -67,7 +67,7 @@ fun main(reAuthorize: Boolean, uwp: Boolean = false, onAuth: () -> Unit) { } else { Route.Auth.Type.APP } - browseUrl(href(Route.Auth(protocol), URLBuilder(getUrl())).build().toURI()) + launchUri(href(Route.Auth(protocol), URLBuilder(getUrl())).build().toURI()) if (!uwp) { startAuthorizationServer(reAuthorize, onAuth) } else { @@ -96,7 +96,7 @@ fun startApplication(uwp: Boolean) = application { ) { Text(strings.needsUpdate, color = ColorScheme.textColor) Button({ - browseUrl(URI("https://github.com/DRSchlaubi/tonbrett/releases/latest")) + launchUri(URI("https://github.com/DRSchlaubi/tonbrett/releases/latest")) exitApplication() }) { Icon(Icons.Default.Refresh, null) diff --git a/app/desktop/src/commonMain/kotlin/NativeUtil.kt b/app/desktop/src/commonMain/kotlin/NativeUtil.kt new file mode 100644 index 0000000..da47367 --- /dev/null +++ b/app/desktop/src/commonMain/kotlin/NativeUtil.kt @@ -0,0 +1,17 @@ +package dev.schlaubi.tonbrett.app.desktop + +import java.net.URI + +/** + * Tries to launch the URI using the UWP `Launcher`. + * + * @param uri the [URI] to launch + */ +expect fun launchUri(uri: URI) + +/** + * Tries to retrieve the current UWP app data folder. + * + * @return the absolute path to the folder + */ +expect fun getAppDataRoaming(): String diff --git a/app/desktop/src/main/resources/logback.xml b/app/desktop/src/commonMain/resources/logback.xml similarity index 100% rename from app/desktop/src/main/resources/logback.xml rename to app/desktop/src/commonMain/resources/logback.xml diff --git a/app/desktop/src/main/resources/logo.png b/app/desktop/src/commonMain/resources/logo.png similarity index 100% rename from app/desktop/src/main/resources/logo.png rename to app/desktop/src/commonMain/resources/logo.png diff --git a/app/desktop/src/main/kotlin/NativeUtil.kt b/app/desktop/src/main/kotlin/NativeUtil.kt deleted file mode 100644 index ae5301e..0000000 --- a/app/desktop/src/main/kotlin/NativeUtil.kt +++ /dev/null @@ -1,37 +0,0 @@ -package dev.schlaubi.tonbrett.app.desktop - -import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult.`is_error$get` -import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult.`length$get` -import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.* -import java.lang.foreign.Arena -import java.net.URI - -/** - * Tries to launch the URI using the UWP `Launcher`. - * - * @param uri the [URI] to launch - */ -fun launchUri(uri: URI) = Arena.openConfined().use { arena -> - val url = arena.allocateUtf8String(uri.toString()) - launch_uri(url) -} - -/** - * Tries to retrieve the current UWP app data folder. - * - * @return the absolute path to the folder - */ -fun getAppDataRoaming() = Arena.openConfined().use { arena -> - val result = get_app_data_roaming(arena) - val isError = `is_error$get`(result) - val length = `length$get`(result).coerceAtLeast(0) - val buffer = arena.allocateArray(uint16_t, length) - copy_string_from_get_app_data_roaming_result_into_buffer(result, buffer) - val shortArray = buffer.toArray(uint16_t) - val charArray = CharArray(shortArray.size) - for ((index, short) in shortArray.withIndex()) { - charArray[index] = short.toInt().toChar() - } - val string = String(charArray) - if (isError) throw Exception(string) else string -} diff --git a/app/desktop/src/main/kotlin/URIUtil.kt b/app/desktop/src/main/kotlin/URIUtil.kt deleted file mode 100644 index c581db6..0000000 --- a/app/desktop/src/main/kotlin/URIUtil.kt +++ /dev/null @@ -1,17 +0,0 @@ -package dev.schlaubi.tonbrett.app.desktop - -import java.awt.Desktop -import java.net.URI - -fun browseUrl(url: URI) { - if (System.getProperty("os.name").contains("windows", ignoreCase = true) && windowsAppDataFolder != null) { - launchUri(url) - } else { - val desktop = Desktop.getDesktop() - if (desktop.isSupported(Desktop.Action.BROWSE)) { - desktop.browse(url) - } else { - Runtime.getRuntime().exec(arrayOf("xdg-open", url.toString())) - } - } -} diff --git a/app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt b/app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt new file mode 100644 index 0000000..a2486ef --- /dev/null +++ b/app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt @@ -0,0 +1,16 @@ +package dev.schlaubi.tonbrett.app.desktop + +import java.awt.Desktop +import java.net.URI + +actual fun launchUri(uri: URI) { + val desktop = Desktop.getDesktop() + if (desktop.isSupported(Desktop.Action.BROWSE)) { + desktop.browse(uri) + } else { + Runtime.getRuntime().exec(arrayOf("xdg-open", uri.toString())) + } +} + +actual fun getAppDataRoaming(): String = + throw UnsupportedOperationException("This function is only supported on Windows") diff --git a/app/desktop/src/windowsMain/kotlin/NativeUtil.kt b/app/desktop/src/windowsMain/kotlin/NativeUtil.kt new file mode 100644 index 0000000..f1d35b1 --- /dev/null +++ b/app/desktop/src/windowsMain/kotlin/NativeUtil.kt @@ -0,0 +1,27 @@ +package dev.schlaubi.tonbrett.app.desktop + +import dev.schlaubi.tonbrett.app.desktop.uwp_helper.AppDataRoamingResult +import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.* +import java.lang.foreign.Arena +import java.net.URI + +actual fun launchUri(uri: URI): Unit = Arena.openConfined().use { arena -> + val url = arena.allocateUtf8String(uri.toString()) + launch_uri(url) +} + +actual fun getAppDataRoaming(): String = + Arena.openConfined().use { arena -> + val result = get_app_data_roaming(arena) + val isError = AppDataRoamingResult.`is_error$get`(result) + val length = AppDataRoamingResult.`length$get`(result).coerceAtLeast(0) + val buffer = arena.allocateArray(uint16_t, length) + copy_string_from_get_app_data_roaming_result_into_buffer(result, buffer) + val shortArray = buffer.toArray(uint16_t) + val charArray = CharArray(shortArray.size) + for ((index, short) in shortArray.withIndex()) { + charArray[index] = short.toInt().toChar() + } + val string = String(charArray) + if (isError) throw Exception(string) else string + }