Search in sources :

Example 1 with NpmDependency

use of com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency in project hub-detect by blackducksoftware.

the class NpmDependencyConverter method convertPackageMapToDependencies.

public List<NpmDependency> convertPackageMapToDependencies(NpmDependency parent, Map<String, PackageLockDependency> packageLockDependencyMap) {
    List<NpmDependency> children = new ArrayList<>();
    if (packageLockDependencyMap == null || packageLockDependencyMap.size() == 0) {
        return children;
    }
    for (Map.Entry<String, PackageLockDependency> packageEntry : packageLockDependencyMap.entrySet()) {
        String packageName = packageEntry.getKey();
        PackageLockDependency packageLockDependency = packageEntry.getValue();
        NpmDependency dependency = createNpmDependency(packageName, packageLockDependency.version, packageLockDependency.dev);
        dependency.setParent(parent);
        children.add(dependency);
        List<NpmRequires> requires = convertNameVersionMapToRequires(packageLockDependency.requires);
        dependency.addAllRequires(requires);
        List<NpmDependency> grandChildren = convertPackageMapToDependencies(dependency, packageLockDependency.dependencies);
        dependency.addAllDependencies(grandChildren);
    }
    return children;
}
Also used : PackageLockDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency) NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) NpmRequires(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmRequires) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 2 with NpmDependency

use of com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency in project hub-detect by blackducksoftware.

the class NpmLockfileParser method traverse.

private void traverse(NpmDependency npmDependency, MutableDependencyGraph dependencyGraph, boolean atRoot, boolean includeDevDependencies) {
    if (!shouldInclude(npmDependency, includeDevDependencies))
        return;
    npmDependency.getRequires().forEach(required -> {
        NpmDependency resolved = lookupDependency(npmDependency, required.getName());
        logger.debug("Required package: " + required.getName() + " of version: " + required.getFuzzyVersion());
        if (resolved != null) {
            logger.debug("Found package: " + resolved.getName() + "with version: " + resolved.getVersion());
            if (atRoot) {
                dependencyGraph.addChildToRoot(resolved.getGraphDependency());
            } else {
                dependencyGraph.addChildWithParent(resolved.getGraphDependency(), npmDependency.getGraphDependency());
            }
        } else {
            logger.error("No dependency found for package: " + required.getName());
        }
    });
    npmDependency.getDependencies().forEach(child -> traverse(child, dependencyGraph, false, includeDevDependencies));
}
Also used : NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency)

Example 3 with NpmDependency

use of com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency in project hub-detect by blackducksoftware.

the class NpmLockfileParser method parse.

public NpmParseResult parse(final String sourcePath, final Optional<String> packageJsonText, final String lockFileText, final boolean includeDevDependencies) {
    final MutableDependencyGraph dependencyGraph = new MutableMapDependencyGraph();
    logger.info("Parsing lock file text: ");
    logger.debug(lockFileText);
    Optional<PackageJson> packageJson = Optional.empty();
    if (packageJsonText.isPresent()) {
        logger.debug(packageJsonText.get());
        packageJson = Optional.of(gson.fromJson(packageJsonText.get(), PackageJson.class));
    }
    final PackageLock packageLock = gson.fromJson(lockFileText, PackageLock.class);
    logger.debug(lockFileText);
    logger.info("Processing project.");
    if (packageLock.dependencies != null) {
        logger.info(String.format("Found %d dependencies.", packageLock.dependencies.size()));
        // Convert to our custom format
        NpmDependencyConverter dependencyConverter = new NpmDependencyConverter(externalIdFactory);
        NpmDependency rootDependency = dependencyConverter.convertLockFile(packageLock, packageJson);
        traverse(rootDependency, dependencyGraph, true, includeDevDependencies);
    } else {
        logger.info("Lock file did not have a 'dependencies' section.");
    }
    logger.info("Finished processing.");
    final ExternalId projectId = externalIdFactory.createNameVersionExternalId(Forge.NPM, packageLock.name, packageLock.version);
    final DetectCodeLocation codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.NPM, sourcePath, projectId, dependencyGraph).build();
    return new NpmParseResult(packageLock.name, packageLock.version, codeLocation);
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) PackageLock(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock) NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) PackageJson(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageJson)

Example 4 with NpmDependency

use of com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency in project hub-detect by blackducksoftware.

the class NpmDependencyConverter method convertLockFile.

public NpmDependency convertLockFile(PackageLock packageLock, Optional<PackageJson> packageJsonOptional) {
    NpmDependency root = createNpmDependency(packageLock.name, packageLock.version, false);
    if (packageLock.dependencies != null) {
        List<NpmDependency> children = convertPackageMapToDependencies(root, packageLock.dependencies);
        root.addAllDependencies(children);
    }
    if (packageJsonOptional.isPresent()) {
        PackageJson packageJson = packageJsonOptional.get();
        if (packageJson.dependencies != null) {
            List<NpmRequires> rootRequires = convertNameVersionMapToRequires(packageJson.dependencies);
            root.addAllRequires(rootRequires);
        }
        if (packageJson.devDependencies != null) {
            List<NpmRequires> rootDevRequires = convertNameVersionMapToRequires(packageJson.devDependencies);
            root.addAllRequires(rootDevRequires);
        }
    } else {
        if (packageLock.dependencies != null) {
            List<NpmRequires> requires = packageLock.dependencies.entrySet().stream().map(entry -> new NpmRequires(entry.getKey(), entry.getValue().version)).collect(Collectors.toList());
            root.addAllRequires(requires);
        }
    }
    return root;
}
Also used : NpmRequires(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmRequires) NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) PackageJson(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageJson) Forge(com.synopsys.integration.bdio.model.Forge) PackageLock(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) Collectors(java.util.stream.Collectors) PackageLockDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency) ArrayList(java.util.ArrayList) List(java.util.List) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) Map(java.util.Map) Optional(java.util.Optional) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Collections(java.util.Collections) NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) NpmRequires(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmRequires) PackageJson(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageJson)

Example 5 with NpmDependency

use of com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency in project hub-detect by blackducksoftware.

the class NpmDependencyConverter method createNpmDependency.

private NpmDependency createNpmDependency(String name, String version, Boolean isDev) {
    ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPM, name, version);
    Dependency graphDependency = new Dependency(name, version, externalId);
    boolean dev = false;
    if (isDev != null && isDev == true) {
        dev = true;
    }
    return new NpmDependency(name, version, dev, graphDependency);
}
Also used : NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) NpmDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency) PackageLockDependency(com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Aggregations

NpmDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency)5 PackageLockDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency)3 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)3 NpmRequires (com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmRequires)2 PackageJson (com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageJson)2 PackageLock (com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLock)2 Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)1 MutableDependencyGraph (com.synopsys.integration.bdio.graph.MutableDependencyGraph)1 MutableMapDependencyGraph (com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)1 Forge (com.synopsys.integration.bdio.model.Forge)1 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1