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 e20538e98..77dd302d6 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 @@ -10,6 +10,9 @@ import java.util.Map; import java.util.Map.Entry; +import org.eclipse.core.internal.resources.Project; +import org.eclipse.core.internal.resources.ProjectDescription; +import org.eclipse.core.internal.resources.VariableDescription; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectDescription; @@ -46,6 +49,9 @@ public class GradleBuildServerProjectImporter extends AbstractProjectImporter { private static final String BSP_VERSION = "2.1.0-M4"; + private static final String SCHEMA_VERSION_KEY = "bspSchemaVersion"; + private static final String SCHEMA_VERSION = "0.1.0"; + public static final String BUILD_GRADLE_DESCRIPTOR = "build.gradle"; public static final String BUILD_GRADLE_KTS_DESCRIPTOR = "build.gradle.kts"; public static final String SETTINGS_GRADLE_DESCRIPTOR = "settings.gradle"; @@ -187,6 +193,10 @@ private IProject createProject(File directory, IProgressMonitor monitor) throws String projectName = findFreeProjectName(directory.getName()); IWorkspace workspace = ResourcesPlugin.getWorkspace(); IProjectDescription projectDescription = workspace.newProjectDescription(projectName); + if (projectDescription instanceof ProjectDescription description) { + VariableDescription variableDescription = new VariableDescription(SCHEMA_VERSION_KEY, SCHEMA_VERSION); + description.setVariableDescription(SCHEMA_VERSION_KEY, variableDescription); + } projectDescription.setLocation(Path.fromOSString(directory.getPath())); projectDescription.setNatureIds(new String[]{ GradleBuildServerProjectNature.NATURE_ID }); ICommand buildSpec = Utils.getBuildServerBuildSpec(projectDescription); @@ -208,6 +218,17 @@ private void updateProjectDescription(IProject project, IProgressMonitor monitor Utils.getBuildServerBuildSpec(projectDescription), problemReporter }, monitor); + + // Here we don't use the public API: {@code project.setDescription()} to update the project, + // because that API will ignore the variable descriptions. + if (project instanceof Project internalProject) { + ProjectDescription description = internalProject.internalGetDescription(); + VariableDescription variableDescription = new VariableDescription(SCHEMA_VERSION_KEY, SCHEMA_VERSION); + boolean changed = description.setVariableDescription(SCHEMA_VERSION_KEY, variableDescription); + if (changed) { + internalProject.writeDescription(IResource.NONE); + } + } } private String findFreeProjectName(String baseName) { diff --git a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/ImporterPlugin.java b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/ImporterPlugin.java index 8eb5f9abb..4aec9ca72 100644 --- a/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/ImporterPlugin.java +++ b/extension/jdtls.ext/com.microsoft.gradle.bs.importer/src/com/microsoft/gradle/bs/importer/ImporterPlugin.java @@ -65,7 +65,7 @@ public static ImporterPlugin getInstance() { public static DigestStore getDigestStore() { return instance.digestStore; - } + } public static BuildServer getBuildServerConnection(IPath rootPath) throws CoreException { Pair pair = instance.buildServers.get(rootPath); 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 611405e88..db6126b4e 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 @@ -124,28 +124,13 @@ public static void addNature(IProject project, String natureId, IProgressMonitor * @throws CoreException */ public static void addBuildSpec(IProject project, String[] buildNames, IProgressMonitor monitor) throws CoreException { - SubMonitor progress = SubMonitor.convert(monitor, 1); - // get the description IProjectDescription description = project.getDescription(); - List currentBuildSpecs = Arrays.asList(description.getBuildSpec()); - List newSpecs = new LinkedList<>(); - newSpecs.addAll(currentBuildSpecs); - for (String buildName : buildNames) { - if (currentBuildSpecs.stream().anyMatch(spec -> Objects.equals(spec.getBuilderName(), buildName))) { - continue; - } - + ICommand[] commands = Arrays.stream(buildNames).map(buildName -> { ICommand buildSpec = description.newCommand(); buildSpec.setBuilderName(buildName); - newSpecs.add(0, buildSpec); - } - - if (newSpecs.size() == currentBuildSpecs.size()) { - return; - } - - description.setBuildSpec(newSpecs.toArray(new ICommand[newSpecs.size()])); - project.setDescription(description, IResource.AVOID_NATURE_CONFIG, progress.newChild(1)); + return buildSpec; + }).toArray(ICommand[]::new); + addBuildSpec(project, commands, monitor); } /**