Search in sources :

Example 1 with NpmPackageInfo

use of org.jfrog.build.extractor.npm.types.NpmPackageInfo in project build-info by JFrogDev.

the class NpmBuildInfoExtractor method populateDependenciesMap.

/**
 * Populate the dependencies map for the specified scope by:
 * 1. Create npm dependency tree from root node of 'npm ls' command tree. Populate each node with name, version and scope.
 * 2. For each dependency, retrieve sha1 and md5 from Artifactory. Use the producer-consumer mechanism to parallelize it.
 */
private void populateDependenciesMap(Map<String, Dependency> dependencies, Map<String, Dependency> previousBuildDependencies, JsonNode npmDependencyTree, NpmScope scope, Path workingDir) throws Exception {
    // Set of packages that could not be found in Artifactory.
    Set<NpmPackageInfo> badPackages = Collections.synchronizedSet(new HashSet<>());
    DefaultMutableTreeNode rootNode = NpmDependencyTree.createDependencyTree(npmDependencyTree, scope, workingDir);
    try (ArtifactoryManager artifactoryManager = artifactoryManagerBuilder.build()) {
        // Create producer Runnable.
        ProducerRunnableBase[] producerRunnable = new ProducerRunnableBase[] { new NpmExtractorProducer(rootNode) };
        // Create consumer Runnables.
        ConsumerRunnableBase[] consumerRunnables = new ConsumerRunnableBase[] { new NpmExtractorConsumer(artifactoryManager, dependencies, previousBuildDependencies, badPackages), new NpmExtractorConsumer(artifactoryManager, dependencies, previousBuildDependencies, badPackages), new NpmExtractorConsumer(artifactoryManager, dependencies, previousBuildDependencies, badPackages) };
        // Create the deployment executor.
        ProducerConsumerExecutor deploymentExecutor = new ProducerConsumerExecutor(logger, producerRunnable, consumerRunnables, CONNECTION_POOL_SIZE);
        deploymentExecutor.start();
        if (!badPackages.isEmpty()) {
            logger.info((Arrays.toString(badPackages.toArray())));
            logger.info("The npm dependencies above could not be found in Artifactory and therefore are not included in the build-info. " + "Make sure the dependencies are available in Artifactory for this build. " + "Deleting the local cache will force populating Artifactory with these dependencies.");
        }
    }
}
Also used : NpmPackageInfo(org.jfrog.build.extractor.npm.types.NpmPackageInfo) ProducerConsumerExecutor(org.jfrog.build.extractor.producerConsumer.ProducerConsumerExecutor) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ArtifactoryManager(org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager) ProducerRunnableBase(org.jfrog.build.extractor.producerConsumer.ProducerRunnableBase) ConsumerRunnableBase(org.jfrog.build.extractor.producerConsumer.ConsumerRunnableBase)

Example 2 with NpmPackageInfo

use of org.jfrog.build.extractor.npm.types.NpmPackageInfo in project build-info by JFrogDev.

the class NpmDependencyTree method createDependencyTree.

/**
 * Create a npm dependency tree from the results of 'npm ls' command.
 *
 * @param npmList    - Results of 'npm ls' command
 * @param scope      - Dependency scope
 * @param workingDir - The package.json directory
 * @return Tree of npm PackageInfos.
 * @see NpmPackageInfo
 */
public static DependencyTree createDependencyTree(JsonNode npmList, NpmScope scope, Path workingDir) {
    DependencyTree rootNode = new DependencyTree();
    populateDependenciesTree(rootNode, npmList.get("dependencies"), new String[] { getProjectName(npmList, workingDir) }, scope);
    for (DependencyTree child : rootNode.getChildren()) {
        NpmPackageInfo packageInfo = (NpmPackageInfo) child.getUserObject();
        child.setScopes(getScopes(packageInfo.getName(), packageInfo.getScope()));
    }
    return rootNode;
}
Also used : NpmPackageInfo(org.jfrog.build.extractor.npm.types.NpmPackageInfo) DependencyTree(org.jfrog.build.extractor.scan.DependencyTree)

Example 3 with NpmPackageInfo

use of org.jfrog.build.extractor.npm.types.NpmPackageInfo in project build-info by JFrogDev.

the class NpmExtractorProducer method producerRun.

@Override
public void producerRun() throws InterruptedException {
    try {
        Enumeration e = dependenciesRootNode.breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            if (Thread.interrupted()) {
                break;
            }
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
            NpmPackageInfo npmPackageInfo = (NpmPackageInfo) node.getUserObject();
            if (npmPackageInfo == null) {
                continue;
            }
            if (StringUtils.isBlank(npmPackageInfo.getVersion())) {
                log.warn("npm dependencies list contains the package " + npmPackageInfo.getName() + " without version information. The dependency will not be added to build-info");
                continue;
            }
            executor.put(npmPackageInfo);
        }
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        // Throw unchecked exception for the UncaughtExceptionHandler
        throw new RuntimeException(e);
    }
}
Also used : NpmPackageInfo(org.jfrog.build.extractor.npm.types.NpmPackageInfo) Enumeration(java.util.Enumeration) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode)

Example 4 with NpmPackageInfo

use of org.jfrog.build.extractor.npm.types.NpmPackageInfo in project build-info by JFrogDev.

the class NpmExtractorConsumer method consumerRun.

@Override
public void consumerRun() {
    while (!Thread.interrupted()) {
        try {
            ProducerConsumerItem item = executor.take();
            if (item == executor.TERMINATE) {
                // If reached the TERMINATE NpmPackageInfo, return it to the queue and exit.
                executor.put(item);
                break;
            }
            NpmPackageInfo npmPackageInfo = (NpmPackageInfo) item;
            // Try to extract sha1 and md5 for 'npmPackageInfo'. If it doesn't exist in Artifactory's cache, add it to the 'badPackages' list.
            if (!appendDependency(npmPackageInfo)) {
                badPackages.add(npmPackageInfo);
            }
        } catch (InterruptedException e) {
            return;
        }
    }
}
Also used : NpmPackageInfo(org.jfrog.build.extractor.npm.types.NpmPackageInfo) ProducerConsumerItem(org.jfrog.build.api.producerConsumer.ProducerConsumerItem)

Example 5 with NpmPackageInfo

use of org.jfrog.build.extractor.npm.types.NpmPackageInfo in project build-info by JFrogDev.

the class NpmDependencyTree method addSubtree.

private static void addSubtree(Map.Entry<String, JsonNode> stringJsonNodeEntry, DependencyTree node, String name, String version, String[] pathToRoot, NpmScope scope) {
    JsonNode jsonNode = stringJsonNodeEntry.getValue();
    String devScope = scope.toString();
    NpmPackageInfo npmPackageInfo = new NpmPackageInfo(name, version, devScope, pathToRoot);
    JsonNode childDependencies = jsonNode.get("dependencies");
    DependencyTree childTreeNode = new DependencyTree(npmPackageInfo);
    // Mutual recursive call
    populateDependenciesTree(childTreeNode, childDependencies, ArrayUtils.insert(0, pathToRoot, npmPackageInfo.toString()), scope);
    node.add(childTreeNode);
}
Also used : NpmPackageInfo(org.jfrog.build.extractor.npm.types.NpmPackageInfo) DependencyTree(org.jfrog.build.extractor.scan.DependencyTree) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

NpmPackageInfo (org.jfrog.build.extractor.npm.types.NpmPackageInfo)5 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)2 DependencyTree (org.jfrog.build.extractor.scan.DependencyTree)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Enumeration (java.util.Enumeration)1 ProducerConsumerItem (org.jfrog.build.api.producerConsumer.ProducerConsumerItem)1 ArtifactoryManager (org.jfrog.build.extractor.clientConfiguration.client.artifactory.ArtifactoryManager)1 ConsumerRunnableBase (org.jfrog.build.extractor.producerConsumer.ConsumerRunnableBase)1 ProducerConsumerExecutor (org.jfrog.build.extractor.producerConsumer.ProducerConsumerExecutor)1 ProducerRunnableBase (org.jfrog.build.extractor.producerConsumer.ProducerRunnableBase)1