From 990814e2f494e44d51a8d1a2a618e020dd0e796b Mon Sep 17 00:00:00 2001 From: Supratik Das Date: Wed, 10 Jul 2024 22:55:01 +0530 Subject: [PATCH 1/4] Add worker to attempt stopping gradle sync --- .../com/dashwave/plugin/PluginStartup.kt | 58 ++++++++++++++++++- .../com/dashwave/plugin/messages/Messages.kt | 1 + 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt index 28c1a35..a519df1 100644 --- a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt +++ b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt @@ -32,6 +32,10 @@ import com.intellij.openapi.application.PathManager import com.intellij.openapi.components.ServiceManager import io.ktor.util.* import java.awt.Font +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.delay +import kotlinx.coroutines.launch +import javax.swing.SwingUtilities var PluginMode:String = "" var PluginEnv:String = "" @@ -53,11 +57,63 @@ class PluginStartup: StartupActivity { dwWindow.show() dwWindows[project.name] = dwWindow checkDW(project, dwWindow) + +// start terminate gradle sync worker + terminateGradleSync(project?.basePath, dwWindow) PluginMode = pluginMode PluginEnv = pluginEnv } } +fun terminateGradleSync(pwd: String?, dwWindow: DashwaveWindow) { + println("Attempting to terminate Gradle sync...") + + GlobalScope.launch { + var attempt = 0 + val maxAttempts = 5 + val delayTime = 500L // 1 seconds delay + + while (attempt < maxAttempts) { + try { + val processBuilder = ProcessBuilder("./gradlew", "--stop") + if (pwd != null) { + processBuilder.directory(File(pwd)) + } + processBuilder.redirectErrorStream(true) + val process = processBuilder.start() + + val exitCode = process.waitFor() + println("Process exit code: $exitCode") + + SwingUtilities.invokeLater { + if (exitCode == 0) { + println("Rendering Dashwave window to hide sync") + dwWindow.show() +// break + } else { + dwWindow.displayInfo(Messages.GRADLE_SYNC_CANCELLATION_FAILED) + } + } + } catch (e: Exception) { + println("Exception occurred: ${e.message}") + dwWindow.displayInfo("Failed to start the process: ${e.message}") + } + + attempt++ + if (attempt < maxAttempts) { + println("Retrying in $delayTime milliseconds... (Attempt $attempt of $maxAttempts)") + delay(delayTime) + } else { + println("Max retry attempts reached.") + } + } + } + + SwingUtilities.invokeLater { + dwWindow.show() + } +} + fun checkDW(project: Project, dwWindow: DashwaveWindow) { val dwCmd = DwCmds("check-update", project.basePath, true, dwWindow) val exitCode = dwCmd.executeWithExitCode() @@ -173,7 +229,7 @@ fun checkProjectConnected(pwd:String?, dwWindow: DashwaveWindow){ val dd = ReadyForBuildDialog() dd.show() }else { - if(PluginMode == "worksapce"){ + if(PluginMode == "workspace"){ dwWindow.displayError("❌ There is some issue in setting up your project (dashwave.yml doesn't exist), please contact us at hello@dashwave.io") return } diff --git a/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt b/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt index 8e65dda..b161fb3 100644 --- a/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt +++ b/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt @@ -13,5 +13,6 @@ class Messages { val PROJECT_CONNECTION_SUCCESS = "✅ Project is successfully connected to dashwave\n\n" val PROJECT_CONNECTION_FAILED = "❌ Dashwave project creation failed\n" val GIT_NOT_CONFIGURED = "Your local codebase is not currently hosted on a Git repository (GitHub/GitLab). Please ensure your codebase is hosted on Git to use this plugin.\n" + val GRADLE_SYNC_CANCELLATION_FAILED = "Gradle sync needs to cancelled before proceeding. Please stop it manually.\n" } } \ No newline at end of file From bcf52e1e75d32e3d672ba3b57f2fab47e6ccfb2d Mon Sep 17 00:00:00 2001 From: Supratik Das Date: Thu, 11 Jul 2024 08:54:54 +0530 Subject: [PATCH 2/4] Terminate only if plugin mode is workspace --- src/main/kotlin/com/dashwave/plugin/PluginStartup.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt index a519df1..7d2fc24 100644 --- a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt +++ b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt @@ -58,7 +58,10 @@ class PluginStartup: StartupActivity { dwWindows[project.name] = dwWindow checkDW(project, dwWindow) -// start terminate gradle sync worker +// start terminate gradle sync worker if pluginMode is 'workspace' + if(pluginMode == "workspace"){ + terminateGradleSync(project.basePath, dwWindow) + } terminateGradleSync(project?.basePath, dwWindow) PluginMode = pluginMode PluginEnv = pluginEnv From 79b8951569828f387462827af1af871d5f8b44c8 Mon Sep 17 00:00:00 2001 From: Supratik Das Date: Thu, 11 Jul 2024 13:30:42 +0530 Subject: [PATCH 3/4] Remove redundant terminate function --- src/main/kotlin/com/dashwave/plugin/PluginStartup.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt index 7d2fc24..aeb58fe 100644 --- a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt +++ b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt @@ -62,7 +62,6 @@ class PluginStartup: StartupActivity { if(pluginMode == "workspace"){ terminateGradleSync(project.basePath, dwWindow) } - terminateGradleSync(project?.basePath, dwWindow) PluginMode = pluginMode PluginEnv = pluginEnv } From 23a03e0e8e03d3431858b8956207ce326b6f52f3 Mon Sep 17 00:00:00 2001 From: Supratik Das Date: Thu, 11 Jul 2024 14:28:46 +0530 Subject: [PATCH 4/4] Add newline to messages --- src/main/kotlin/com/dashwave/plugin/messages/Messages.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt b/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt index b161fb3..f7368ae 100644 --- a/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt +++ b/src/main/kotlin/com/dashwave/plugin/messages/Messages.kt @@ -15,4 +15,4 @@ class Messages { val GIT_NOT_CONFIGURED = "Your local codebase is not currently hosted on a Git repository (GitHub/GitLab). Please ensure your codebase is hosted on Git to use this plugin.\n" val GRADLE_SYNC_CANCELLATION_FAILED = "Gradle sync needs to cancelled before proceeding. Please stop it manually.\n" } -} \ No newline at end of file +}