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..1c2d20794 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.QuarkusRunConfigurationManager; import org.jetbrains.annotations.NotNull; public class QuarkusPostStartupActivity implements StartupActivity, DumbAware { @Override public void runActivity(@NotNull Project project) { + QuarkusRunConfigurationManager.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/QuarkusRunConfigurationManager.java b/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfigurationManager.java new file mode 100644 index 000000000..39e934985 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfigurationManager.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * 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.microprofile.psi.quarkus.PsiQuarkusUtils; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.idea.maven.MavenDisposable; +import org.jetbrains.idea.maven.project.MavenImportListener; +import org.jetbrains.idea.maven.project.MavenProject; +import org.jetbrains.idea.maven.project.MavenProjectsManager; + +import java.util.Collection; +import java.util.List; + +/** + * Quarkus run configuration manager used to: + * + * + */ +public class QuarkusRunConfigurationManager implements Disposable { + + public static QuarkusRunConfigurationManager getInstance(Project project) { + return ServiceManager.getService(project, QuarkusRunConfigurationManager.class); + } + + private final Project project; + private final MessageBusConnection projectConnection; + + public QuarkusRunConfigurationManager(Project project) { + this.project = project; + projectConnection = initializeAutoCreateRunConfigurationWhileImporting(project); + } + + public @Nullable RunnerAndConfigurationSettings findExistingConfigurationFor(@NotNull Module module) { + List configurations = RunManager.getInstance(project).getConfigurationSettingsList(QuarkusRunConfigurationType.class); + if (!configurations.isEmpty()) { + for (RunnerAndConfigurationSettings settings : configurations) { + QuarkusRunConfiguration configuration = (QuarkusRunConfiguration) settings.getConfiguration(); + if (module.equals(configuration.getModule())) { + return settings; + } + } + } + return null; + } + + public @NotNull RunnerAndConfigurationSettings createConfiguration(@NotNull Module module, boolean save) { + var runManager = RunManager.getInstance(module.getProject()); + RunnerAndConfigurationSettings quarkusSettings = runManager.createConfiguration(module.getName(), QuarkusRunConfigurationType.class); + ((QuarkusRunConfiguration) quarkusSettings.getConfiguration()).setModule(module); + if (save) { + quarkusSettings.storeInLocalWorkspace(); + // Save the configuration + runManager.addConfiguration(quarkusSettings); + } + return quarkusSettings; + } + + @NotNull + private MessageBusConnection initializeAutoCreateRunConfigurationWhileImporting(Project project) { + MessageBusConnection projectConnection = project.getMessageBus().connect(MavenDisposable.getInstance(project)); + autoCreateWithMaven(project, projectConnection); + autoCreateWithGradle(project, projectConnection); + return projectConnection; + } + + private void autoCreateWithMaven(Project project, MessageBusConnection projectConnection) { + 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 (MavenProject mavenProject : importedProjects) { + MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(project); + Module module = projectsManager.findModule(mavenProject); + tryToCreateRunConfiguration(module); + } + } + }); + } + + private void autoCreateWithGradle(Project project, MessageBusConnection projectConnection) { + // TODO : track the Gradle import projects + } + + private void tryToCreateRunConfiguration(Module module) { + if (!PsiQuarkusUtils.isQuarkusProject(module)) { + return; + } + // Find existing Quarkus run configuration + RunnerAndConfigurationSettings quarkusSettings = findExistingConfigurationFor(module); + if (quarkusSettings == null) { + // No Quarkus run configuration for the module, create it and save it in the .idea/workspace.xml file + createConfiguration(module, true); + } + } + + @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..ae4b81bbf 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -338,6 +338,7 @@ +