Search in sources :

Example 1 with LazyId

use of com.synopsys.integration.bdio.graph.builder.LazyId in project synopsys-detect by blackducksoftware.

the class PackratLockFileParser method parseProjectDependencies.

public DependencyGraph parseProjectDependencies(List<String> packratLockContents) throws MissingExternalIdException {
    LazyExternalIdDependencyGraphBuilder graphBuilder = new LazyExternalIdDependencyGraphBuilder();
    LazyId currentParent = null;
    String name = null;
    boolean requiresSection = false;
    for (String line : packratLockContents) {
        if (StringUtils.isBlank(line)) {
            currentParent = null;
            name = null;
            requiresSection = false;
            continue;
        }
        if (!(line.startsWith(PACKAGE_TOKEN) || line.startsWith(VERSION_TOKEN) || line.startsWith(REQUIRES_TOKEN) || line.startsWith(INDENTATION_TOKEN))) {
            continue;
        }
        if (line.startsWith(PACKAGE_TOKEN)) {
            name = getValueFromLine(line);
            currentParent = LazyId.fromName(name);
            graphBuilder.setDependencyName(currentParent, name);
            graphBuilder.addChildToRoot(currentParent);
            requiresSection = false;
        } else if (line.startsWith(VERSION_TOKEN)) {
            String version = getValueFromLine(line);
            graphBuilder.setDependencyVersion(currentParent, version);
            LazyId realId = LazyId.fromNameAndVersion(name, version);
            ExternalId externalId = this.externalIdFactory.createNameVersionExternalId(Forge.CRAN, name, version);
            graphBuilder.setDependencyAsAlias(realId, currentParent);
            graphBuilder.setDependencyInfo(realId, name, version, externalId);
            currentParent = realId;
        } else if (line.startsWith(REQUIRES_TOKEN)) {
            requiresSection = true;
            String cleanLine = getValueFromLine(line);
            List<LazyId> children = getChildrenNames(cleanLine).stream().map(LazyId::fromName).collect(Collectors.toList());
            graphBuilder.addParentWithChildren(currentParent, children);
        } else if (requiresSection && line.startsWith(INDENTATION_TOKEN)) {
            List<LazyId> children = getChildrenNames(line).stream().map(LazyId::fromName).collect(Collectors.toList());
            graphBuilder.addParentWithChildren(currentParent, children);
        }
    }
    return graphBuilder.build();
}
Also used : LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) List(java.util.List) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 2 with LazyId

use of com.synopsys.integration.bdio.graph.builder.LazyId in project synopsys-detect by blackducksoftware.

the class PackagistParser method getDependencyGraphFromProject.

// TODO: Why are we dealing with JsonObjects rather than Gson straight to classes? Is this to avoid TypeAdapters? If so... smh JM-01/2022
public PackagistParseResult getDependencyGraphFromProject(String composerJsonText, String composerLockText) throws MissingExternalIdException {
    LazyExternalIdDependencyGraphBuilder builder = new LazyExternalIdDependencyGraphBuilder();
    JsonObject composerJsonObject = new JsonParser().parse(composerJsonText).getAsJsonObject();
    NameVersion projectNameVersion = parseNameVersionFromJson(composerJsonObject);
    JsonObject composerLockObject = new JsonParser().parse(composerLockText).getAsJsonObject();
    List<PackagistPackage> models = convertJsonToModel(composerLockObject);
    List<NameVersion> rootPackages = parseDependencies(composerJsonObject);
    models.forEach(it -> {
        ExternalId id = externalIdFactory.createNameVersionExternalId(Forge.PACKAGIST, it.getNameVersion().getName(), it.getNameVersion().getVersion());
        LazyId dependencyId = LazyId.fromName(it.getNameVersion().getName());
        builder.setDependencyInfo(dependencyId, it.getNameVersion().getName(), it.getNameVersion().getVersion(), id);
        if (isRootPackage(it.getNameVersion(), rootPackages)) {
            builder.addChildToRoot(dependencyId);
        }
        it.getDependencies().forEach(child -> {
            if (existsInPackages(child, models)) {
                LazyId childId = LazyId.fromName(child.getName());
                builder.addChildWithParent(childId, dependencyId);
            } else {
                logger.warn("Dependency was not found in packages list but found a require that used it: " + child.getName());
            }
        });
    });
    DependencyGraph graph = builder.build();
    CodeLocation codeLocation;
    if (projectNameVersion.getName() == null || projectNameVersion.getVersion() == null) {
        codeLocation = new CodeLocation(graph);
    } else {
        codeLocation = new CodeLocation(graph, externalIdFactory.createNameVersionExternalId(Forge.PACKAGIST, projectNameVersion.getName(), projectNameVersion.getVersion()));
    }
    return new PackagistParseResult(projectNameVersion.getName(), projectNameVersion.getVersion(), codeLocation);
}
Also used : PackagistPackage(com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) NameVersion(com.synopsys.integration.util.NameVersion) PackagistParseResult(com.synopsys.integration.detectable.detectables.packagist.model.PackagistParseResult) LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) JsonObject(com.google.gson.JsonObject) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) JsonParser(com.google.gson.JsonParser) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 3 with LazyId

use of com.synopsys.integration.bdio.graph.builder.LazyId in project synopsys-detect by blackducksoftware.

the class YarnTransformer method buildGraphForProjectOrWorkspace.

private DependencyGraph buildGraphForProjectOrWorkspace(YarnLockResult yarnLockResult, NullSafePackageJson projectOrWorkspacePackageJson, List<NameVersion> externalDependencies) throws MissingExternalIdException {
    LazyExternalIdDependencyGraphBuilder graphBuilder = new LazyExternalIdDependencyGraphBuilder();
    addRootNodesToGraph(graphBuilder, projectOrWorkspacePackageJson, yarnLockResult.getWorkspaceData());
    for (YarnLockEntry entry : yarnLockResult.getYarnLock().getEntries()) {
        for (YarnLockEntryId entryId : entry.getIds()) {
            LazyId id = generateComponentDependencyId(entryId.getName(), entryId.getVersion());
            graphBuilder.setDependencyInfo(id, entryId.getName(), entry.getVersion(), generateComponentExternalId(entryId.getName(), entry.getVersion()));
            addYarnLockDependenciesToGraph(yarnLockResult, graphBuilder, entry, id);
        }
    }
    return graphBuilder.build(getLazyBuilderHandler(externalDependencies));
}
Also used : YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 4 with LazyId

use of com.synopsys.integration.bdio.graph.builder.LazyId in project synopsys-detect by blackducksoftware.

the class CargoLockPackageTransformer method transformToGraph.

public DependencyGraph transformToGraph(List<CargoLockPackage> lockPackages) throws MissingExternalIdException, DetectableException {
    verifyNoDuplicatePackages(lockPackages);
    LazyExternalIdDependencyGraphBuilder graph = new LazyExternalIdDependencyGraphBuilder();
    lockPackages.forEach(lockPackage -> {
        String parentName = lockPackage.getPackageNameVersion().getName();
        String parentVersion = lockPackage.getPackageNameVersion().getVersion();
        LazyId parentId = LazyId.fromNameAndVersion(parentName, parentVersion);
        Dependency parentDependency = dependencyFactory.createNameVersionDependency(Forge.CRATES, parentName, parentVersion);
        graph.addChildToRoot(parentId);
        graph.setDependencyInfo(parentId, parentDependency.getName(), parentDependency.getVersion(), parentDependency.getExternalId());
        graph.setDependencyAsAlias(parentId, LazyId.fromName(parentName));
        lockPackage.getDependencies().forEach(childPackage -> {
            if (childPackage.getVersion().isPresent()) {
                LazyId childId = LazyId.fromNameAndVersion(childPackage.getName(), childPackage.getVersion().get());
                graph.addChildWithParent(childId, parentId);
            } else {
                LazyId childId = LazyId.fromName(childPackage.getName());
                graph.addChildWithParent(childId, parentId);
            }
        });
    });
    return graph.build();
}
Also used : LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 5 with LazyId

use of com.synopsys.integration.bdio.graph.builder.LazyId in project synopsys-detect by blackducksoftware.

the class PodlockParser method extractDependencyGraph.

public DependencyGraph extractDependencyGraph(String podLockText) throws IOException, MissingExternalIdException {
    LazyExternalIdDependencyGraphBuilder lazyBuilder = new LazyExternalIdDependencyGraphBuilder();
    YAMLMapper mapper = new YAMLMapper();
    PodfileLock podfileLock = mapper.readValue(podLockText, PodfileLock.class);
    Map<LazyId, Forge> forgeOverrides = createForgeOverrideMap(podfileLock);
    List<String> knownPods = determineAllPodNames(podfileLock);
    for (Pod pod : podfileLock.getPods()) {
        logger.trace(String.format("Processing pod %s", pod.getName()));
        processPod(pod, forgeOverrides, lazyBuilder, knownPods);
    }
    for (Pod dependency : podfileLock.getDependencies()) {
        logger.trace(String.format("Processing pod dependency from pod lock file %s", dependency.getName()));
        String podText = dependency.getName();
        Optional<LazyId> dependencyId = parseDependencyId(podText);
        dependencyId.ifPresent(lazyBuilder::addChildToRoot);
    }
    logger.trace("Attempting to build the dependency graph.");
    DependencyGraph dependencyGraph = lazyBuilder.build();
    logger.trace("Completed the dependency graph.");
    return dependencyGraph;
}
Also used : Pod(com.synopsys.integration.detectable.detectables.cocoapods.model.Pod) YAMLMapper(com.fasterxml.jackson.dataformat.yaml.YAMLMapper) LazyExternalIdDependencyGraphBuilder(com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder) Forge(com.synopsys.integration.bdio.model.Forge) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) PodfileLock(com.synopsys.integration.detectable.detectables.cocoapods.model.PodfileLock) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Aggregations

LazyId (com.synopsys.integration.bdio.graph.builder.LazyId)14 LazyExternalIdDependencyGraphBuilder (com.synopsys.integration.bdio.graph.builder.LazyExternalIdDependencyGraphBuilder)6 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)5 NameVersion (com.synopsys.integration.util.NameVersion)5 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)3 Forge (com.synopsys.integration.bdio.model.Forge)3 List (java.util.List)2 Map (java.util.Map)2 YAMLMapper (com.fasterxml.jackson.dataformat.yaml.YAMLMapper)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 MissingExternalIdException (com.synopsys.integration.bdio.graph.builder.MissingExternalIdException)1 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)1 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 Pod (com.synopsys.integration.detectable.detectables.cocoapods.model.Pod)1 PodfileLock (com.synopsys.integration.detectable.detectables.cocoapods.model.PodfileLock)1 PackagistPackage (com.synopsys.integration.detectable.detectables.packagist.model.PackagistPackage)1 PackagistParseResult (com.synopsys.integration.detectable.detectables.packagist.model.PackagistParseResult)1 BUNDLED_WITH (com.synopsys.integration.detectable.detectables.rubygems.gemlock.parse.GemlockParser.GemfileLockSection.BUNDLED_WITH)1