diff --git a/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt b/src/main/kotlin/com/dashwave/plugin/PluginStartup.kt index 28c1a35..aeb58fe 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,65 @@ class PluginStartup: StartupActivity { dwWindow.show() dwWindows[project.name] = dwWindow checkDW(project, dwWindow) + +// start terminate gradle sync worker if pluginMode is 'workspace' + if(pluginMode == "workspace"){ + 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 +231,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..f7368ae 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 +}