Skip to content

Commit

Permalink
WIP on new experimental build file strategy idea
Browse files Browse the repository at this point in the history
  • Loading branch information
guw committed Dec 3, 2024
1 parent fe9feb9 commit 7df9b7d
Show file tree
Hide file tree
Showing 15 changed files with 812 additions and 193 deletions.
3 changes: 2 additions & 1 deletion bundles/com.salesforce.bazel.eclipse.core/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ output.. = bin/
additional.bundles = com.github.ben-manes.caffeine,\
com.google.gson,\
org.apache.commons.text,\
org.osgi.service.event
org.osgi.service.event,\
com.google.errorprone.annotations
4 changes: 4 additions & 0 deletions bundles/com.salesforce.bazel.eclipse.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@
class="com.salesforce.bazel.eclipse.core.model.discovery.ProjectPerPackageProvisioningStrategy"
name="project-per-package">
</targetProvisioningStrategy>
<targetDiscoveryStrategy
class="com.salesforce.bazel.eclipse.core.model.discovery.BazelBuildfileTargetDiscovery"
name="buildfiles">
</targetDiscoveryStrategy>
<targetProvisioningStrategy
class="com.salesforce.bazel.eclipse.core.model.discovery.BuildFileAndVisibilityDrivenProvisioningStrategy"
name="build-file-and-visibility-driven">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import org.eclipse.core.resources.ICommand;
import org.eclipse.core.resources.IFile;
Expand All @@ -44,6 +45,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.idea.blaze.base.model.primitives.TargetName;
import com.salesforce.bazel.eclipse.core.BazelCore;
import com.salesforce.bazel.eclipse.core.projectview.BazelProjectView;
import com.salesforce.bazel.sdk.model.BazelLabel;
Expand Down Expand Up @@ -592,24 +594,12 @@ boolean removeBazelBuilder(IProject project, IProgressMonitor monitor) throws Co
return true;
}

public void setBazelTargets(List<BazelTarget> targets, IProgressMonitor monitor) throws CoreException {
// the list can become extremely huge
// thus, instead of storing it as a persistent property on the project
// we put it into the .bazeltargets file

List<String> lines = new ArrayList<>();
lines.add("# targets used to setup the project");
lines.add("# (do not modify manually; synchronize projects to update)");
targets.stream().map(BazelTarget::getTargetName).distinct().sorted().forEach(lines::add);
public void setBazelTargetNames(List<TargetName> targetNames, IProgressMonitor monitor) throws CoreException {
writeBazeltargetsFile(targetNames.stream().map(TargetName::toString), monitor);
}

var content = lines.stream().collect(joining(lineSeparator())).getBytes(UTF_8);
var bazelTargetsFile = getProject().getFile(FILE_NAME_DOT_BAZELTARGETS);
if (!bazelTargetsFile.exists()) {
bazelTargetsFile.create(new ByteArrayInputStream(content), IResource.FORCE | IResource.DERIVED, monitor);
} else {
bazelTargetsFile
.setContents(new ByteArrayInputStream(content), IResource.FORCE | IResource.KEEP_HISTORY, monitor);
}
public void setBazelTargets(List<BazelTarget> targets, IProgressMonitor monitor) throws CoreException {
writeBazeltargetsFile(targets.stream().map(BazelTarget::getTargetName), monitor);
}

public void setModel(BazelModel bazelModel) {
Expand Down Expand Up @@ -637,4 +627,24 @@ public String toString() {
result.append("]");
return result.toString();
}

private void writeBazeltargetsFile(Stream<String> targetNames, IProgressMonitor monitor) throws CoreException {
// the list can become extremely huge
// thus, instead of storing it as a persistent property on the project
// we put it into the .bazeltargets file

List<String> lines = new ArrayList<>();
lines.add("# targets used to setup the project");
lines.add("# (do not modify manually; synchronize projects to update)");
targetNames.distinct().sorted().forEach(lines::add);

var content = lines.stream().collect(joining(lineSeparator())).getBytes(UTF_8);
var bazelTargetsFile = getProject().getFile(FILE_NAME_DOT_BAZELTARGETS);
if (!bazelTargetsFile.exists()) {
bazelTargetsFile.create(new ByteArrayInputStream(content), IResource.FORCE | IResource.DERIVED, monitor);
} else {
bazelTargetsFile
.setContents(new ByteArrayInputStream(content), IResource.FORCE | IResource.KEEP_HISTORY, monitor);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public BazelModuleFile getBazelModuleFile() throws CoreException {
*/
public BazelPackage getBazelPackage(BazelLabel label) {
checkIsRootedAtThisWorkspace(label);
return getBazelPackage(new org.eclipse.core.runtime.Path(label.getPackagePath()));
return getBazelPackage(IPath.forPosix(label.getPackagePath()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,8 @@ def format(target):
}

/**
* Called by {@link #provisionProjectsForSelectedTargets(Collection, BazelWorkspace, IProgressMonitor)} after base
* workspace information has been detected.
* Called by {@link #doProvisionProjects(Collection, BazelWorkspace, TracingSubMonitor)} after packages were opened
* and {@link BazelTarget targets} looked up.
* <p>
* Implementors are expected to map all of the targets into projects.
* </p>
Expand All @@ -1140,6 +1140,56 @@ def format(target):
protected abstract List<BazelProject> doProvisionProjects(Collection<BazelTarget> targets,
TracingSubMonitor monitor) throws CoreException;

/**
* Called by {@link #provisionProjectsForSelectedTargets(Collection, BazelWorkspace, IProgressMonitor)} after base
* workspace information has been detected.
* <p>
* The default implementation obtains and opens {@link BazelPackage} and {@link BazelTarget} objects.
* </p>
*
* @param targetsOrPackages
* collection of {@link TargetExpression target or wildcard expressions}
* @param workspace
* the Bazel workspace
* @param monitor
* monitor for reporting progress
* @return list of provisioned projects
* @throws CoreException
*/
protected List<BazelProject> doProvisionProjects(Collection<TargetExpression> targetsOrPackages,
BazelWorkspace workspace, TracingSubMonitor monitor) throws CoreException {
// open all packages at once
monitor.subTask("Loading packages");
var bazelPackages = targetsOrPackages.parallelStream().map(e -> {
if (e instanceof Label l) {
return new BazelLabel(l.toString());
}
var w = WildcardTargetPattern.stripWildcardSuffix(e.toString());
if (w != null) {
return new BazelLabel(w);
}
return null;
}).filter(Predicate.not(Objects::isNull)).map(workspace::getBazelPackage).distinct().toList();
workspace.open(bazelPackages);

// collect targets
monitor.subTask("Collecting targets");
List<BazelTarget> targets = new ArrayList<>();
for (TargetExpression targetExpression : targetsOrPackages) {
if (targetExpression instanceof Label l) {
// we don't check for no-ide tag here because we assume this was done already when discovering targets
targets.add(workspace.getBazelTarget(new BazelLabel(l.toString())));
} else {
LOG.warn(
"Ignoring target expression '{}' for provisioning because this is not supported by the current implementation.",
targetExpression);
}
}

// create projects
return doProvisionProjects(targets, monitor);
}

private void ensureFolderLinksToTarget(IFolder folderWhichShouldBeALink, IPath linkTarget, SubMonitor monitor)
throws CoreException {
if (folderWhichShouldBeALink.exists() && !folderWhichShouldBeALink.isLinked()) {
Expand Down Expand Up @@ -1674,34 +1724,6 @@ public List<BazelProject> provisionProjectsForSelectedTargets(Collection<TargetE
try {
var monitor = TracingSubMonitor.convert(progress, "Provisioning projects", 3);

// open all packages at once
monitor.subTask("Loading packages");
var bazelPackages = targetsOrPackages.parallelStream().map(e -> {
if (e instanceof Label l) {
return new BazelLabel(l.toString());
}
var w = WildcardTargetPattern.stripWildcardSuffix(e.toString());
if (w != null) {
return new BazelLabel(w);
}
return null;
}).filter(Predicate.not(Objects::isNull)).map(workspace::getBazelPackage).distinct().toList();
workspace.open(bazelPackages);

// collect targets
monitor.subTask("Collecting targets");
List<BazelTarget> targets = new ArrayList<>();
for (TargetExpression targetExpression : targetsOrPackages) {
if (targetExpression instanceof Label l) {
// we don't check for no-ide tag here because we assume this was done already when discovering targets
targets.add(workspace.getBazelTarget(new BazelLabel(l.toString())));
} else {
LOG.warn(
"Ignoring target expression '{}' for provisioning because this is not supported by the current implementation.",
targetExpression);
}
}

// ensure there is a mapper
fileSystemMapper = new BazelProjectFileSystemMapper(workspace);

Expand All @@ -1715,8 +1737,7 @@ public List<BazelProject> provisionProjectsForSelectedTargets(Collection<TargetE
// configure the classpath of the workspace project
configureRawClasspathOfWorkspaceProject(workspace.getBazelProject().getProject(), monitor.slice(1));

// create projects
return doProvisionProjects(targets, monitor);
return doProvisionProjects(targetsOrPackages, workspace, monitor);
} finally {
progress.done();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.salesforce.bazel.eclipse.core.model.discovery;

import java.util.Collection;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

import com.google.idea.blaze.base.model.primitives.TargetExpression;
import com.google.idea.blaze.base.model.primitives.WorkspacePath;
import com.salesforce.bazel.eclipse.core.model.BazelWorkspace;

/**
* A simplification of {@link BazelQueryTargetDiscovery} which ignores targets and assumes all from selected packages.
* <p>
* This is recommended with the {@link BuildfileDrivenProvisioningStrategy}.
* </p>
*/
public class BazelBuildfileTargetDiscovery extends BazelQueryTargetDiscovery implements TargetDiscoveryStrategy {

public static final String STRATEGY_NAME = "buildfiles";

@Override
public Collection<TargetExpression> discoverTargets(BazelWorkspace bazelWorkspace,
Collection<WorkspacePath> bazelPackages, IProgressMonitor progress) throws CoreException {
return bazelPackages.stream().map(TargetExpression::allFromPackageNonRecursive).toList();
}

}
Loading

0 comments on commit 7df9b7d

Please sign in to comment.