|
| 1 | +// Copyright (c) 2024 Gitpod GmbH. All rights reserved. |
| 2 | +// Licensed under the GNU Affero General Public License (AGPL). |
| 3 | +// See License.AGPL.txt in the project root for license information. |
| 4 | + |
| 5 | +package io.gitpod.jetbrains.remote.listeners |
| 6 | + |
| 7 | +import com.intellij.openapi.diagnostic.thisLogger |
| 8 | +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId |
| 9 | +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener |
| 10 | +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter |
| 11 | +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType |
| 12 | +import com.intellij.openapi.progress.ProgressIndicator |
| 13 | +import com.intellij.openapi.progress.ProgressManager |
| 14 | +import com.intellij.openapi.progress.Task |
| 15 | +import kotlinx.coroutines.delay |
| 16 | +import java.util.concurrent.CompletableFuture |
| 17 | +import java.util.concurrent.TimeUnit |
| 18 | + |
| 19 | +class GradleSyncListener : ExternalSystemTaskNotificationListener { |
| 20 | + override fun onStart(id: ExternalSystemTaskId, workingDir: String?) { |
| 21 | + thisLogger().warn("===========hwen.onStart.1 ${id.projectSystemId} ${id.type} ${workingDir}") |
| 22 | + if (id.projectSystemId.toString() != "GRADLE" || id.type != ExternalSystemTaskType.RESOLVE_PROJECT) { |
| 23 | + return |
| 24 | + } |
| 25 | + thisLogger().warn("===========hwen.onStart.2 gradle") |
| 26 | + |
| 27 | + Thread.sleep(30000L) |
| 28 | + thisLogger().warn("===========hwen.onStart.3") |
| 29 | + |
| 30 | + // 使用后台任务执行延迟 |
| 31 | + ProgressManager.getInstance().run( |
| 32 | + object : Task.Backgroundable(null, "Waiting before Gradle sync", true) { |
| 33 | + override fun run(indicator: ProgressIndicator) { |
| 34 | + try { |
| 35 | + indicator.isIndeterminate = false |
| 36 | + |
| 37 | + for (i in 1..10) { |
| 38 | + if (indicator.isCanceled) { |
| 39 | + thisLogger().warn("===========hwen.4 Delay was cancelled by user") |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + indicator.fraction = i / 10.0 |
| 44 | + indicator.text = "Waiting ${10 - i} seconds before Gradle sync..." |
| 45 | + |
| 46 | + CompletableFuture.delayedExecutor(1, TimeUnit.SECONDS) |
| 47 | + .execute {}.run { } |
| 48 | + } |
| 49 | + |
| 50 | + thisLogger().warn("===========hwen.5 Delay completed, continuing with sync") |
| 51 | + } catch (e: Exception) { |
| 52 | + thisLogger().error("===========hwen.6 Error during delay", e) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + override fun onCancel() { |
| 57 | + thisLogger().warn("==============hwen.7 Delay was cancelled") |
| 58 | + super.onCancel() |
| 59 | + } |
| 60 | + } |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
0 commit comments