Search in sources :

Example 1 with NugetInspector

use of com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector in project synopsys-detect by blackducksoftware.

the class NugetInspectorExtractor method executeTarget.

private NugetTargetResult executeTarget(NugetInspector inspector, File targetFile, File outputDirectory, NugetInspectorOptions nugetInspectorOptions) throws ExecutableRunnerException, IOException, DetectableException {
    if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
        throw new DetectableException(String.format("Executing the nuget inspector failed, could not create output directory: %s", outputDirectory));
    }
    ExecutableOutput executableOutput = inspector.execute(outputDirectory, targetFile, outputDirectory, nugetInspectorOptions);
    if (executableOutput.getReturnCode() != 0) {
        throw new DetectableException(String.format("Executing the nuget inspector failed: %s", executableOutput.getReturnCode()));
    }
    List<File> dependencyNodeFiles = fileFinder.findFiles(outputDirectory, INSPECTOR_OUTPUT_PATTERN);
    List<NugetParseResult> parseResults = new ArrayList<>();
    if (dependencyNodeFiles != null) {
        for (File dependencyNodeFile : dependencyNodeFiles) {
            String text = FileUtils.readFileToString(dependencyNodeFile, StandardCharsets.UTF_8);
            NugetParseResult result = nugetInspectorParser.createCodeLocation(text);
            parseResults.add(result);
        }
    }
    NugetTargetResult targetResult = new NugetTargetResult();
    targetResult.codeLocations = parseResults.stream().flatMap(it -> it.getCodeLocations().stream()).collect(Collectors.toList());
    targetResult.nameVersion = parseResults.stream().filter(it -> StringUtils.isNotBlank(it.getProjectName())).map(it -> new NameVersion(it.getProjectName(), it.getProjectVersion())).findFirst().orElse(null);
    return targetResult;
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) NugetInspectorOptions(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorOptions) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) NugetInspector(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector) Logger(org.slf4j.Logger) NugetInspectorParser(com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Optional(java.util.Optional) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) File(java.io.File) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException)

Example 2 with NugetInspector

use of com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector in project synopsys-detect by blackducksoftware.

the class NugetInspectorExtractor method extract.

public Extraction extract(List<File> targets, File outputDirectory, NugetInspector inspector, NugetInspectorOptions nugetInspectorOptions) {
    try {
        List<NugetTargetResult> results = new ArrayList<>();
        for (int i = 0; i < targets.size(); i++) {
            File targetDirectory = new File(outputDirectory, "inspection-" + i);
            results.add(executeTarget(inspector, targets.get(i), targetDirectory, nugetInspectorOptions));
        }
        List<CodeLocation> codeLocations = results.stream().flatMap(it -> it.codeLocations.stream()).collect(Collectors.toList());
        Map<File, CodeLocation> codeLocationsBySource = new HashMap<>();
        DependencyGraphCombiner combiner = new DependencyGraphCombiner();
        codeLocations.forEach(codeLocation -> {
            File sourcePathFile = codeLocation.getSourcePath().orElse(null);
            if (codeLocationsBySource.containsKey(sourcePathFile)) {
                logger.debug("Combined code location for: " + sourcePathFile);
                CodeLocation destination = codeLocationsBySource.get(sourcePathFile);
                combiner.addGraphAsChildrenToRoot((MutableDependencyGraph) destination.getDependencyGraph(), codeLocation.getDependencyGraph());
            } else {
                codeLocationsBySource.put(sourcePathFile, codeLocation);
            }
        });
        Optional<NameVersion> nameVersion = results.stream().filter(it -> it.nameVersion != null).map(it -> it.nameVersion).findFirst();
        List<CodeLocation> uniqueCodeLocations = new ArrayList<>(codeLocationsBySource.values());
        return new Extraction.Builder().success(uniqueCodeLocations).nameVersionIfPresent(nameVersion).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : Extraction(com.synopsys.integration.detectable.extraction.Extraction) MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) NugetParseResult(com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) ArrayList(java.util.ArrayList) NameVersion(com.synopsys.integration.util.NameVersion) NugetInspectorOptions(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorOptions) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) NugetInspector(com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector) Logger(org.slf4j.Logger) NugetInspectorParser(com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) Optional(java.util.Optional) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) IOException(java.io.IOException) DetectableException(com.synopsys.integration.detectable.detectable.exception.DetectableException) Extraction(com.synopsys.integration.detectable.extraction.Extraction) File(java.io.File) DependencyGraphCombiner(com.synopsys.integration.bdio.graph.DependencyGraphCombiner)

Aggregations

DependencyGraphCombiner (com.synopsys.integration.bdio.graph.DependencyGraphCombiner)2 MutableDependencyGraph (com.synopsys.integration.bdio.graph.MutableDependencyGraph)2 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)2 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 DetectableException (com.synopsys.integration.detectable.detectable.exception.DetectableException)2 NugetInspector (com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspector)2 NugetInspectorOptions (com.synopsys.integration.detectable.detectable.inspector.nuget.NugetInspectorOptions)2 NugetInspectorParser (com.synopsys.integration.detectable.detectables.nuget.parse.NugetInspectorParser)2 NugetParseResult (com.synopsys.integration.detectable.detectables.nuget.parse.NugetParseResult)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)2 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)2 NameVersion (com.synopsys.integration.util.NameVersion)2 File (java.io.File)2 IOException (java.io.IOException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2