From a86ceddabe00953d4ef154e14ddd2bb459872374 Mon Sep 17 00:00:00 2001 From: Sheng Chen Date: Mon, 25 Sep 2023 19:49:53 -0700 Subject: [PATCH] fix: Skip builders when build server is disabled (#1424) Signed-off-by: Sheng Chen --- .../GradleBuildServerProjectImporter.java | 16 +--------------- .../microsoft/gradle/bs/importer/Utils.java | 18 ++++++++++++++++++ .../importer/builder/BuildServerBuilder.java | 6 ++++++ .../java/builder/jdtbuilder/JavaBuilder.java | 6 ++++++ 4 files changed, 31 insertions(+), 15 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 5de270284..4128a55ca 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 @@ -1,7 +1,5 @@ package com.microsoft.gradle.bs.importer; -import static org.eclipse.jdt.ls.core.internal.handlers.MapFlattener.getString; - import java.io.File; import java.net.URI; import java.util.Arrays; @@ -57,20 +55,14 @@ public class GradleBuildServerProjectImporter extends AbstractProjectImporter { public static final String SETTINGS_GRADLE_DESCRIPTOR = "settings.gradle"; public static final String SETTINGS_GRADLE_KTS_DESCRIPTOR = "settings.gradle.kts"; - private static final String JAVA_BUILD_SERVER_GRADLE_ENABLED = "java.gradle.buildServer.enabled"; - @Override public boolean applies(IProgressMonitor monitor) throws OperationCanceledException, CoreException { if (rootFolder == null) { return false; } - Preferences preferences = getPreferences(); - if (!preferences.isImportGradleEnabled()) { - return false; - } - if (!isBuildServerEnabled()) { + if (!Utils.isBuildServerEnabled(getPreferences())) { return false; } @@ -264,10 +256,4 @@ private BuildServerPreferences getBuildServerPreferences() { pref.setJdks(EclipseVmUtil.getAllVmInstalls()); return pref; } - - private boolean isBuildServerEnabled() { - Preferences preferences = getPreferences(); - String bspImporterEnabled = getString(preferences.asMap(), JAVA_BUILD_SERVER_GRADLE_ENABLED); - return "on".equalsIgnoreCase(bspImporterEnabled); - } } diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java index 66ec4407d..1e993697d 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/Utils.java @@ -1,5 +1,7 @@ package com.microsoft.gradle.bs.importer; +import static org.eclipse.jdt.ls.core.internal.handlers.MapFlattener.getString; + import java.net.URI; import java.net.URISyntaxException; import java.util.Arrays; @@ -20,6 +22,7 @@ import org.eclipse.core.runtime.URIUtil; import org.eclipse.jdt.core.JavaCore; import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences; import com.microsoft.gradle.bs.importer.builder.BuildServerBuilder; @@ -29,6 +32,8 @@ public class Utils { private Utils() {} + private static final String JAVA_BUILD_SERVER_GRADLE_ENABLED = "java.gradle.buildServer.enabled"; + public static boolean isGradleBuildServerProject(IProject project) { return ProjectUtils.hasNature(project, GradleBuildServerProjectNature.NATURE_ID); } @@ -189,4 +194,17 @@ public static ICommand getBuildServerBuildSpec(IProjectDescription description) buildSpec.setBuilding(IncrementalProjectBuilder.CLEAN_BUILD, false); return buildSpec; } + + public static boolean isBuildServerEnabled(Preferences preferences) { + if (preferences == null) { + return false; + } + + if (!preferences.isImportGradleEnabled()) { + return false; + } + + String bspImporterEnabled = getString(preferences.asMap(), JAVA_BUILD_SERVER_GRADLE_ENABLED); + return "on".equalsIgnoreCase(bspImporterEnabled); + } } diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/builder/BuildServerBuilder.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/builder/BuildServerBuilder.java index 2be62aeb2..d00122e2c 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/builder/BuildServerBuilder.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/builder/BuildServerBuilder.java @@ -15,6 +15,7 @@ import org.eclipse.core.runtime.Status; import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; import org.eclipse.jdt.ls.core.internal.ProjectUtils; +import org.eclipse.jdt.ls.core.internal.preferences.Preferences; import com.microsoft.gradle.bs.importer.BuildServerConnection; import com.microsoft.gradle.bs.importer.ImporterPlugin; @@ -32,6 +33,11 @@ public class BuildServerBuilder extends IncrementalProjectBuilder { @Override protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws CoreException { + Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences(); + if (!Utils.isBuildServerEnabled(preferences)) { + return null; + } + IProject project = this.getProject(); IPath rootPath = ProjectUtils.findBelongedWorkspaceRoot(project.getLocation()); if (rootPath == null) { diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/java/builder/jdtbuilder/JavaBuilder.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/java/builder/jdtbuilder/JavaBuilder.java index 17ed3bdac..1fc959edb 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/java/builder/jdtbuilder/JavaBuilder.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/java/builder/jdtbuilder/JavaBuilder.java @@ -25,7 +25,9 @@ import org.eclipse.jdt.internal.core.*; import org.eclipse.jdt.internal.core.util.Messages; import org.eclipse.jdt.internal.core.util.Util; +import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin; +import com.microsoft.gradle.bs.importer.Utils; import com.microsoft.java.builder.BuildStateManager; import java.io.*; @@ -171,6 +173,10 @@ public static void writeState(Object state, DataOutputStream out) throws IOExcep @Override protected IProject[] build(int kind, Map ignored, IProgressMonitor monitor) throws CoreException { + org.eclipse.jdt.ls.core.internal.preferences.Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences(); + if (!Utils.isBuildServerEnabled(preferences)) { + return null; + } this.currentProject = getProject(); if (this.currentProject == null || !this.currentProject.isAccessible()) return new IProject[0];