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

Commit

Permalink
Merge 9a49d4a into 2e3cc15
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrichard91 committed Jul 16, 2018
2 parents 2e3cc15 + 9a49d4a commit c1730ad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@
*/
package com.blackducksoftware.integration.hub.detect.bomtool.npm;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -60,15 +57,15 @@ public NpmCliDependencyFinder(final ExternalIdFactory externalIdFactory) {
this.externalIdFactory = externalIdFactory;
}

public NpmParseResult generateCodeLocation(final BomToolType bomToolType, final String sourcePath, final File npmLsOutputFile) throws IOException {
if (npmLsOutputFile == null || npmLsOutputFile.length() <= 0) {
public NpmParseResult generateCodeLocation(final BomToolType bomToolType, final String sourcePath, final String npmLsOutput) {
if (StringUtils.isBlank(npmLsOutput)) {
logger.error("Ran into an issue creating and writing to file");
return null;
}

logger.info("Generating results from npm ls -json");

return convertNpmJsonFileToCodeLocation(bomToolType, sourcePath, FileUtils.readFileToString(npmLsOutputFile, StandardCharsets.UTF_8));
return convertNpmJsonFileToCodeLocation(bomToolType, sourcePath, npmLsOutput);
}

NpmParseResult convertNpmJsonFileToCodeLocation(final BomToolType bomToolType, final String sourcePath, final String npmLsOutput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,6 +38,7 @@
import com.blackducksoftware.integration.hub.detect.configuration.DetectProperty;
import com.blackducksoftware.integration.hub.detect.util.DetectFileManager;
import com.blackducksoftware.integration.hub.detect.util.executable.Executable;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableOutput;
import com.blackducksoftware.integration.hub.detect.util.executable.ExecutableRunner;
import com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction;

Expand All @@ -62,8 +62,6 @@ public NpmCliExtractor(final ExecutableRunner executableRunner, final DetectFile

public Extraction extract(final BomToolType bomToolType, final File directory, final String npmExe, final ExtractionId extractionId) {
final File outputDirectory = detectFileManager.getOutputDirectory("Npm", extractionId);
final File npmLsOutputFile = detectFileManager.getOutputFile(outputDirectory, NpmCliExtractor.OUTPUT_FILE);
final File npmLsErrorFile = detectFileManager.getOutputFile(outputDirectory, NpmCliExtractor.ERROR_FILE);

final boolean includeDevDeps = detectConfigWrapper.getBooleanProperty(DetectProperty.DETECT_NPM_INCLUDE_DEV_DEPENDENCIES);
final List<String> exeArgs = Arrays.asList("ls", "-json");
Expand All @@ -72,48 +70,38 @@ public Extraction extract(final BomToolType bomToolType, final File directory, f
}

final Executable npmLsExe = new Executable(directory, npmExe, exeArgs);
ExecutableOutput executableOutput;
try {
executableRunner.executeToFile(npmLsExe, npmLsOutputFile, npmLsErrorFile);
executableOutput = executableRunner.execute(npmLsExe);
} catch (final Exception e) {
return new Extraction.Builder().exception(e).build();
}

if (npmLsOutputFile.length() > 0) {
if (npmLsErrorFile.length() > 0) {
String standardOutput = executableOutput.getStandardOutput();
String errorOutput = executableOutput.getErrorOutput();
if (StringUtils.isNotBlank(standardOutput)) {
if (StringUtils.isNotBlank(errorOutput)) {
logger.debug("Error when running npm ls -json command");
printFileToDebug(npmLsErrorFile);
logger.debug(errorOutput);
return new Extraction.Builder().failure("Npm returned no output after runnin npm ls.").build();
}
logger.debug("Parsing npm ls file.");
printFileToDebug(npmLsOutputFile);
logger.debug(standardOutput);
try {
final NpmParseResult result = npmCliDependencyFinder.generateCodeLocation(bomToolType, directory.getCanonicalPath(), npmLsOutputFile);
final NpmParseResult result = npmCliDependencyFinder.generateCodeLocation(bomToolType, directory.getCanonicalPath(), standardOutput);
return new Extraction.Builder().success(result.codeLocation).projectName(result.projectName).projectVersion(result.projectVersion).build();
} catch (final IOException e) {
return new Extraction.Builder().exception(e).build();
}

} else {
if (npmLsErrorFile.length() > 0) {
if (StringUtils.isNotBlank(errorOutput)) {
logger.error("Error when running npm ls -json command");
printFileToDebug(npmLsErrorFile);
logger.debug(errorOutput);
} else {
logger.warn("Nothing returned from npm ls -json command");
}
return new Extraction.Builder().failure("Npm returned error after running npm ls.").build();
}
}

void printFileToDebug(final File errorFile) {
String text = "";
try {
for (final String line : Files.readAllLines(errorFile.toPath(), StandardCharsets.UTF_8)) {
text += line + System.lineSeparator();
}
} catch (final IOException e) {
logger.debug("Failed to read NPM error file.");
}
logger.debug(text);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,7 @@ public ExecutableOutput execute(final Executable executable) throws ExecutableRu
public ExecutableOutput executeQuietly(final Executable executable) throws ExecutableRunnerException {
return runExecutable(executable, logger::debug, logger::trace);
}

public void executeToFile(final String executablePath, final File standardOutputFile, final File errorOutputFile, final String... args) throws ExecutableRunnerException {
final Executable executable = new Executable(new File(detectConfigWrapper.getProperty(DetectProperty.DETECT_SOURCE_PATH)), executablePath, Arrays.asList(args));
executeToFile(executable, standardOutputFile, errorOutputFile);
}

public void executeToFile(final Executable executable, final File standardOutputFile, final File errorOutputFile) throws ExecutableRunnerException {
logger.debug(String.format("Running executable >%s", executable.getMaskedExecutableDescription()));
try {
final ProcessBuilder processBuilder = executable.createProcessBuilder().redirectOutput(standardOutputFile).redirectError(errorOutputFile);
final Process process = processBuilder.start();
process.waitFor();
} catch (final Exception e) {
throw new ExecutableRunnerException(e);
}
}


private ExecutableOutput runExecutable(final Executable executable, final Consumer<String> standardLoggingMethod, final Consumer<String> traceLoggingMethod) throws ExecutableRunnerException {
standardLoggingMethod.accept(String.format("Running executable >%s", executable.getMaskedExecutableDescription()));
try {
Expand Down

0 comments on commit c1730ad

Please sign in to comment.