Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo committed Nov 13, 2023
1 parent e0a1f24 commit 96a08e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ public void initialize(File rootFolder) {
public abstract boolean applies(IProgressMonitor monitor) throws OperationCanceledException, CoreException;

@Override
public boolean isResolved(File folder) throws OperationCanceledException, CoreException {
return directories != null && directories.contains(folder.toPath());
public boolean isResolved(File file) throws OperationCanceledException, CoreException {
if (directories == null) {
return false;
}
// if input is a directory (usually the root folder),
// check if the importer has imported it.
if (file.isDirectory()) {
return directories.contains(file.toPath());
}

// if the input is a file, check if the parent directory is imported.
return directories.stream().anyMatch((directory) -> {
return file.toPath().getParent().equals(directory);
});
};

@Override
Expand Down Expand Up @@ -82,6 +94,10 @@ protected Collection<Path> findProjectPathByConfigurationName(Collection<IPath>
return filteredPaths;
}

return eliminateNestedPaths(filteredPaths);
}

protected List<Path> eliminateNestedPaths(List<Path> filteredPaths) {
Path parentDir = null;
List<Path> result = new LinkedList<>();
for (Path path : filteredPaths) {
Expand All @@ -95,7 +111,6 @@ protected Collection<Path> findProjectPathByConfigurationName(Collection<IPath>
parentDir = path;
}
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ public class GradleProjectImporter extends AbstractProjectImporter {
.replaceAll("\n", System.lineSeparator());
//@formatter:on

/**
* A flag whether this importer is activated by manual selection mode.
*/
private boolean manualSelection = false;

/* (non-Javadoc)
* @see org.eclipse.jdt.ls.core.internal.managers.IProjectImporter#applies(org.eclipse.core.runtime.IProgressMonitor)
*/
Expand Down Expand Up @@ -158,6 +163,7 @@ public boolean applies(IProgressMonitor monitor) throws CoreException {

@Override
public boolean applies(Collection<IPath> buildFiles, IProgressMonitor monitor) {
manualSelection = true;
if (!getPreferences().isImportGradleEnabled()) {
return false;
}
Expand All @@ -167,7 +173,7 @@ public boolean applies(Collection<IPath> buildFiles, IProgressMonitor monitor) {
SETTINGS_GRADLE_DESCRIPTOR,
BUILD_GRADLE_KTS_DESCRIPTOR,
SETTINGS_GRADLE_KTS_DESCRIPTOR
), false /*includeNested*/);
), true /*includeNested*/);
if (configurationDirs == null || configurationDirs.isEmpty()) {
return false;
}
Expand Down Expand Up @@ -199,16 +205,20 @@ public void importToWorkspace(IProgressMonitor monitor) throws CoreException {
if (!applies(monitor)) {
return;
}
int projectSize = directories.size();
List<Path> directoriesToImport = new ArrayList<>(this.directories);
if (manualSelection) {
directoriesToImport = eliminateNestedPaths(directoriesToImport);
}
int projectSize = directoriesToImport.size();
SubMonitor subMonitor = SubMonitor.convert(monitor, projectSize + 1);
subMonitor.setTaskName(IMPORTING_GRADLE_PROJECTS);
JavaLanguageServerPlugin.logInfo(IMPORTING_GRADLE_PROJECTS);
subMonitor.worked(1);
// run just once at the first project, assuming that all projects are using the same gradle version.
inferGradleJavaHome(directories.iterator().next(), monitor);
inferGradleJavaHome(directoriesToImport.iterator().next(), monitor);
MultiStatus compatibilityStatus = new MultiStatus(IConstants.PLUGIN_ID, -1, "Compatibility issue occurs when importing Gradle projects", null);
MultiStatus gradleUpgradeWrapperStatus = new MultiStatus(IConstants.PLUGIN_ID, -1, "Gradle upgrade wrapper", null);
for (Path directory : directories) {
for (Path directory : directoriesToImport) {
IStatus importStatus = importDir(directory, subMonitor.newChild(1));
if (isFailedStatus(importStatus) && importStatus instanceof GradleCompatibilityStatus) {
compatibilityStatus.add(importStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -173,30 +174,39 @@ 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();
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;
}
Set<IPath> buildFiles = projectConfigurations.stream()
.filter(rootPath::isPrefixOf).collect(Collectors.toSet());
try {
for (IProjectImporter importer : importers()) {
importer.initialize(rootFolder);
if (importer.applies(buildFiles, subMonitor.split(1))) {
importer.importToWorkspace(subMonitor.split(70));
buildFiles = removeImportedConfigurations(buildFiles, importer);
}
} 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()) {
throw new CoreException(importStatusCollection);
}
}

private Set<IPath> removeImportedConfigurations(Set<IPath> configurations, IProjectImporter importer) {
return configurations.stream()
.filter(config -> {
try {
return !importer.isResolved(config.toFile());
} catch (OperationCanceledException | CoreException e) {
return true;
}
})
.collect(Collectors.toSet());
}

public void importProjects(IProgressMonitor monitor) {
WorkspaceJob job = new WorkspaceJob("Importing projects in workspace...") {

Expand Down

0 comments on commit 96a08e0

Please sign in to comment.