From 5a3d28358accc8ecff2ed058191dd6bb674b7ed9 Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 9 Nov 2023 16:46:25 +0100 Subject: [PATCH] feat: Auto create Quarkus run config while importing project Fixes #1233 Signed-off-by: azerr --- .../quarkus/QuarkusPostStartupActivity.java | 2 + ...oCreateQuarkusRunConfigurationManager.java | 79 +++++++++++++++++++ src/main/resources/META-INF/plugin.xml | 1 + 3 files changed, 82 insertions(+) create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/run/AutoCreateQuarkusRunConfigurationManager.java diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java index e4719bace..800e22b50 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusPostStartupActivity.java @@ -15,11 +15,13 @@ import com.intellij.openapi.startup.StartupActivity; import com.redhat.devtools.intellij.lsp4mp4ij.classpath.ClasspathResourceChangedManager; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project.PsiMicroProfileProjectManager; +import com.redhat.devtools.intellij.quarkus.run.AutoCreateQuarkusRunConfigurationManager; import org.jetbrains.annotations.NotNull; public class QuarkusPostStartupActivity implements StartupActivity, DumbAware { @Override public void runActivity(@NotNull Project project) { + AutoCreateQuarkusRunConfigurationManager.getInstance(project); ClasspathResourceChangedManager.getInstance(project); // Force the instantiation of the manager to be sure that classpath listener // are registered before QuarkusLanguageClient classpath listener diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/run/AutoCreateQuarkusRunConfigurationManager.java b/src/main/java/com/redhat/devtools/intellij/quarkus/run/AutoCreateQuarkusRunConfigurationManager.java new file mode 100644 index 000000000..a8f490af5 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/run/AutoCreateQuarkusRunConfigurationManager.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.run; + +import com.intellij.execution.RunManager; +import com.intellij.execution.RunnerAndConfigurationSettings; +import com.intellij.openapi.Disposable; +import com.intellij.openapi.components.ServiceManager; +import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; +import com.intellij.util.messages.MessageBusConnection; +import com.redhat.devtools.intellij.lsp4mp4ij.classpath.ClasspathResourceChangedManager; +import com.redhat.microprofile.psi.quarkus.PsiQuarkusUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.idea.maven.project.MavenImportListener; +import org.jetbrains.idea.maven.project.MavenProject; + +import java.util.Collection; +import java.util.List; + +public class AutoCreateQuarkusRunConfigurationManager implements Disposable { + + public static AutoCreateQuarkusRunConfigurationManager getInstance(Project project) { + return ServiceManager.getService(project, AutoCreateQuarkusRunConfigurationManager.class); + } + + private final Project project; + private final MessageBusConnection projectConnection; + + public AutoCreateQuarkusRunConfigurationManager(Project project) { + this.project = project; + projectConnection = project.getMessageBus().connect(); + projectConnection.subscribe(MavenImportListener.TOPIC, new MavenImportListener() { + @Override + public void importFinished(@NotNull Collection importedProjects, @NotNull List newModules) { + // TODO manage a settings to auto create Quarkus run config + for (Module module : newModules) { + if (PsiQuarkusUtils.isQuarkusProject(module)) { + Project project = module.getProject(); + // Find existing Quarkus run configuration + RunnerAndConfigurationSettings quarkusSettings = null; + List list = RunManager.getInstance(project).getConfigurationSettingsList(QuarkusRunConfigurationType.class); + if (!list.isEmpty()) { + for (RunnerAndConfigurationSettings settings : list) { + QuarkusRunConfiguration configuration = (QuarkusRunConfiguration) settings.getConfiguration(); + if (module.equals(configuration.getModule())) { + quarkusSettings = settings; + break; + } + } + } + if (quarkusSettings == null) { + // No Quarkus run configuration for the module, create it and save it in the .idea/workspace.xml file + quarkusSettings = RunManager.getInstance(project).createConfiguration(module.getName(), QuarkusRunConfigurationType.class); + ((QuarkusRunConfiguration) quarkusSettings.getConfiguration()).setModule(module); + quarkusSettings.storeInLocalWorkspace(); + } + } + } + } + }); + } + + @Override + public void dispose() { + projectConnection.disconnect(); + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index d62a9349d..0da6789ea 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -338,6 +338,7 @@ +