From d997fcf5cca9fa3e124bf8ce84f68fbb735d251c Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Wed, 25 Oct 2023 14:09:43 +0800 Subject: [PATCH] refactor: Implement all the apply() methods Signed-off-by: Sheng Chen --- .../GradleBuildServerProjectImporter.java | 59 ++++++++++++++++--- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java index dc1e260cf..55cef9ee8 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/GradleBuildServerProjectImporter.java @@ -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) @@ -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) @@ -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 projectConfigurations, IProgressMonitor monitor) + throws OperationCanceledException, CoreException { + if (rootFolder == null) { + return false; + } + + + if (!Utils.isBuildServerEnabled(getPreferences())) { + return false; } - return !directories.isEmpty(); + Collection 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 @@ -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 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