Search in sources :

Example 86 with CodeLocation

use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.

the class PubDepsExtractor method extract.

public Extraction extract(File directory, @Nullable ExecutableTarget dartExe, @Nullable ExecutableTarget flutterExe, DartPubDepsDetectableOptions dartPubDepsDetectableOptions, File pubSpecYamlFile) {
    try {
        toolVersionLogger.log(directory, dartExe);
        toolVersionLogger.log(directory, flutterExe);
        List<String> pubDepsCommand = new ArrayList<>();
        pubDepsCommand.add("pub");
        pubDepsCommand.add("deps");
        if (dartPubDepsDetectableOptions.getDependencyTypeFilter().shouldExclude(DartPubDependencyType.DEV)) {
            pubDepsCommand.add("--no-dev");
        }
        ExecutableOutput pubDepsOutput = null;
        if (dartExe != null) {
            pubDepsOutput = runPubDepsCommand(directory, dartExe, pubDepsCommand);
        }
        if (pubDepsOutput == null || pubDepsOutput.getReturnCode() != 0) {
            if (flutterExe == null && dartExe != null) {
                return new Extraction.Builder().failure(String.format("An error occurred trying to run %s %s", dartExe.toCommand(), String.join(" ", pubDepsCommand))).build();
            } else {
                // If command does not work with Dart, it could be because at least one of the packages requires Flutter
                logger.debug("Running dart pub deps was not successful.  Going to try running flutter pub deps.");
                pubDepsCommand.add(0, "--no-version-check");
                pubDepsOutput = runPubDepsCommand(directory, flutterExe, pubDepsCommand);
            }
        }
        Optional<NameVersion> nameVersion = Optional.empty();
        if (pubSpecYamlFile != null) {
            List<String> pubSpecYamlLines = Files.readAllLines(pubSpecYamlFile.toPath(), StandardCharsets.UTF_8);
            nameVersion = nameVersionParser.parseNameVersion(pubSpecYamlLines);
        }
        DependencyGraph dependencyGraph = pubDepsParser.parse(pubDepsOutput.getStandardOutputAsList());
        CodeLocation codeLocation = new CodeLocation(dependencyGraph);
        return new Extraction.Builder().success(codeLocation).nameVersionIfPresent(nameVersion).build();
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) ArrayList(java.util.ArrayList) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Extraction(com.synopsys.integration.detectable.extraction.Extraction) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException)

Example 87 with CodeLocation

use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.

the class DockerExtractor method findCodeLocations.

private Extraction.Builder findCodeLocations(File directoryToSearch, File directory, @Nullable String dockerInspectorMessage) {
    File bdioFile = fileFinder.findFile(directoryToSearch, DEPENDENCIES_PATTERN);
    if (bdioFile != null) {
        SimpleBdioDocument simpleBdioDocument = null;
        try (InputStream dockerOutputInputStream = new FileInputStream(bdioFile);
            BdioReader bdioReader = new BdioReader(gson, dockerOutputInputStream)) {
            simpleBdioDocument = bdioReader.readSimpleBdioDocument();
        } catch (Exception e) {
            return new Extraction.Builder().exception(e);
        }
        if (simpleBdioDocument != null) {
            String projectName = simpleBdioDocument.getProject().name;
            String projectVersionName = simpleBdioDocument.getProject().version;
            // TODO ejk - update this when project external id is not req'd anymore
            Forge dockerForge = new Forge(BdioId.BDIO_ID_SEPARATOR, simpleBdioDocument.getProject().bdioExternalIdentifier.forge);
            String externalIdPath = simpleBdioDocument.getProject().bdioExternalIdentifier.externalId;
            ExternalId projectExternalId = externalIdFactory.createPathExternalId(dockerForge, externalIdPath);
            ProjectDependency projectDependency = new ProjectDependency(projectName, projectVersionName, projectExternalId);
            DependencyGraph dependencyGraph = bdioTransformer.transformToDependencyGraph(projectDependency, simpleBdioDocument.getProject(), simpleBdioDocument.getComponents());
            CodeLocation detectCodeLocation = new CodeLocation(dependencyGraph, projectExternalId);
            return new Extraction.Builder().success(detectCodeLocation).projectName(projectName).projectVersion(projectVersionName);
        }
    }
    logger.error("Docker Inspector returned no BDIO files");
    String dockerInspectorMsgSuffix = Optional.ofNullable(dockerInspectorMessage).filter(StringUtils::isNotBlank).map(s -> "; Docker Inspector message: " + s).orElse("");
    return new Extraction.Builder().failure("No files found matching pattern [" + DEPENDENCIES_PATTERN + "]. Expected docker-inspector to produce file in " + directory.toString() + dockerInspectorMsgSuffix);
}
Also used : Arrays(java.util.Arrays) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) IntegrationException(com.synopsys.integration.exception.IntegrationException) Forge(com.synopsys.integration.bdio.model.Forge) Extraction(com.synopsys.integration.detectable.extraction.Extraction) 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) ProjectDependency(com.synopsys.integration.bdio.model.dependency.ProjectDependency) ExecutableTarget(com.synopsys.integration.detectable.ExecutableTarget) DockerInspectorResultsFileParser(com.synopsys.integration.detectable.detectables.docker.parser.DockerInspectorResultsFileParser) DockerInspectorResults(com.synopsys.integration.detectable.detectables.docker.model.DockerInspectorResults) Gson(com.google.gson.Gson) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) Map(java.util.Map) BdioReader(com.synopsys.integration.bdio.BdioReader) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) BdioTransformer(com.synopsys.integration.bdio.BdioTransformer) Logger(org.slf4j.Logger) SimpleBdioDocument(com.synopsys.integration.bdio.model.SimpleBdioDocument) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) ExecutableOutput(com.synopsys.integration.executable.ExecutableOutput) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) BdioId(com.synopsys.integration.bdio.model.BdioId) Nullable(org.jetbrains.annotations.Nullable) Executable(com.synopsys.integration.executable.Executable) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectableExecutableRunner(com.synopsys.integration.detectable.detectable.executable.DetectableExecutableRunner) Optional(java.util.Optional) ExecutableUtils(com.synopsys.integration.detectable.ExecutableUtils) ExtractionMetadata(com.synopsys.integration.detectable.extraction.ExtractionMetadata) InputStream(java.io.InputStream) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) BdioReader(com.synopsys.integration.bdio.BdioReader) Forge(com.synopsys.integration.bdio.model.Forge) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) ProjectDependency(com.synopsys.integration.bdio.model.dependency.ProjectDependency) FileInputStream(java.io.FileInputStream) IntegrationException(com.synopsys.integration.exception.IntegrationException) ExecutableRunnerException(com.synopsys.integration.executable.ExecutableRunnerException) IOException(java.io.IOException) SimpleBdioDocument(com.synopsys.integration.bdio.model.SimpleBdioDocument) StringUtils(org.apache.commons.lang3.StringUtils) Extraction(com.synopsys.integration.detectable.extraction.Extraction) File(java.io.File)

Example 88 with CodeLocation

use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.

the class GoDepExtractor method extract.

public Extraction extract(InputStream goLockInputStream) {
    DependencyGraph graph = goLockParser.parseDepLock(goLockInputStream);
    CodeLocation codeLocation = new CodeLocation(graph);
    return new Extraction.Builder().success(codeLocation).build();
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph)

Example 89 with CodeLocation

use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.

the class DetectorToolTest method createExtractionBuilder.

private Extraction.Builder createExtractionBuilder() {
    File relevantFile = new File("go.mod");
    DependencyGraph dependencyGraph = Mockito.mock(DependencyGraph.class);
    CodeLocation codeLocation = new CodeLocation(dependencyGraph, relevantFile);
    Extraction.Builder builder = new Extraction.Builder();
    return builder.relevantFiles(relevantFile).codeLocations(codeLocation).projectName("test-project").projectVersion("1.0").unrecognizedPaths(Collections.emptyList());
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) DetectorRuleSetBuilder(com.synopsys.integration.detector.rule.DetectorRuleSetBuilder) DetectorRuleBuilder(com.synopsys.integration.detector.rule.DetectorRuleBuilder) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) Extraction(com.synopsys.integration.detectable.extraction.Extraction) File(java.io.File)

Example 90 with CodeLocation

use of com.synopsys.integration.detectable.detectable.codelocation.CodeLocation in project synopsys-detect by blackducksoftware.

the class YarnTransformerTest method doesntThrowOnMissingExternalId.

@Test
void doesntThrowOnMissingExternalId() throws MissingExternalIdException {
    // Ensure components not defined in the graph doesn't cause an exception to be thrown. See IDETECT-1974.
    PackageJson rawPackageJson = new PackageJson();
    rawPackageJson.dependencies = new HashMap<>();
    rawPackageJson.dependencies.put("foo", "fooFuzzyVersion-1.0");
    NullSafePackageJson packageJson = new NullSafePackageJson(rawPackageJson);
    List<YarnLockEntryId> validYarnLockEntryIds = Collections.singletonList(new YarnLockEntryId("foo", "fooFuzzyVersion-1.0"));
    List<YarnLockDependency> validYarnLockDependencies = Collections.singletonList(new YarnLockDependency("yarn", "^1.22.4", false));
    List<YarnLockEntry> yarnLockEntries = Collections.singletonList(new YarnLockEntry(false, validYarnLockEntryIds, "1.0", validYarnLockDependencies));
    YarnLock yarnLock = new YarnLock(null, true, yarnLockEntries);
    YarnLockResult yarnLockResult = new YarnLockResult(packageJson, YarnWorkspaces.EMPTY, yarnLock);
    // This should not throw an exception.
    List<CodeLocation> codeLocations = createTransformer().generateCodeLocations(yarnLockResult, new ArrayList<>(0), ExcludedIncludedWildcardFilter.EMPTY);
    // Sanity check.
    assertEquals(1, codeLocations.size());
    CodeLocation codeLocation = codeLocations.get(0);
    DependencyGraph dependencyGraph = codeLocation.getDependencyGraph();
    Assertions.assertNotNull(dependencyGraph, "The dependency graph should not be null.");
    assertEquals(1, dependencyGraph.getRootDependencies().size(), "Only 'foo:1.0' should appear in the graph.");
    ExternalId fooExternalId = externalIdFactory.createNameVersionExternalId(Forge.NPMJS, "foo", "1.0");
    assertTrue(dependencyGraph.hasDependency(fooExternalId), "Missing the only expected dependency.");
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockResult(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult) YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) Test(org.junit.jupiter.api.Test) UnitTest(com.synopsys.integration.detectable.annotations.UnitTest)

Aggregations

CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)104 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)60 Extraction (com.synopsys.integration.detectable.extraction.Extraction)41 File (java.io.File)24 ArrayList (java.util.ArrayList)23 Test (org.junit.jupiter.api.Test)22 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)21 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)20 NameVersion (com.synopsys.integration.util.NameVersion)19 IOException (java.io.IOException)19 BasicDependencyGraph (com.synopsys.integration.bdio.graph.BasicDependencyGraph)16 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)16 List (java.util.List)14 UnitTest (com.synopsys.integration.detectable.annotations.UnitTest)13 ExecutableOutput (com.synopsys.integration.executable.ExecutableOutput)11 ExecutableRunnerException (com.synopsys.integration.executable.ExecutableRunnerException)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10