Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
BazelExecutableFinder now extends CacheableExecutableFinder
Browse files Browse the repository at this point in the history
  • Loading branch information
Steve Billings committed Jan 16, 2019
1 parent 0e5973c commit 5045d8d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public MavenExecutableFinder mavenExecutableFinder() {

@Bean
public BazelExecutableFinder bazelExecutableFinder() {
return new BazelExecutableFinder(executableRunner, executableFinder, detectConfiguration);
return new BazelExecutableFinder(executableRunner, directoryManager, executableFinder, detectConfiguration);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,55 @@
*/
package com.blackducksoftware.integration.hub.detect.tool.bazel;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.blackducksoftware.integration.hub.detect.configuration.DetectConfiguration;
import com.blackducksoftware.integration.hub.detect.configuration.DetectProperty;
import com.blackducksoftware.integration.hub.detect.configuration.PropertyAuthority;
import com.blackducksoftware.integration.hub.detect.detector.DetectorEnvironment;
import com.blackducksoftware.integration.hub.detect.type.ExecutableType;
import com.blackducksoftware.integration.hub.detect.detector.DetectorException;
import com.blackducksoftware.integration.hub.detect.util.executable.CacheableExecutableFinder;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableFinder;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunnerException;
import com.blackducksoftware.integration.hub.detect.workflow.file.DirectoryManager;

public class BazelExecutableFinder {
public class BazelExecutableFinder extends CacheableExecutableFinder {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private static final String BAZEL_VERSION_SUBCOMMAND = "version";
private final ExecutableRunner executableRunner;
private final ExecutableFinder executableFinder;
private final DetectConfiguration detectConfiguration;

private boolean hasLookedForSystemBazel = false;
private String resolvedBazel = null;

public BazelExecutableFinder(final ExecutableRunner executableRunner, final ExecutableFinder executableFinder, final DetectConfiguration detectConfiguration) {
public BazelExecutableFinder(final ExecutableRunner executableRunner, final DirectoryManager directoryManager, final ExecutableFinder executableFinder, final DetectConfiguration detectConfiguration) {
super(directoryManager, executableFinder, detectConfiguration);
this.executableRunner = executableRunner;
this.executableFinder = executableFinder;
this.detectConfiguration = detectConfiguration;
}

public String findBazel(final DetectorEnvironment environment) {
if (!hasLookedForSystemBazel) {
final String userProvidedBazelPath = detectConfiguration.getProperty(DetectProperty.DETECT_BAZEL_PATH, PropertyAuthority.None);
resolvedBazel = executableFinder.getExecutablePathOrOverride(ExecutableType.BAZEL, true, environment.getDirectory(), userProvidedBazelPath);
hasLookedForSystemBazel = true;
}

final ExecutableOutput bazelQueryDepsRecursiveOutput;
final boolean resolvedPreviously = isAlreadyFound(CacheableExecutableType.BAZEL);
String resolvedBazel = null;
try {
bazelQueryDepsRecursiveOutput = executableRunner.executeQuietly(environment.getDirectory(), resolvedBazel, BAZEL_VERSION_SUBCOMMAND);
int returnCode = bazelQueryDepsRecursiveOutput.getReturnCode();
logger.trace(String.format("Bazel version returned %d; output: %s", returnCode, bazelQueryDepsRecursiveOutput.getStandardOutput()));
} catch (ExecutableRunnerException e) {
logger.debug(String.format("Bazel version threw exception: %s", e.getMessage()));
resolvedBazel = null;
final File bazelExeFile = getExecutable(CacheableExecutableType.BAZEL);
if (bazelExeFile == null) {
logger.debug("Unable to locate Bazel executable");
return null;
}
resolvedBazel = bazelExeFile.getAbsolutePath();
} catch (DetectorException e) {
logger.debug(String.format("Unable to locate Bazel executable: %s", e.getMessage()));
return null;
}
if (!resolvedPreviously) {
final ExecutableOutput bazelQueryDepsRecursiveOutput;
try {
bazelQueryDepsRecursiveOutput = executableRunner.executeQuietly(environment.getDirectory(), resolvedBazel, BAZEL_VERSION_SUBCOMMAND);
int returnCode = bazelQueryDepsRecursiveOutput.getReturnCode();
logger.trace(String.format("Bazel version returned %d; output: %s", returnCode, bazelQueryDepsRecursiveOutput.getStandardOutput()));
} catch (ExecutableRunnerException e) {
logger.debug(String.format("Bazel version threw exception: %s", e.getMessage()));
resolvedBazel = null;
}
}
return resolvedBazel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public enum CacheableExecutableType {
REBAR3,
PEAR,
YARN,
JAVA
JAVA,
BAZEL
}

private final ExecutableFinder executableFinder;
Expand Down Expand Up @@ -107,10 +108,16 @@ public StandardExecutableInfo createInfo(final CacheableExecutableType type) {
return new StandardExecutableInfo(ExecutableType.YARN, detectConfiguration.getProperty(DetectProperty.DETECT_YARN_PATH, PropertyAuthority.None));
case JAVA:
return new StandardExecutableInfo(ExecutableType.JAVA, detectConfiguration.getProperty(DetectProperty.DETECT_JAVA_PATH, PropertyAuthority.None));
case BAZEL:
return new StandardExecutableInfo(ExecutableType.BAZEL, detectConfiguration.getProperty(DetectProperty.DETECT_BAZEL_PATH, PropertyAuthority.None));
}
return null;
}

protected boolean isAlreadyFound(final CacheableExecutableType executableType) {
return alreadyFound.containsKey(executableType);
}

private class StandardExecutableInfo {
public ExecutableType detectExecutableType;
public String override;
Expand Down

0 comments on commit 5045d8d

Please sign in to comment.