Skip to content

Commit

Permalink
Today's dogfooding exercise: fix issues salesforce#70 and salesforce#79
Browse files Browse the repository at this point in the history
… + minor tweaks
  • Loading branch information
simontoens committed Mar 24, 2020
1 parent 0056201 commit 490ff8e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,23 @@
*/
package com.salesforce.bazel.eclipse.launch;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IPath;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.sourcelookup.AbstractSourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant;
import org.eclipse.debug.core.sourcelookup.containers.ExternalArchiveSourceContainer;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;
import org.eclipse.jdt.launching.sourcelookup.containers.JavaSourceLookupParticipant;

import com.salesforce.bazel.eclipse.BazelPluginActivator;

/**
* Copied and adapted from org.eclipse.jdt.internal.launching.JavaSourceLookupDirector.
*
Expand All @@ -52,25 +58,40 @@
*/
public class BazelJavaSourceLookupDirector extends AbstractSourceLookupDirector {

private static final String JAVA_SRC_PATH_COMP = "org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer";

private final IJavaProject mainProject;
private final List<IJavaProject> otherProjects;

public BazelJavaSourceLookupDirector(IJavaProject mainProject, List<IJavaProject> otherProjects) {
this.mainProject = mainProject;
this.otherProjects = otherProjects;
setSourcePathComputer(
getLaunchManager().getSourcePathComputer("org.eclipse.jdt.launching.sourceLookup.javaSourcePathComputer"));
setSourcePathComputer(getLaunchManager().getSourcePathComputer(JAVA_SRC_PATH_COMP));
}

@Override
public void initializeParticipants() {
addParticipants(new ISourceLookupParticipant[] { new JavaSourceLookupParticipant() });
ISourceContainer[] sourceContainers = new JavaProjectSourceContainer[otherProjects.size() + 1];
sourceContainers[0] = new JavaProjectSourceContainer(mainProject);
for (int i = 0; i < otherProjects.size(); i++) {
sourceContainers[i+1] = new JavaProjectSourceContainer(otherProjects.get(i));
List<ISourceContainer> sourceContainers = new ArrayList<>();
sourceContainers.add(new JavaProjectSourceContainer(this.mainProject));
addSourceJarsForDependencies(this.mainProject, sourceContainers);
for (IJavaProject project : this.otherProjects) {
sourceContainers.add(new JavaProjectSourceContainer(project));
addSourceJarsForDependencies(project, sourceContainers);
}
setSourceContainers(sourceContainers.toArray(new ISourceContainer[sourceContainers.size()]));
}

private static void addSourceJarsForDependencies(IJavaProject project, List<ISourceContainer> sourceContainers) {
IClasspathEntry[] resolvedClasspath = BazelPluginActivator.getJavaCoreHelper().getResolvedClasspath(project, true);
for (IClasspathEntry e : resolvedClasspath) {
if (e.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
IPath sourceAttachmentPath = e.getSourceAttachmentPath();
if (sourceAttachmentPath != null) {
sourceContainers.add(new ExternalArchiveSourceContainer(sourceAttachmentPath.toOSString(), false));
}
}
}
setSourceContainers(sourceContainers);
}

private static ILaunchManager getLaunchManager() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private BazelLabel chooseBazelTargetForProject(IProject project) {

BazelLabel target = getSelectedTarget();
if (target != null) {
dialog.setInitialSelections(new Object[] { target.toString() });
dialog.setInitialSelections(new Object[] { target });
}
if (dialog.open() == Window.OK) {
return (BazelLabel) dialog.getFirstResult();
Expand Down Expand Up @@ -313,12 +313,13 @@ public String getText(Object element) {
private ILabelProvider BAZEL_LABEL_LABEL_PROVIDER = new DefaultLabelProvider() {
@Override
public String getText(Object element) {
// lets also show the target type next to each label, for example:
// //projects/libs/scone:app (java_binrary)
// //projects/libs/scone/abstractions:mytest1 (java_test)
BazelLabel label = ((BazelLabel)element);
TargetKind targetKind = BazelLaunchConfigurationTab.this.lookupLabelKind(label);
return label.getLabel() + " (" + targetKind.getKind() + ")";
return toFriendlyLabelRepresentation(label, targetKind);
}
};

private static String toFriendlyLabelRepresentation(BazelLabel label, TargetKind targetKind) {
return label.getLastComponentOfTargetName() + " (" + targetKind.getKind() + ") - " + label.getLabel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void editorSaved() {
List<BazelPackageLocation> currentlyImportedPackages = getPackages(currentlyImportedProjects);
List<BazelPackageLocation> packagesToImport = projectView.getPackages();

if (new HashSet<>(currentlyImportedPackages).equals(new HashSet<>(packagesToImport))) {
if (currentlyImportedPackages != null && new HashSet<>(currentlyImportedPackages).equals(new HashSet<>(packagesToImport))) {
LOG.info("The Bazel Packages in the " + ProjectViewConstants.PROJECT_VIEW_FILE_NAME + " file match the set of Eclipse Projects currently imported");
} else {
boolean ok = MessageDialog.openConfirm(this.getSite().getShell(), "Update Imported Projects", CONFIRMATION_TEXT);
Expand All @@ -104,6 +104,12 @@ private List<BazelPackageLocation> getPackages(IJavaProject[] projects) {
List<BazelPackageLocation> packageLocations = new ArrayList<>(projects.length);
for (IJavaProject project : projects) {
// get the target to get at the package path
List<String> targets = BazelEclipseProjectSupport.getBazelTargetsForEclipseProject(project.getProject(), false);
if (targets == null || targets.isEmpty()) {
// this shouldn't happen, but if it does, we do not want to blow up here
// instead return null to force re-import
return null;
}
String target = BazelEclipseProjectSupport.getBazelTargetsForEclipseProject(project.getProject(), false).get(0);
BazelLabel label = new BazelLabel(target);
packageLocations.add(new ProjectViewPackageLocation(this.rootDirectory, label.getPackagePath()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public File computeBazelWorkspaceOutputBase() {
if (bazelOutputBaseDirectory == null) {
try {
ImmutableList.Builder<String> argBuilder = ImmutableList.builder();
argBuilder.add("info").add("execution_root");
argBuilder.add("info").add("output_base");

List<String> outputLines = bazelCommandExecutor.runBazelAndGetOutputLines(bazelWorkspaceRootDirectory, null,
argBuilder.build(), (t) -> t);
Expand Down

0 comments on commit 490ff8e

Please sign in to comment.