From 2642ff56aa5dc715567a5db803dfaf8c3eb314af Mon Sep 17 00:00:00 2001
From: Yiqun Zhang <guiyanakuang@gmail.com>
Date: Thu, 18 Apr 2024 21:32:01 +0800
Subject: [PATCH] :bug: Fix the problem that chrome-driver
 chrome-headless-shell does not have executable permissions in the packaged
 application package (#759)

---
 composeApp/build.gradle.kts                   |  3 ++
 .../clipevery/clip/DesktopChromeService.kt    | 37 ++++++++++++++-----
 .../desktopMain/kotlin/com/clipevery/main.kt  | 21 ++++++-----
 3 files changed, 42 insertions(+), 19 deletions(-)

diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts
index 4bb3a79d0..5513699ae 100644
--- a/composeApp/build.gradle.kts
+++ b/composeApp/build.gradle.kts
@@ -123,6 +123,9 @@ compose.desktop {
             packageName = "Clipevery"
             packageVersion = "1.0.0"
 
+            // If we want to use arthas attach application in production environment,
+            // we need to use
+            // includeAllModules = true
             modules("jdk.charsets")
 
             val properties = Properties()
diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopChromeService.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopChromeService.kt
index 8a4cb74f1..1d4b62f2b 100644
--- a/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopChromeService.kt
+++ b/composeApp/src/desktopMain/kotlin/com/clipevery/clip/DesktopChromeService.kt
@@ -11,6 +11,7 @@ import org.openqa.selenium.Dimension
 import org.openqa.selenium.OutputType
 import org.openqa.selenium.chrome.ChromeDriver
 import org.openqa.selenium.chrome.ChromeOptions
+import java.io.File
 import java.nio.file.Path
 import kotlin.io.path.absolutePathString
 import kotlin.math.max
@@ -50,19 +51,35 @@ object DesktopChromeService : ChromeService {
         }
 
     private val initChromeDriver: (String, String, String, Path) -> Unit = { chromeSuffix, driverName, headlessName, resourcesPath ->
+        val chromeDriverFile =
+            File(
+                resourcesPath
+                    .resolve("$CHROME_DRIVER-$chromeSuffix")
+                    .resolve(driverName)
+                    .absolutePathString(),
+            )
+
+        val chromeHeadlessShellFile =
+            File(
+                resourcesPath
+                    .resolve("$CHROME_HEADLESS_SHELL-$chromeSuffix")
+                    .resolve(headlessName)
+                    .absolutePathString(),
+            )
+
+        if (!chromeDriverFile.canExecute()) {
+            chromeDriverFile.setExecutable(true)
+        }
+
+        if (!chromeHeadlessShellFile.canExecute()) {
+            chromeHeadlessShellFile.setExecutable(true)
+        }
+
         System.setProperty(
             "webdriver.chrome.driver",
-            resourcesPath
-                .resolve("$CHROME_DRIVER-$chromeSuffix")
-                .resolve(driverName)
-                .absolutePathString(),
-        )
-        options.setBinary(
-            resourcesPath
-                .resolve("$CHROME_HEADLESS_SHELL-$chromeSuffix")
-                .resolve(headlessName)
-                .absolutePathString(),
+            chromeDriverFile.absolutePath,
         )
+        options.setBinary(chromeHeadlessShellFile.absolutePath)
     }
 
     private val windowDimension: Dimension =
diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt
index 66a99bcb7..ec6b662fb 100644
--- a/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt
+++ b/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt
@@ -59,21 +59,24 @@ fun main() {
     val logger = KotlinLogging.logger {}
 
     logger.info { "Starting Clipevery" }
+    try {
+        val koinApplication = Dependencies.koinApplication
 
-    val koinApplication = Dependencies.koinApplication
-
-    initInject(koinApplication)
+        initInject(koinApplication)
 
-    val clipboardService = koinApplication.koin.get<ClipboardService>()
-    clipboardService.start()
+        val clipboardService = koinApplication.koin.get<ClipboardService>()
+        clipboardService.start()
 
-    val cleanClipScheduler = koinApplication.koin.get<CleanClipScheduler>()
-    cleanClipScheduler.start()
+        val cleanClipScheduler = koinApplication.koin.get<CleanClipScheduler>()
+        cleanClipScheduler.start()
+    } catch (throwable: Throwable) {
+        logger.error(throwable) { "cant start clipevery" }
+    }
 
     application {
         val ioScope = rememberCoroutineScope { ioDispatcher }
 
-        val appUI = koinApplication.koin.get<AppUI>()
+        val appUI = Dependencies.koinApplication.koin.get<AppUI>()
 
         val trayIcon =
             if (currentPlatform().isMacos()) {
@@ -129,7 +132,7 @@ fun main() {
                 )
             }
             ClipeveryApp(
-                koinApplication,
+                Dependencies.koinApplication,
                 hideWindow = { appUI.showMainWindow = false },
                 exitApplication = exitApplication,
             )