Skip to content

Commit

Permalink
Import projects by configurations
Browse files Browse the repository at this point in the history
- Group the configuration files by file names and import them
  group by group.

Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo committed Oct 23, 2023
1 parent 8b2f626 commit c971e49
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,23 @@ protected void importProjectsFromConfigurationFiles(Collection<IPath> rootPaths,
MultiStatus importStatusCollection = new MultiStatus(IConstants.PLUGIN_ID, -1, "Failed to import projects", null);
for (IPath rootPath : rootPaths) {
File rootFolder = rootPath.toFile();
try {
for (IProjectImporter importer : importers()) {
importer.initialize(rootFolder);
if (importer.applies(projectConfigurations, subMonitor.split(1))) {
importer.importToWorkspace(subMonitor.split(70));
Map<String, List<IPath>> configurationsByFileName = projectConfigurations.stream()
.filter(rootPath::isPrefixOf)
.collect(Collectors.groupingBy(IPath::lastSegment));
for (List<IPath> configurations : configurationsByFileName.values()) {
try {
for (IProjectImporter importer : importers()) {
importer.initialize(rootFolder);
if (importer.applies(configurations, subMonitor.split(1))) {
importer.importToWorkspace(subMonitor.split(70));
break;
}
}
} catch (CoreException e) {
// if one type of the project import failed, keep importing the next type
importStatusCollection.add(e.getStatus());
JavaLanguageServerPlugin.logException("Failed to import projects", e);
}
} catch (CoreException e) {
// if a rootPath import failed, keep importing the next rootPath
importStatusCollection.add(e.getStatus());
JavaLanguageServerPlugin.logException("Failed to import projects", e);
}
}
if (!importStatusCollection.isOK()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,14 @@ public void dontFilterGitLikePackages() throws Exception {

@Test
public void testImportMavenSubModule() throws IOException, OperationCanceledException, CoreException {
Path projectDir = copyFiles("maven/multimodule", true).toPath();
File projectDir = copyFiles("maven/multimodule", true);
Path projectDirPath = projectDir.toPath();
Collection<IPath> configurationPaths = new ArrayList<>();
Path subModuleConfiguration = projectDir.resolve("module1/pom.xml");
Path subModuleConfiguration = projectDirPath.resolve("module1/pom.xml");
IPath filePath = ResourceUtils.canonicalFilePathFromURI(subModuleConfiguration.toUri().toString());
configurationPaths.add(filePath);
preferenceManager.getPreferences().setProjectConfigurations(configurationPaths);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.toString())), monitor);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.getAbsolutePath())), monitor);
IProject[] allProjects = ProjectUtils.getAllProjects();
Set<String> expectedProjects = new HashSet<>(Arrays.asList(
"module1",
Expand All @@ -232,13 +233,14 @@ public void testImportMavenSubModule() throws IOException, OperationCanceledExce

@Test
public void testImportMixedProjects() throws IOException, OperationCanceledException, CoreException {
Path projectDir = copyFiles("mixed", true).toPath();
File projectDir = copyFiles("mixed", true);
Path projectDirPath = projectDir.toPath();
Collection<IPath> configurationPaths = new ArrayList<>();
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDir.resolve("hello/.project").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDir.resolve("simple-gradle/build.gradle").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDir.resolve("salut/pom.xml").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDirPath.resolve("hello/.project").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDirPath.resolve("simple-gradle/build.gradle").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDirPath.resolve("salut/pom.xml").toUri().toString()));
preferenceManager.getPreferences().setProjectConfigurations(configurationPaths);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.toString())), monitor);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.getAbsolutePath())), monitor);
IProject[] allProjects = ProjectUtils.getAllProjects();
Set<String> expectedProjects = new HashSet<>(Arrays.asList(
"jdt.ls-java-project",
Expand All @@ -254,12 +256,13 @@ public void testImportMixedProjects() throws IOException, OperationCanceledExcep

@Test
public void testImportMixedProjectsPartially() throws IOException, OperationCanceledException, CoreException {
Path projectDir = copyFiles("mixed", true).toPath();
File projectDir = copyFiles("mixed", true);
Path projectDirPath = projectDir.toPath();
Collection<IPath> configurationPaths = new ArrayList<>();
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDir.resolve("simple-gradle/build.gradle").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDir.resolve("salut/pom.xml").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDirPath.resolve("simple-gradle/build.gradle").toUri().toString()));
configurationPaths.add(ResourceUtils.canonicalFilePathFromURI(projectDirPath.resolve("salut/pom.xml").toUri().toString()));
preferenceManager.getPreferences().setProjectConfigurations(configurationPaths);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.toString())), monitor);
projectsManager.initializeProjects(Collections.singleton(new org.eclipse.core.runtime.Path(projectDir.getAbsolutePath())), monitor);
IProject[] allProjects = ProjectUtils.getAllProjects();
Set<String> expectedProjects = new HashSet<>(Arrays.asList(
"jdt.ls-java-project",
Expand Down

0 comments on commit c971e49

Please sign in to comment.