Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Commit

Permalink
(#83) Fix ported subset of SDK classes
Browse files Browse the repository at this point in the history
  • Loading branch information
PMitrafanau committed Mar 18, 2021
1 parent 0113ce8 commit 75a0485
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@
@SuppressWarnings("restriction")
public final class BazelProjectImporter extends AbstractProjectImporter {

private static final String WORKSPACE_FILE_NAME = "WORKSPACE";
public static final String BAZELPROJECT_FILE_NAME = ".bazelproject";

private static final String BAZELPROJECT_FILE_NAME = ".bazelproject";
private static final String WORKSPACE_FILE_NAME = "WORKSPACE";

@Override
public boolean applies(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
Expand All @@ -85,46 +85,45 @@ public boolean applies(IProgressMonitor monitor) throws OperationCanceledExcepti

@Override
public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
try {
BazelWorkspaceScanner workspaceScanner = new BazelWorkspaceScanner();
BazelPackageInfo workspaceRootPackage = workspaceScanner.getPackages(rootFolder);

if (workspaceRootPackage == null) {
throw new IllegalArgumentException();
}

List<BazelPackageInfo> allBazelPackages = new ArrayList<>(
workspaceRootPackage.getChildPackageInfos()
);

List<BazelPackageInfo> bazelPackagesToImport = allBazelPackages;

File targetsFile = new File(rootFolder, BAZELPROJECT_FILE_NAME);

if (targetsFile.exists()) {
ProjectView projectView = new ProjectView(rootFolder, readFile(targetsFile.getPath()));

Set<String> projectViewPaths = projectView.getDirectories().stream()
.map(p -> p.getBazelPackageFSRelativePath()).collect(Collectors.toSet());

bazelPackagesToImport = allBazelPackages.stream().filter(bpi -> projectViewPaths.contains(bpi.getBazelPackageFSRelativePath()))
.collect(Collectors.toList());
}
try {
BazelWorkspaceScanner workspaceScanner = new BazelWorkspaceScanner();
BazelPackageInfo workspaceRootPackage = workspaceScanner.getPackages(rootFolder);

if (workspaceRootPackage == null) {
throw new IllegalArgumentException();
}

List<BazelPackageInfo> allBazelPackages = new ArrayList<>(workspaceRootPackage.getChildPackageInfos());

List<BazelPackageInfo> bazelPackagesToImport = allBazelPackages;

WorkProgressMonitor progressMonitor = new EclipseWorkProgressMonitor(null);
File targetsFile = new File(rootFolder, BAZELPROJECT_FILE_NAME);

BazelEclipseProjectFactory.importWorkspace(workspaceRootPackage, bazelPackagesToImport, progressMonitor,
monitor);
} catch (IOException e) {
// TODO: proper handling here
}
if (targetsFile.exists()) {
ProjectView projectView = new ProjectView(rootFolder, readFile(targetsFile.getPath()));

Set<String> projectViewPaths = projectView.getDirectories().stream()
.map(p -> p.getBazelPackageFSRelativePath()).collect(Collectors.toSet());

bazelPackagesToImport = allBazelPackages.stream()
.filter(bpi -> projectViewPaths.contains(bpi.getBazelPackageFSRelativePath()))
.collect(Collectors.toList());
}

WorkProgressMonitor progressMonitor = new EclipseWorkProgressMonitor(null);

BazelEclipseProjectFactory.importWorkspace(workspaceRootPackage, bazelPackagesToImport, progressMonitor,
monitor);
} catch (IOException e) {
// TODO: proper handling here
}
}

@Override
public void reset() {

}

private static String readFile(String path) {
try {
return new String(Files.readAllBytes(Paths.get(path)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
* @author Blaine Buxton
*
*/
public class LogHelper {
final Class<?> from;
public final class LogHelper {
private final Class<?> from;

public static LogHelper log(Class<?> from) {
return new LogHelper(from);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
* This is an interface so that tests can use it and verify logging
*/
public abstract class LoggerFacade {
static LoggerFacade instance = new BasicLoggerFacade();
private static LoggerFacade instance = new BasicLoggerFacade();

/**
* Default instance, this can change - DO NOT CACHE or STORE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@
import com.google.common.annotations.VisibleForTesting;
import com.salesforce.bazel.sdk.logging.LogHelper;

public class BazelBuildFileHelper {
public final class BazelBuildFileHelper {

private BazelBuildFileHelper() {

}

static final LogHelper LOG = LogHelper.log(BazelBuildFileHelper.class);
/**
* List of Strings that can be found in BUILD files that will indicate a Bazel package that is supported by the
Expand All @@ -55,8 +60,8 @@ public class BazelBuildFileHelper {
* enough to trigger the detector.
*/
public static final String[] JAVA_PROJECT_INDICATORS =
{ "java_binary", "java_library", "java_test", "java_web_test_suite", "springboot", "springboot_test",
"java_proto_library", "java_lite_proto_library", "java_grpc_library" };
{"java_binary", "java_library", "java_test", "java_web_test_suite", "springboot", "springboot_test",
"java_proto_library", "java_lite_proto_library", "java_grpc_library"};

/**
* Parses a File, presumed to be a Bazel BUILD file, looking for indications that it contains Java rules.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public boolean isPackageDefault() {
* @return true if this instance represents a concrete label, false otherwise
*/
public boolean isConcrete() {
return !(this.localLabelPart.endsWith("*") ||
this.localLabelPart.endsWith("...") ||
this.localLabelPart.endsWith("all"));
return !(this.localLabelPart.endsWith("*")
|| this.localLabelPart.endsWith("...")
|| this.localLabelPart.endsWith("all"));
}

/**
Expand Down Expand Up @@ -258,9 +258,9 @@ public String toString() {
}

private static BazelLabel withRepositoryNameAndLocalLabelPart(String repositoryName, String localLabelPart) {
return repositoryName == null ?
new BazelLabel(localLabelPart) :
new BazelLabel("@" + repositoryName + "//" + localLabelPart);
return repositoryName == null
? new BazelLabel(localLabelPart)
: new BazelLabel("@" + repositoryName + "//" + localLabelPart);
}

private static String sanitizePackagePath(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
package com.salesforce.bazel.sdk.model;

import java.io.File;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

/**
* Model class for a Bazel Java package. It is a node in a tree of the hierarchy of packages. The root node in this tree
Expand All @@ -65,8 +68,8 @@ public class BazelPackageInfo implements BazelPackageLocation {

private BazelPackageInfo parent;
private final boolean isWorkspaceRoot;
protected final File workspaceRoot;
protected BazelPackageInfo workspaceRootNode;
private final File workspaceRoot;
private BazelPackageInfo workspaceRootNode;

public static final String WORKSPACE_FILENAME = "WORKSPACE";
public static final String WORKSPACE_FILENAME_ALT = "WORKSPACE.bazel";
Expand Down Expand Up @@ -314,20 +317,11 @@ public String getBazelPackageName() {
return computedPackageName;
}

// split the file system path by OS path separator
String[] pathElements = relativeWorkspacePath.split(File.separator);

// assemble the path elements into a proper Bazel package name
String name = "/";
for (String e : pathElements) {
if (e.isEmpty()) {
continue;
}
name = name + "/" + e;
}

// set computedPackageName only when done computing it, to avoid threading issues
computedPackageName = name;
computedPackageName = StreamSupport.stream(
Paths.get(relativeWorkspacePath).spliterator(),
false
).map(Object::toString).collect(Collectors.joining("/", "//", ""));

// and cache the last segment as well
getBazelPackageNameLastSegment();

Expand Down Expand Up @@ -436,18 +430,28 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
if (this == obj)
if (this == obj) {
return true;
if (obj == null)
}

if (obj == null) {
return false;
if (getClass() != obj.getClass())
}

if (getClass() != obj.getClass()) {
return false;
}

BazelPackageInfo other = (BazelPackageInfo) obj;

if (computedPackageName == null) {
if (other.computedPackageName != null)
if (other.computedPackageName != null) {
return false;
} else if (!computedPackageName.equals(other.computedPackageName))
}
} else if (!computedPackageName.equals(other.computedPackageName)) {
return false;
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ public interface BazelPackageLocation {
*
* e.g. "//projects/libs/apple"
*/
public String getBazelPackageName();
String getBazelPackageName();

/**
* Builds a list containing this node, plus all children (recursively)
*/
public List<BazelPackageLocation> gatherChildren();
List<BazelPackageLocation> gatherChildren();

/**
* Returns the targets configured for this Bazel Package, at import time.
*
* A null return value indicates that the user did not specify any specific targets.
*/
default public List<BazelLabel> getBazelTargets() {
default List<BazelLabel> getBazelTargets() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
* @since March 2020
*/
public class ProjectView {

private static final int INIT_INDENT = 3;

static String DIRECTORIES_SECTION = "directories:";
static String TARGETS_SECTION = "targets:";
static String DIRECTORIES_COMMENT = "# Add the directories you want added as source here";
static String INDENT = " ";
private static final String DIRECTORIES_SECTION = "directories:";
private static final String TARGETS_SECTION = "targets:";
private static final String DIRECTORIES_COMMENT = "# Add the directories you want added as source here";
private static final String INDENT = " ";

private final File rootWorkspaceDirectory;
private final Map<BazelPackageLocation, Integer> packageToLineNumber;
Expand Down Expand Up @@ -172,9 +174,9 @@ public boolean equals(Object other) {
if (!(other instanceof ProjectView)) {
return false;
}
ProjectView o = (ProjectView)other;
return packageToLineNumber.keySet().equals(o.packageToLineNumber.keySet()) &&
targetToLineNumber.keySet().equals(o.targetToLineNumber.keySet());
ProjectView o = (ProjectView) other;
return packageToLineNumber.keySet().equals(o.packageToLineNumber.keySet())
&& targetToLineNumber.keySet().equals(o.targetToLineNumber.keySet());
}

private List<BazelLabel> getTargetsForDirectory(String directory) {
Expand All @@ -193,12 +195,11 @@ private List<BazelLabel> getTargetsForDirectory(String directory) {

private static void initSections(List<BazelPackageLocation> packages, List<BazelLabel> targets,
Map<BazelPackageLocation, Integer> packageToLineNumber,
Map<BazelLabel, Integer> targetToLineNumber)
{
Map<BazelLabel, Integer> targetToLineNumber) {
// directories:
// # comment
// therefore:
int lineNumber = 3;
int lineNumber = INIT_INDENT;
for (BazelPackageLocation pack : packages) {
packageToLineNumber.put(pack, lineNumber);
lineNumber += 1;
Expand All @@ -213,8 +214,7 @@ private static void initSections(List<BazelPackageLocation> packages, List<Bazel

private static void parseSections(String content, File rootWorkspaceDirectory,
Map<BazelPackageLocation, Integer> packageToLineNumber,
Map<BazelLabel, Integer> targetToLineNumber)
{
Map<BazelLabel, Integer> targetToLineNumber) {
boolean withinDirectoriesSection = false;
boolean withinTargetsSection = false;
int lineNumber = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@
import java.util.Collections;
import java.util.HashSet;

public interface BazelConstants {

public final class BazelConstants {

private BazelConstants() {

}

/**
* The Bazel BUILD files BEF looks for.
*/
Collection<String> BUILD_FILE_NAMES =
public static final Collection<String> BUILD_FILE_NAMES =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
Expand All @@ -52,7 +56,7 @@ public interface BazelConstants {
/**
* The targets configured by default for each imported Bazel package.
*/
Collection<String> DEFAULT_PACKAGE_TARGETS =
public static final Collection<String> DEFAULT_PACKAGE_TARGETS =
Collections.unmodifiableSet(
new HashSet<>(
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
/**
* Static utilities.
*/
public class BazelPathHelper {
public final class BazelPathHelper {

private BazelPathHelper() {

}

/**
* Resolve softlinks and other abstractions in the workspace paths.
Expand Down
Loading

0 comments on commit 75a0485

Please sign in to comment.