From da83282ee1eb48f86836a2b92fb99fb3b847065e Mon Sep 17 00:00:00 2001 From: azerr Date: Thu, 23 Nov 2023 16:46:09 +0100 Subject: [PATCH] fix: Read action when project is imported Signed-off-by: azerr --- .../gradle/AbstractGradleToolDelegate.java | 27 ++++++---- .../buildtool/maven/MavenToolDelegate.java | 54 ++++++++++--------- .../run/QuarkusRunConfigurationManager.java | 11 ++-- 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/gradle/AbstractGradleToolDelegate.java b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/gradle/AbstractGradleToolDelegate.java index c5bddb453..2cbbc309d 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/gradle/AbstractGradleToolDelegate.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/gradle/AbstractGradleToolDelegate.java @@ -14,6 +14,7 @@ import com.intellij.execution.RunnerAndConfigurationSettings; import com.intellij.ide.util.newProjectWizard.AddModuleWizard; import com.intellij.openapi.application.ApplicationManager; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.externalSystem.model.settings.ExternalSystemExecutionSettings; import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId; import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType; @@ -39,6 +40,7 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.projectImport.ProjectImportBuilder; import com.intellij.projectImport.ProjectImportProvider; +import com.intellij.util.concurrency.NonUrgentExecutor; import com.intellij.util.messages.MessageBusConnection; import com.redhat.devtools.intellij.quarkus.QuarkusModuleUtil; import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; @@ -48,6 +50,8 @@ import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import org.jetbrains.idea.maven.project.MavenProject; +import org.jetbrains.idea.maven.project.MavenProjectsManager; import org.jetbrains.plugins.gradle.execution.GradleRunnerUtil; import org.jetbrains.plugins.gradle.service.execution.GradleExternalTaskConfigurationType; import org.jetbrains.plugins.gradle.service.execution.GradleRunConfiguration; @@ -379,15 +383,20 @@ public void onImportFinished(@Nullable String projectPath) { // The imported project doesn't belong to the input project, ignore it. return; } - List modules = new ArrayList<>(); - Module[] existingModules = ModuleManager.getInstance(project).getModules(); - for (Module module : existingModules) { - // Check if the module is a Gradle project - if (GradleRunnerUtil.isGradleModule(module) && isValidGradleModule(module)) { - modules.add(module); - } - } - listener.importFinished(modules); + + ReadAction.nonBlocking(() -> { + List modules = new ArrayList<>(); + Module[] existingModules = ModuleManager.getInstance(project).getModules(); + for (Module module : existingModules) { + // Check if the module is a Gradle project + if (GradleRunnerUtil.isGradleModule(module) && isValidGradleModule(module)) { + modules.add(module); + } + } + listener.importFinished(modules); + }) + .inSmartMode(project) + .submit(NonUrgentExecutor.getInstance()); } }); } diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java index eaf8d053f..c45db09b8 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java @@ -12,6 +12,7 @@ import com.intellij.execution.RunManager; import com.intellij.execution.RunnerAndConfigurationSettings; +import com.intellij.openapi.application.ReadAction; import com.intellij.openapi.module.Module; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; @@ -19,11 +20,12 @@ import com.intellij.openapi.roots.ModuleRootManager; import com.intellij.openapi.vfs.VfsUtilCore; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.concurrency.NonUrgentExecutor; import com.intellij.util.messages.MessageBusConnection; import com.redhat.devtools.intellij.quarkus.QuarkusModuleUtil; +import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; import com.redhat.devtools.intellij.quarkus.buildtool.ProjectImportListener; import com.redhat.devtools.intellij.quarkus.run.QuarkusRunConfiguration; -import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.idea.maven.execution.MavenRunConfiguration; @@ -32,12 +34,11 @@ import org.jetbrains.idea.maven.model.MavenArtifact; import org.jetbrains.idea.maven.model.MavenArtifactInfo; import org.jetbrains.idea.maven.model.MavenId; -import org.jetbrains.idea.maven.project.MavenImportListener; import org.jetbrains.idea.maven.project.MavenGeneralSettings; +import org.jetbrains.idea.maven.project.MavenImportListener; import org.jetbrains.idea.maven.project.MavenProject; import org.jetbrains.idea.maven.project.MavenProjectsManager; import org.jetbrains.idea.maven.server.MavenEmbedderWrapper; -import org.jetbrains.idea.maven.server.MavenServerManager; import org.jetbrains.idea.maven.utils.MavenProcessCanceledException; import org.jetbrains.idea.maven.utils.MavenUtil; import org.slf4j.Logger; @@ -77,7 +78,7 @@ public void processImport(Module module) { Project project = module.getProject(); VirtualFile pomFile = null; VirtualFile[] contentRoots = ModuleRootManager.getInstance(module).getContentRoots(); - for(VirtualFile contentRoot : contentRoots) { + for (VirtualFile contentRoot : contentRoots) { VirtualFile child = contentRoot.findChild("pom.xml"); if (child != null) { pomFile = child; @@ -94,7 +95,7 @@ public void processImport(Module module) { if (distributionUrl != null) { String mavenHome = mavenSettings.getMavenHome(); String WRAPPED_MAVEN = "Use Maven wrapper"; //MavenServerManager.WRAPPED_MAVEN was removed in 2023.3 without any deprecation warning! - if (!WRAPPED_MAVEN.equals(mavenHome)){ + if (!WRAPPED_MAVEN.equals(mavenHome)) { mavenSettings.setMavenHome(WRAPPED_MAVEN); } } @@ -105,8 +106,8 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List downloaded = new HashSet<>(); Set toDownload = new HashSet<>(); - List dependencies = mavenProject.getDependencies(); - double counter = 80d /100d / 3d; + List dependencies = mavenProject.getDependencies(); + double counter = 80d / 100d / 3d; double i = counter / dependencies.size(); double p = 0; @@ -123,7 +124,7 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List resolveDeploymentArtifacts(Module module, MavenProjec return Collections.emptySet(); } if (classifier != null) { - for(MavenId id : deploymentIds) { + for (MavenId id : deploymentIds) { deploymentArtifacts.add(serverWrapper.resolve(new MavenArtifactInfo(id, "jar", classifier), mavenProject.getRemoteRepositories())); } } else { @@ -190,9 +191,9 @@ private Set resolveDeploymentArtifacts(Module module, MavenProjec progressIndicator.setText2("Resolving " + (shouldResolveArtifactTransitively ? " (Transitevely) " : "") + "'" + deploymentId + "'"); if (shouldResolveArtifactTransitively) { // Resolving the deployment artifact and their dependencies - List infos = Arrays.asList( new MavenArtifactInfo(deploymentId, "jar", classifier)); + List infos = Arrays.asList(new MavenArtifactInfo(deploymentId, "jar", classifier)); List resolvedArtifacts = serverWrapper.resolveArtifactTransitively(infos, mavenProject.getRemoteRepositories()).mavenResolvedArtifacts; - for (var resolvedArtifact: resolvedArtifacts) { + for (var resolvedArtifact : resolvedArtifacts) { addDeploymentArtifact(resolvedArtifact, deploymentArtifacts); } } else { @@ -219,7 +220,7 @@ private static void addDeploymentArtifact(MavenArtifact resolvedArtifact, Set * This code can be removed once the minimum version gets IC-2023.1. * *
    @@ -252,7 +253,8 @@ private MavenEmbedderWrapper createEmbedderWrapper(Project project, String worki false, workingDirectory, workingDirectory); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | + InvocationTargetException e) { throw new RuntimeException(e); } } @@ -279,15 +281,19 @@ public void addProjectImportListener(@NotNull Project project, @NotNull MessageB connection.subscribe(MavenImportListener.TOPIC, new MavenImportListener() { @Override public void importFinished(@NotNull Collection importedProjects, @NotNull List newModules) { - List modules = new ArrayList<>(); - for (MavenProject mavenProject : importedProjects) { - MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(project); - Module module = projectsManager.findModule(mavenProject); - if (module != null) { - modules.add(module); - } - } - listener.importFinished(modules); + ReadAction.nonBlocking(() -> { + List modules = new ArrayList<>(); + for (MavenProject mavenProject : importedProjects) { + MavenProjectsManager projectsManager = MavenProjectsManager.getInstance(project); + Module module = projectsManager.findModule(mavenProject); + if (module != null) { + modules.add(module); + } + } + listener.importFinished(modules); + }) + .inSmartMode(project) + .submit(NonUrgentExecutor.getInstance()); } }); 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 index 660970d40..fe5452c70 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfigurationManager.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfigurationManager.java @@ -114,12 +114,7 @@ private void tryToCreateRunConfigurations(List modules) { if (modules.isEmpty()) { return; } - ReadAction.nonBlocking(() -> doTryToCreateRunConfigurations(modules)) - .inSmartMode(project) - .submit(NonUrgentExecutor.getInstance()); - } - - private void doTryToCreateRunConfigurations(List modules) { + boolean runConfigurationCreated = false; for (Module module : modules) { if (tryToCreateRunConfiguration(module)) { @@ -175,6 +170,7 @@ private synchronized boolean createRunConfigurationIfNeeded(Module module) { /** * Add "QuarkusRunConfigurationType" in the Services view settings to show "Quarkus Dev Mode" and their Quarkus run configuration in the Services view. + * * @param logError true if error must be logged while updating Services view settings. * @return true if the update is done correctly and false otherwise. */ @@ -187,8 +183,7 @@ private boolean addQuarkusRunConfigurationTypeInServicesViewIfNeeded(boolean log runDashboardManager.setTypes(types); } return true; - } - catch(Exception e) { + } catch (Exception e) { // This case comes from when Ultimate is used and Ultimate Quarkus support update in same time their Quarkus Configuration Type. // java.util.ConcurrentModificationException // at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1597)