Skip to content

Commit

Permalink
refactor: Implement all the apply() methods
Browse files Browse the repository at this point in the history
Signed-off-by: Sheng Chen <[email protected]>
  • Loading branch information
jdneo committed Oct 25, 2023
1 parent 205f273 commit d997fcf
Showing 1 changed file with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti
return false;
}

// if the current root already contains Gradle Java project
// imported by Buildship, skip the build server importer.
for (IProject project : ProjectUtils.getGradleProjects()) {
if (ProjectUtils.isJavaProject(project)
&& project.getLocation().toFile().toPath().startsWith(rootFolder.toPath())) {
return false;
}
}

if (directories == null) {
BasicFileDetector gradleDetector = new BasicFileDetector(rootFolder.toPath(), BUILD_GRADLE_DESCRIPTOR,
SETTINGS_GRADLE_DESCRIPTOR, BUILD_GRADLE_KTS_DESCRIPTOR, SETTINGS_GRADLE_KTS_DESCRIPTOR)
Expand All @@ -77,6 +86,10 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti
directories = gradleDetector.scan(monitor);
}

if (directories.isEmpty()) {
return false;
}

for (java.nio.file.Path directory : directories) {
// we don't support android
BasicFileDetector androidDetector = new BasicFileDetector(directory, ANDROID_MANIFEST)
Expand All @@ -90,17 +103,35 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti
telemetry);
return false;
}
}

IProject project = ProjectUtils.getProjectFromUri(directory.toUri().toString());
// skip this importer if any of the project in workspace is already
// imported by other importers.
if (project != null && (!Utils.isGradleBuildServerProject(project)
&& ProjectUtils.isJavaProject(project))) {
return false;
}
return true;
}

@Override
public boolean applies(Collection<IPath> projectConfigurations, IProgressMonitor monitor)
throws OperationCanceledException, CoreException {
if (rootFolder == null) {
return false;
}


if (!Utils.isBuildServerEnabled(getPreferences())) {
return false;
}

return !directories.isEmpty();
Collection<java.nio.file.Path> directories = findProjectPathByConfigurationName(
projectConfigurations,
Arrays.asList(
BUILD_GRADLE_DESCRIPTOR,
SETTINGS_GRADLE_DESCRIPTOR,
BUILD_GRADLE_KTS_DESCRIPTOR,
SETTINGS_GRADLE_KTS_DESCRIPTOR
),
false /*includeNested*/
);

return !directories.isEmpty() && !importedByOtherImporters(directories);
}

@Override
Expand Down Expand Up @@ -200,6 +231,18 @@ public static boolean updateConfigurationDigest(IProject project) {
return result;
}

/**
* Return false if any of the input folder has already been imported as a
* Java project by other importer.
*/
private boolean importedByOtherImporters(Collection<java.nio.file.Path> directories) {
return directories.stream()
.map(directory -> ProjectUtils.getProjectFromUri(directory.toUri().toString()))
.anyMatch(project -> !Utils.isGradleBuildServerProject(project) &&
ProjectUtils.isJavaProject(project)
);
}

/**
* Import the projects according to the available build targets. If a build target
* maps to a project that is already imported by other importer
Expand Down

0 comments on commit d997fcf

Please sign in to comment.