Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gradle JDK Automanagement][Intellij plugin] Background tool window #405

Merged
merged 13 commits into from
Sep 11, 2024
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this fixes: #394 I wasn't able to repro the issue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

});

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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {}
});
Comment on lines +111 to +132
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the Gradle JDK setup process would look similar to the Gradle Sync background process. The logs can be seen in Gradle JDK Setup window tab.

Screenshot 2024-09-06 at 12 21 50 PM

}

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the process has failed for any reason, a pop-up will appear:

Screenshot 2024-09-06 at 12 28 17 PM

I could also focus the Gradle JDK setup window tool by default. wdyt ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to focus the window on failure. The notification windows are quite quiet. I for some reason barely ever notice the "required plugins" notification lolol and I think it's quite common.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in: aceac47

.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>