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

fix: Store the schema version of the BSP project after import #1395

Merged
merged 2 commits into from
Aug 29, 2023
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 @@ -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;
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static ImporterPlugin getInstance() {

public static DigestStore getDigestStore() {
return instance.digestStore;
}
}

public static BuildServer getBuildServerConnection(IPath rootPath) throws CoreException {
Pair<BuildServer, BuildClient> pair = instance.buildServers.get(rootPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICommand> currentBuildSpecs = Arrays.asList(description.getBuildSpec());
List<ICommand> 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);
}

/**
Expand Down
Loading