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

fix: Read action when project is imported #1271

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -379,15 +381,20 @@ public void onImportFinished(@Nullable String projectPath) {
// The imported project doesn't belong to the input project, ignore it.
return;
}
List<Module> 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<Module> 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());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@

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;
import com.intellij.openapi.project.ProjectUtil;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand All @@ -105,8 +106,8 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List<V
Set<MavenArtifact> downloaded = new HashSet<>();
Set<MavenId> toDownload = new HashSet<>();

List<MavenArtifact> dependencies = mavenProject.getDependencies();
double counter = 80d /100d / 3d;
List<MavenArtifact> dependencies = mavenProject.getDependencies();
double counter = 80d / 100d / 3d;
double i = counter / dependencies.size();
double p = 0;

Expand All @@ -123,7 +124,7 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List<V
}
}
}
p+=i;
p += i;
progressIndicator.setFraction(p);
}

Expand All @@ -141,7 +142,7 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List<V
toDownload.add(binaryDependency.getMavenId());
}
}
p+=i;
p += i;
progressIndicator.setFraction(p);
}

Expand All @@ -154,7 +155,7 @@ private void getDeploymentFiles(Module module, MavenProject mavenProject, List<V
progressIndicator.checkCanceled();
progressIndicator.setText2("Searching deployment descriptor in '" + sourceDependency.getArtifactId() + "' sources");
processDependency(mavenProject, result, downloaded, sourceDependency, SOURCES);
p+=i;
p += i;
progressIndicator.setFraction(p);
}
}
Expand All @@ -180,7 +181,7 @@ private Set<MavenArtifact> 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 {
Expand All @@ -190,9 +191,9 @@ private Set<MavenArtifact> resolveDeploymentArtifacts(Module module, MavenProjec
progressIndicator.setText2("Resolving " + (shouldResolveArtifactTransitively ? " (Transitevely) " : "") + "'" + deploymentId + "'");
if (shouldResolveArtifactTransitively) {
// Resolving the deployment artifact and their dependencies
List<MavenArtifactInfo> infos = Arrays.asList( new MavenArtifactInfo(deploymentId, "jar", classifier));
List<MavenArtifactInfo> infos = Arrays.asList(new MavenArtifactInfo(deploymentId, "jar", classifier));
List<MavenArtifact> resolvedArtifacts = serverWrapper.resolveArtifactTransitively(infos, mavenProject.getRemoteRepositories()).mavenResolvedArtifacts;
for (var resolvedArtifact: resolvedArtifacts) {
for (var resolvedArtifact : resolvedArtifacts) {
addDeploymentArtifact(resolvedArtifact, deploymentArtifacts);
}
} else {
Expand All @@ -219,7 +220,7 @@ private static void addDeploymentArtifact(MavenArtifact resolvedArtifact, Set<Ma
* This code is using reflection to get the instance of the {@code MavenServerManager} and calls
* {@code createEmbedder} on it (MavenServerManager.getInstance().createEmbedder()).
* The instance that is created is then returned.
*
* <p>
* This code can be removed once the minimum version gets IC-2023.1.
*
* <ul>
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -279,15 +281,19 @@ public void addProjectImportListener(@NotNull Project project, @NotNull MessageB
connection.subscribe(MavenImportListener.TOPIC, new MavenImportListener() {
@Override
public void importFinished(@NotNull Collection<MavenProject> importedProjects, @NotNull List<Module> newModules) {
List<Module> 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<Module> 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());
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ private void tryToCreateRunConfigurations(List<Module> modules) {
if (modules.isEmpty()) {
return;
}
ReadAction.nonBlocking(() -> doTryToCreateRunConfigurations(modules))
.inSmartMode(project)
.submit(NonUrgentExecutor.getInstance());
}

private void doTryToCreateRunConfigurations(List<Module> modules) {
boolean runConfigurationCreated = false;
for (Module module : modules) {
if (tryToCreateRunConfiguration(module)) {
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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)
Expand Down
Loading