From 9748f853fefb74cb7f6ff24e3aaf08f5cbadaad5 Mon Sep 17 00:00:00 2001 From: Michael Rittmeister Date: Thu, 28 Sep 2023 23:19:40 +0200 Subject: [PATCH] No longer bundle Ktor with UWP --- .idea/misc.xml | 2 +- app/desktop/build.gradle.kts | 6 ++- .../commonMain/kotlin/AuthorizationScreen.kt | 17 ++------- app/desktop/src/commonMain/kotlin/Main.kt | 38 ++----------------- .../src/commonMain/kotlin/NativeUtil.kt | 15 -------- app/desktop/src/commonMain/kotlin/Platform.kt | 25 ++++++++++++ .../kotlin/AuthorizationServer.kt | 2 +- .../kotlin/{NativeUtil.kt => Platform.kt} | 10 ++++- .../kotlin/{NativeUtil.kt => Platform.kt} | 28 +++++++++++++- app/desktop/uwp_helper/build.gradle.kts | 2 +- build.gradle.kts | 2 +- 11 files changed, 76 insertions(+), 71 deletions(-) create mode 100644 app/desktop/src/commonMain/kotlin/Platform.kt rename app/desktop/src/{commonMain => nonWindowsMain}/kotlin/AuthorizationServer.kt (97%) rename app/desktop/src/nonWindowsMain/kotlin/{NativeUtil.kt => Platform.kt} (75%) rename app/desktop/src/windowsMain/kotlin/{NativeUtil.kt => Platform.kt} (62%) diff --git a/.idea/misc.xml b/.idea/misc.xml index 915a7a2..2c03559 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -28,7 +28,7 @@ - + \ No newline at end of file diff --git a/app/desktop/build.gradle.kts b/app/desktop/build.gradle.kts index 2142c3f..0a3362a 100644 --- a/app/desktop/build.gradle.kts +++ b/app/desktop/build.gradle.kts @@ -20,6 +20,10 @@ kotlin { } val nonWindowsMain by creating { dependsOn(commonMain.get()) + dependencies { + implementation(libs.ktor.server.netty) + implementation(libs.ktor.server.cors) + } } named("jvmMain") { @@ -34,8 +38,6 @@ kotlin { implementation(project.dependencies.compose.materialIconsExtended) implementation(project.dependencies.compose.material3) implementation(libs.logback) - implementation(libs.ktor.server.netty) - implementation(libs.ktor.server.cors) } } } diff --git a/app/desktop/src/commonMain/kotlin/AuthorizationScreen.kt b/app/desktop/src/commonMain/kotlin/AuthorizationScreen.kt index 2d88a87..c56c3d8 100644 --- a/app/desktop/src/commonMain/kotlin/AuthorizationScreen.kt +++ b/app/desktop/src/commonMain/kotlin/AuthorizationScreen.kt @@ -21,25 +21,14 @@ import dev.schlaubi.tonbrett.app.api.getUrl import dev.schlaubi.tonbrett.client.href import dev.schlaubi.tonbrett.common.Route import io.ktor.http.* -import kotlin.system.exitProcess @Composable -fun AuthorizationScreen(uwp: Boolean, alreadyWaiting: Boolean, onAuth: () -> Unit) { +fun AuthorizationScreen(alreadyWaiting: Boolean, onAuth: () -> Unit) { var waiting by remember { mutableStateOf(alreadyWaiting) } if (waiting) { SideEffect { - val protocol = if (uwp) { - Route.Auth.Type.PROTOCOL - } else { - Route.Auth.Type.APP - } - - launchUri(href(Route.Auth(protocol), URLBuilder(getUrl())).build().toURI()) - if (uwp) { - exitProcess(0) - } else { - startAuthorizationServer(onAuth) - } + launchUri(href(Route.Auth(loginType), URLBuilder(getUrl())).build().toURI()) + prepareAuthentication(onAuth) } } Column( diff --git a/app/desktop/src/commonMain/kotlin/Main.kt b/app/desktop/src/commonMain/kotlin/Main.kt index 88ea376..033ef1b 100644 --- a/app/desktop/src/commonMain/kotlin/Main.kt +++ b/app/desktop/src/commonMain/kotlin/Main.kt @@ -22,42 +22,16 @@ import androidx.compose.ui.window.* import cafe.adriel.lyricist.LocalStrings import dev.schlaubi.tonbrett.app.* import dev.schlaubi.tonbrett.app.api.ProvideContext -import io.ktor.http.* import io.ktor.serialization.* import mu.KotlinLogging import java.net.URI -import kotlin.io.path.absolutePathString import java.awt.Window as AWTWindow private val LOG = KotlinLogging.logger { } -fun main(args: Array) { - val argsString = args.joinToString(" ") - if (argsString.startsWith("tonbrett://login")) { - try { - LOG.info { "Launched App with $argsString saving token now" } - val token = Url(argsString).parameters["token"]!! - setToken(token) - } catch (e: Exception) { - e.printStackTrace() - Thread.sleep(50000) - } - startApplication(isUwp) - } else { - main(reAuthorize = false, uwp = isUwp) { startApplication(isUwp) } - } -} +fun main(array: Array) = start(array) -fun main(reAuthorize: Boolean, uwp: Boolean = false, onAuth: () -> Unit) { - val token = runCatching { getToken() }.getOrNull() - if (reAuthorize || token == null) { - startApplication(uwp, true) - } else { - startApplication(uwp) - } -} - -fun startApplication(uwp: Boolean, forAuth: Boolean = false) = application { +fun startApplication(forAuth: Boolean = false) = application { val sessionExpired = remember { mutableStateOf(false) } var needsUpdate by remember { mutableStateOf(false) } val exceptionHandler = ExceptionHandlerFactory { @@ -84,14 +58,13 @@ fun startApplication(uwp: Boolean, forAuth: Boolean = false) = application { } } } else { - startActualApplication(uwp, forAuth, exceptionHandler, sessionExpired) + startActualApplication(forAuth, exceptionHandler, sessionExpired) } } @OptIn(ExperimentalComposeUiApi::class) @Composable private fun ApplicationScope.startActualApplication( - uwp: Boolean, forAuth: Boolean, exceptionHandler: ExceptionHandlerFactory, sessionExpired: MutableState ) { @@ -110,10 +83,7 @@ private fun ApplicationScope.startActualApplication( } if (needsAuth) { - AuthorizationScreen(uwp, !firstAuth) { - context.resetApi() - needsAuth = false - } + AuthorizationScreen(!firstAuth) { needsAuth = false } } else { context.resetApi() ProvideContext(context) { diff --git a/app/desktop/src/commonMain/kotlin/NativeUtil.kt b/app/desktop/src/commonMain/kotlin/NativeUtil.kt index 825834e..943f32a 100644 --- a/app/desktop/src/commonMain/kotlin/NativeUtil.kt +++ b/app/desktop/src/commonMain/kotlin/NativeUtil.kt @@ -3,18 +3,3 @@ package dev.schlaubi.tonbrett.app.desktop import java.net.URI import java.nio.file.Path -/** - * Whether the current platform is UWP. - */ -expect val isUwp: Boolean - -/** - * Tries to launch the URI using the UWP `Launcher`. - * - * @param uri the [URI] to launch - */ -expect fun launchUri(uri: URI) - -expect fun getToken(): String - -expect fun setToken(token: String) diff --git a/app/desktop/src/commonMain/kotlin/Platform.kt b/app/desktop/src/commonMain/kotlin/Platform.kt new file mode 100644 index 0000000..533c2ef --- /dev/null +++ b/app/desktop/src/commonMain/kotlin/Platform.kt @@ -0,0 +1,25 @@ +@file:JvmName("Platform") +package dev.schlaubi.tonbrett.app.desktop + +import dev.schlaubi.tonbrett.common.Route +import java.net.URI + +expect val loginType: Route.Auth.Type + +/** + * Runs any preparation steps for authentication + */ +expect fun prepareAuthentication(onAuth: () -> Unit) + +expect fun start(args: Array) + +/** + * Tries to launch the URI using the UWP `Launcher`. + * + * @param uri the [URI] to launch + */ +expect fun launchUri(uri: URI) + +expect fun getToken(): String + +expect fun setToken(token: String) \ No newline at end of file diff --git a/app/desktop/src/commonMain/kotlin/AuthorizationServer.kt b/app/desktop/src/nonWindowsMain/kotlin/AuthorizationServer.kt similarity index 97% rename from app/desktop/src/commonMain/kotlin/AuthorizationServer.kt rename to app/desktop/src/nonWindowsMain/kotlin/AuthorizationServer.kt index 4301e71..a2c3261 100644 --- a/app/desktop/src/commonMain/kotlin/AuthorizationServer.kt +++ b/app/desktop/src/nonWindowsMain/kotlin/AuthorizationServer.kt @@ -30,7 +30,7 @@ fun Application.authModule(onAuth: () -> Unit, scope: CoroutineScope) { post("login") { val token = call.parameters["token"] ?: throw BadRequestException("Missing token") - //saveConfig(Config(token)) + saveConfig(Config(token)) call.respond(HttpStatusCode.Accepted) // This stops the server, see stopServerOnCancellation above scope.cancel() diff --git a/app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt b/app/desktop/src/nonWindowsMain/kotlin/Platform.kt similarity index 75% rename from app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt rename to app/desktop/src/nonWindowsMain/kotlin/Platform.kt index fdc28c0..c1ef4ac 100644 --- a/app/desktop/src/nonWindowsMain/kotlin/NativeUtil.kt +++ b/app/desktop/src/nonWindowsMain/kotlin/Platform.kt @@ -1,12 +1,20 @@ package dev.schlaubi.tonbrett.app.desktop +import dev.schlaubi.tonbrett.common.Route import java.awt.Desktop import java.net.URI import java.nio.file.Path import kotlin.io.path.Path import kotlin.io.path.div -actual val isUwp = false +actual val loginType = Route.Auth.Type.APP + +actual fun start(args: Array) { + val needsAuth = runCatching { getToken() }.isFailure + startApplication(needsAuth) +} + +actual fun prepareAuthentication(onAuth: () -> Unit) = startAuthorizationServer(onAuth) actual fun launchUri(uri: URI) { val desktop = Desktop.getDesktop() diff --git a/app/desktop/src/windowsMain/kotlin/NativeUtil.kt b/app/desktop/src/windowsMain/kotlin/Platform.kt similarity index 62% rename from app/desktop/src/windowsMain/kotlin/NativeUtil.kt rename to app/desktop/src/windowsMain/kotlin/Platform.kt index b919158..e7f7e18 100644 --- a/app/desktop/src/windowsMain/kotlin/NativeUtil.kt +++ b/app/desktop/src/windowsMain/kotlin/Platform.kt @@ -2,12 +2,38 @@ package dev.schlaubi.tonbrett.app.desktop import dev.schlaubi.tonbrett.app.desktop.uwp_helper.StringResult import dev.schlaubi.tonbrett.app.desktop.uwp_helper.UwpHelper.* +import dev.schlaubi.tonbrett.common.Route +import io.ktor.http.* +import mu.KotlinLogging import java.lang.foreign.Arena import java.lang.foreign.MemorySegment import java.lang.foreign.SegmentAllocator import java.net.URI +import kotlin.system.exitProcess -actual val isUwp = true +private val LOG = KotlinLogging.logger { } + +actual val loginType = Route.Auth.Type.PROTOCOL + +actual fun prepareAuthentication(onAuth: () -> Unit): Unit = exitProcess(0) + +actual fun start(args: Array) { + val argsString = args.joinToString(" ") + if (argsString.startsWith("tonbrett://login")) { + try { + LOG.info { "Launched App with $argsString saving token now" } + val token = Url(argsString).parameters["token"]!! + setToken(token) + } catch (e: Exception) { + e.printStackTrace() + Thread.sleep(50000) + } + startApplication() + } else { + val needsAuth = runCatching { getToken() }.isFailure + startApplication(needsAuth) + } +} actual fun launchUri(uri: URI): Unit = Arena.openConfined().use { arena -> val url = arena.allocateUtf8String(uri.toString()) diff --git a/app/desktop/uwp_helper/build.gradle.kts b/app/desktop/uwp_helper/build.gradle.kts index 4db2baa..1493b20 100644 --- a/app/desktop/uwp_helper/build.gradle.kts +++ b/app/desktop/uwp_helper/build.gradle.kts @@ -31,7 +31,7 @@ tasks { rootProject.file("jextract-20/bin/jextract.bat").absolutePath } else { if (OSUtils.IS_WINDOWS) { - "C:\\Users\\micha\\.jdks\\jextract-20\\bin\\jextract.bat" + "jextract.bat" } else { "jextract" } diff --git a/build.gradle.kts b/build.gradle.kts index a919736..219b48d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ plugins { allprojects { group = "dev.schlaubi.tonbrett" - version = "1.14.25" + version = "1.14.26" repositories { mavenCentral()