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

Commit

Permalink
(#83) Adjust parser to use universal bazelproject file format
Browse files Browse the repository at this point in the history
  • Loading branch information
PMitrafanau committed Mar 16, 2021
1 parent 22040c8 commit 0113ce8
Show file tree
Hide file tree
Showing 19 changed files with 1,674 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
# limitations under the License.
#


# Aspect for Bazel Eclipse Feature, taken from an early version of intellij_info.bzl
# TODO upgrade this to their latest work

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
import com.salesforce.b2eclipse.model.AspectPackageInfo;
import com.salesforce.b2eclipse.model.AspectPackageInfos;
import com.salesforce.b2eclipse.model.BazelLabel;
import com.salesforce.b2eclipse.model.BazelPackageInfo;
import com.salesforce.bazel.sdk.model.BazelPackageInfo;
import com.salesforce.b2eclipse.runtime.api.ResourceHelper;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import com.google.common.graph.Traverser;
import com.salesforce.b2eclipse.model.AspectPackageInfo;
import com.salesforce.b2eclipse.model.AspectPackageInfos;
import com.salesforce.b2eclipse.model.BazelPackageInfo;
import com.salesforce.bazel.sdk.model.BazelPackageInfo;

final class ImportOrderResolver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
import com.salesforce.b2eclipse.BazelJdtPlugin;
import com.salesforce.b2eclipse.command.BazelCommandManager;
import com.salesforce.b2eclipse.command.BazelWorkspaceCommandRunner;
import com.salesforce.b2eclipse.model.BazelPackageInfo;
import com.salesforce.bazel.sdk.model.BazelPackageInfo;
import com.salesforce.b2eclipse.runtime.impl.EclipseWorkProgressMonitor;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,34 @@
package com.salesforce.b2eclipse.managers;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.ls.core.internal.AbstractProjectImporter;

import com.salesforce.b2eclipse.BazelJdtPlugin;
import com.salesforce.b2eclipse.abstractions.WorkProgressMonitor;
import com.salesforce.b2eclipse.command.BazelCommandManager;
import com.salesforce.b2eclipse.config.BazelEclipseProjectFactory;
import com.salesforce.b2eclipse.importer.BazelProjectImportScanner;
import com.salesforce.b2eclipse.model.BazelPackageInfo;
import com.salesforce.bazel.sdk.model.BazelPackageInfo;
import com.salesforce.b2eclipse.runtime.impl.EclipseWorkProgressMonitor;
import com.salesforce.bazel.sdk.project.ProjectView;
import com.salesforce.bazel.sdk.workspace.BazelWorkspaceScanner;

@SuppressWarnings("restriction")
public final class BazelProjectImporter extends AbstractProjectImporter {

private static final String WORKSPACE_FILE_NAME = "WORKSPACE";

private static final String BAZELPROJECT_FILE_NAME = ".bazelproject";

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

@Override
public void importToWorkspace(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
BazelCommandManager bazelCommandManager = BazelJdtPlugin.getBazelCommandManager();
BazelProjectImportScanner scanner = new BazelProjectImportScanner(bazelCommandManager, rootFolder);

BazelPackageInfo workspaceRootPackage = scanner.getProjects(monitor);

try {
BazelWorkspaceScanner workspaceScanner = new BazelWorkspaceScanner();
BazelPackageInfo workspaceRootPackage = workspaceScanner.getPackages(rootFolder);

if (workspaceRootPackage == null) {
throw new IllegalArgumentException();
}
List<BazelPackageInfo> bazelPackagesToImport =
workspaceRootPackage.getChildPackageInfos().stream().collect(Collectors.toList());

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());
}

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)));
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* Copyright (c) 2019, Salesforce.com, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2016 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
package com.salesforce.bazel.sdk.logging;

// we will revisit this later, see https://github.com/salesforce/bazel-eclipse/issues/10
// import org.slf4j.LoggerFactory;

/**
*
* Default facade that crudely logs to stdout/stderr.
*
*/
public class BasicLoggerFacade extends LoggerFacade {

@Override
public void error(Class<?> from, String message, Object... args) {
// LoggerFactory.getLogger(from).error(message, args);
System.err.println(formatMsg(from, message, args));
}

@Override
public void error(Class<?> from, String message, Throwable exception, Object... args) {
//LoggerFactory.getLogger(from).error(message, exception, args);
System.err.println(formatMsg(from, message, args));
}

@Override
public void warn(Class<?> from, String message, Object... args) {
// LoggerFactory.getLogger(from).warn(message, args);
System.out.println(formatMsg(from, message, args));
}

@Override
public void info(Class<?> from, String message, Object... args) {
// LoggerFactory.getLogger(from).info(message, args);
System.out.println(formatMsg(from, message, args));
}

@Override
public void debug(Class<?> from, String message, Object... args) {
// LoggerFactory.getLogger(from).debug(message, args);
// System.out.println(formatMsg(from, message, args));
}

private String formatMsg(Class<?> from, String message, Object... args) {
return "[" + from.getName() + "] " + message;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2019, Salesforce.com, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2016 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
package com.salesforce.bazel.sdk.logging;

/**
* Helper to log messages. Doesn't cache anything but class name for common logging frameworks. This allows the
* LoggerFacade to be changed and without having to constantly give the class.
*
* This is the preferred way to log.
*
* @author Blaine Buxton
*
*/
public class LogHelper {
final Class<?> from;

public static LogHelper log(Class<?> from) {
return new LogHelper(from);
}

private LogHelper(Class<?> from) {
this.from = from;
}

public void error(String message, Object... args) {
getFacade().error(from, message, args);
}

public void error(String message, Throwable exception, Object... args) {
getFacade().error(from, message, exception, args);
}

public void warn(String message, Object... args) {
getFacade().warn(from, message, args);
}

public void info(String message, Object... args) {
getFacade().info(from, message, args);
}

public void debug(String message, Object... args) {
getFacade().debug(from, message, args);
}

private LoggerFacade getFacade() {
return LoggerFacade.instance();
}
}
Loading

0 comments on commit 0113ce8

Please sign in to comment.