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

Delete unrelated gradle projects during initialization #1265

Merged
merged 1 commit into from
Nov 13, 2019
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 @@ -29,6 +29,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
Expand Down Expand Up @@ -245,13 +246,22 @@ public void cleanupResources(IProject project) throws CoreException {

private void deleteInvalidProjects(Collection<IPath> rootPaths, IProgressMonitor monitor) {
List<String> workspaceProjects = rootPaths.stream().map((IPath rootPath) -> ProjectUtils.getWorkspaceInvisibleProjectName(rootPath)).collect(Collectors.toList());
List<IProject> validGradleProjects = new ArrayList<>();
List<IProject> suspiciousGradleProjects = new ArrayList<>();
for (IProject project : getWorkspaceRoot().getProjects()) {
if (project.equals(this.getDefaultProject())) {
continue;
}
if (project.exists() && (ResourceUtils.isContainedIn(project.getLocation(), rootPaths) || ProjectUtils.isGradleProject(project)) || workspaceProjects.contains(project.getName())) {
if (project.exists() && (ResourceUtils.isContainedIn(project.getLocation(), rootPaths) || ProjectUtils.isGradleProject(project) || workspaceProjects.contains(project.getName()))) {
try {
project.getDescription();
if (ProjectUtils.isGradleProject(project)) {
if (ResourceUtils.isContainedIn(project.getLocation(), rootPaths)) {
validGradleProjects.add(project);
} else {
suspiciousGradleProjects.add(project);
}
}
} catch (CoreException e) {
try {
project.delete(true, monitor);
Expand All @@ -267,6 +277,69 @@ private void deleteInvalidProjects(Collection<IPath> rootPaths, IProgressMonitor
}
}
}

List<IProject> unrelatedProjects = findUnrelatedGradleProjects(suspiciousGradleProjects, validGradleProjects);
unrelatedProjects.forEach((project) -> {
try {
project.delete(false, true, monitor);
} catch (CoreException e1) {
JavaLanguageServerPlugin.logException(e1.getMessage(), e1);
}
});
}

/**
* Find those gradle projects not referenced by any gradle project in the current workspace.
*/
private List<IProject> findUnrelatedGradleProjects(List<IProject> suspiciousProjects, List<IProject> validProjects) {
suspiciousProjects.sort((IProject p1, IProject p2) -> p1.getLocation().toOSString().length() - p2.getLocation().toOSString().length());

List<IProject> unrelatedProjects = new ArrayList<>();
Collection<IPath> verifiedPaths = new ArrayList<>();
for (IProject suspiciousProject : suspiciousProjects) {
IPath uncheckedPath = suspiciousProject.getLocation();
if (ResourceUtils.isContainedIn(uncheckedPath, verifiedPaths)) {
continue;
}

if (isReferencedByGradleProjects(suspiciousProject, validProjects)) {
verifiedPaths.add(suspiciousProject.getLocation());
} else {
unrelatedProjects.add(suspiciousProject);
}
}

return unrelatedProjects;
}

private boolean isReferencedByGradleProjects(IProject target, List<IProject> gradleProjects) {
for (IProject gradleProject : gradleProjects) {
IPath commonPath = getCommonPath(target.getLocation(), gradleProject.getLocation());
if (commonPath != null && commonPath.append("build.gradle").toFile().exists() || commonPath.append("settings.gradle").toFile().exists()) {
return true;
}
}

return false;
}

private IPath getCommonPath(IPath path1, IPath path2) {
if (path1 == null || path2 == null) {
return null;
}

if (path1.segmentCount() > path2.segmentCount()) {
IPath temp = path1;
path1 = path2;
path2 = temp;
}

int commonSegments = path1.matchingFirstSegments(path2);
if (commonSegments > 0 && Objects.equals(path1.getDevice(), path2.getDevice())) {
return path1.removeLastSegments(path1.segmentCount() - commonSegments);
}

return null;
}

private static IWorkspaceRoot getWorkspaceRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -87,6 +88,23 @@ public void importNestedGradleProject() throws Exception {
assertIsGradleProject(gradle2);
}

@Test
public void testDeleteInvalidProjects() throws Exception {
List<IProject> projects = importProjects(Arrays.asList("gradle/nested/gradle1", "gradle/nested/gradle2"));
assertEquals(3, projects.size());//default + 2 gradle projects
IProject gradle1 = WorkspaceHelper.getProject("gradle1");
assertIsGradleProject(gradle1);
IProject gradle2 = WorkspaceHelper.getProject("gradle2");
assertIsGradleProject(gradle2);

projects = importProjects("gradle/nested/gradle1");
assertEquals(2, projects.size());
gradle1 = WorkspaceHelper.getProject("gradle1");
assertNotNull(gradle1);
gradle2 = WorkspaceHelper.getProject("gradle2");
assertNull(gradle2);
}

@Test
public void testJavaImportExclusions() throws Exception {
List<String> javaImportExclusions = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getJavaImportExclusions();
Expand Down