Skip to content

Commit

Permalink
[Gradle JDK Automanagement][Intellij plugin] Background tool window (#…
Browse files Browse the repository at this point in the history
…405)

[Gradle JDK Automanagement][Intellij plugin] Background tool window
  • Loading branch information
crogoz authored Sep 11, 2024
1 parent 01d35f8 commit 9dbc638
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-379.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: '[Intellij] Focus on `Gradle Jdk` Tool Window while the setup is running'
links:
- https://github.com/palantir/gradle-jdks/pull/379
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationEvent;
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener;
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.gradle.util.GradleConstants;

Expand All @@ -30,7 +31,10 @@ public void onStart(@NotNull ExternalSystemTaskId id, String _workingDir) {
if (id.getProjectSystemId().equals(GradleConstants.SYSTEM_ID)
&& (id.getType() == ExternalSystemTaskType.RESOLVE_PROJECT
|| id.getType() == ExternalSystemTaskType.EXECUTE_TASK)) {
id.findProject().getService(GradleJdksProjectService.class).maybeSetupGradleJdks();
Project project = id.findProject();
if (project != null) {
project.getService(GradleJdksProjectService.class).maybeSetupGradleJdks();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@
import com.intellij.execution.process.ProcessTerminatedListener;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.platform.ide.progress.TasksKt;
import com.intellij.platform.util.progress.StepsKt;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import java.io.File;
Expand All @@ -42,6 +48,9 @@
import java.util.Optional;
import java.util.Properties;
import java.util.function.Supplier;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.coroutines.EmptyCoroutineContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.gradle.settings.GradleProjectSettings;
import org.jetbrains.plugins.gradle.settings.GradleSettings;
Expand Down Expand Up @@ -72,13 +81,22 @@ private ConsoleView initConsoleView() {
ContentFactory contentFactory = ContentFactory.getInstance();
Content content = contentFactory.createContent(newConsoleView.getComponent(), "", false);
toolWindow.getContentManager().addContent(content);
// TODO(crogoz): Focus only when error or takes a long time?
toolWindow.activate(null);
Disposer.register(project, newConsoleView);
});

return newConsoleView;
}

public void focusOnWindow() {
ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
toolWindowManager.invokeLater(() -> {
ToolWindow toolWindow = toolWindowManager.getToolWindow(TOOL_WINDOW_NAME);
if (toolWindow != null) {
toolWindow.activate(null, true, false);
}
});
}

public void maybeSetupGradleJdks() {
if (project.getBasePath() == null) {
logger.warn("Skipping setupGradleJdks because project path is null");
Expand All @@ -90,7 +108,28 @@ public void maybeSetupGradleJdks() {
"Skipping setupGradleJdks because gradle JDK setup is not found %s", gradleSetupScript));
return;
}
setupGradleJdks();
TasksKt.withBackgroundProgress(
project,
"Gradle JDK Setup",
(_coroutineScope, continuation) -> {
StepsKt.withProgressText(
"`Gradle JDK Setup` is running. Logs in the `Gradle JDK Setup` window ...",
(_cor, conti) -> {
setupGradleJdks();
return conti;
},
continuation);
return continuation;
},
new Continuation<>() {
@Override
public @NotNull CoroutineContext getContext() {
return EmptyCoroutineContext.INSTANCE;
}

@Override
public void resumeWith(@NotNull Object _object) {}
});
}

private void setupGradleJdks() {
Expand All @@ -101,6 +140,7 @@ private void setupGradleJdks() {
OSProcessHandler handler = new OSProcessHandler(cli);
handler.startNotify();
handler.addProcessListener(new ProcessListener() {

@Override
public void processTerminated(@NotNull ProcessEvent _event) {
updateGradleJvm();
Expand All @@ -109,6 +149,16 @@ public void processTerminated(@NotNull ProcessEvent _event) {
consoleView.get().attachToProcess(handler);
ProcessTerminatedListener.attach(handler, project, "Gradle JDK setup finished with exit code $EXIT_CODE$");
handler.waitFor();
if (handler.getExitCode() != 0) {
Notification notification = NotificationGroupManager.getInstance()
.getNotificationGroup("Gradle JDK setup Notifications")
.createNotification(
"Gradle JDK setup",
String.format("Gradle JDK setup failed with exit code %s", handler.getExitCode()),
NotificationType.ERROR);
notification.notify(project);
focusOnWindow();
}
} catch (ExecutionException e) {
throw new RuntimeException("Failed to setup Gradle JDKs for Intellij", e);
}
Expand All @@ -126,6 +176,7 @@ private void updateGradleJvm() {
"Skipping gradleJvm Configuration because no value was configured in"
+ " `.gradle/config.properties`",
ConsoleViewContentType.LOG_INFO_OUTPUT);

continue;
}

Expand Down
3 changes: 3 additions & 0 deletions idea-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@

<extensions defaultExtensionNs="com.intellij">
<externalSystemTaskNotificationListener implementation="com.palantir.gradle.jdks.GradleJdksExternalSystemTaskNotificationListener"/>
<notificationGroup id="Gradle JDK setup Notifications"
displayType="BALLOON"
key="notification.group.gradleJdkSetup"/>
</extensions>
</idea-plugin>

0 comments on commit 9dbc638

Please sign in to comment.