Search in sources :

Example 1 with DependencyTree

use of org.jfrog.build.extractor.scan.DependencyTree 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 2 with DependencyTree

use of org.jfrog.build.extractor.scan.DependencyTree in project build-info by JFrogDev.

the class GoRun method populateModuleAndDeps.

/**
 * Populate the module name and the dependencies.
 */
private void populateModuleAndDeps() throws Exception {
    backupModAndSumFiles();
    try {
        DependencyTree dependencyTree = createDependencyTree(goDriver, logger, true);
        moduleName = dependencyTree.toString();
        String cachePath = getCachePath();
        Enumeration<?> nodes = dependencyTree.breadthFirstEnumeration();
        // Skip root
        nodes.nextElement();
        while (nodes.hasMoreElements()) {
            DependencyTree node = (DependencyTree) nodes.nextElement();
            addDependency(node.toString(), cachePath);
        }
    } finally {
        restoreModAnsSumFiles();
    }
}
Also used : DependencyTree(org.jfrog.build.extractor.scan.DependencyTree) GoDependencyTree.createDependencyTree(org.jfrog.build.extractor.go.extractor.GoDependencyTree.createDependencyTree)

Example 3 with DependencyTree

use of org.jfrog.build.extractor.scan.DependencyTree in project build-info by JFrogDev.

the class GoDependencyTree method populateDependencyTree.

/**
 * Recursively populate the dependency tree.
 *
 * @param currNode              - The current node
 * @param currNameVersionString - Current dependency in form of <name>@v<version>
 * @param allDependencies       - Dependency to children map
 * @param logger                - The logger
 */
private static void populateDependencyTree(DependencyTree currNode, String currNameVersionString, Map<String, List<String>> allDependencies, Log logger) {
    if (hasLoop(currNode, logger)) {
        return;
    }
    List<String> currDependencies = allDependencies.get(currNameVersionString);
    if (currDependencies == null) {
        return;
    }
    for (String dependency : currDependencies) {
        String[] dependencyNameVersion = dependency.split("@v");
        DependencyTree DependencyTree = new DependencyTree(dependencyNameVersion[0] + ":" + dependencyNameVersion[1]);
        currNode.add(DependencyTree);
        populateDependencyTree(DependencyTree, dependency, allDependencies, logger);
    }
}
Also used : DependencyTree(org.jfrog.build.extractor.scan.DependencyTree)

Example 4 with DependencyTree

use of org.jfrog.build.extractor.scan.DependencyTree in project build-info by JFrogDev.

the class GoDependencyTree method createDependencyTree.

/**
 * Create Go dependency tree of actually used dependencies.
 *
 * @param goDriver - Go driver
 * @param logger   - The logger
 * @param verbose  - verbose logging
 * @return Go dependency tree
 * @throws IOException in case of any I/O error.
 */
public static DependencyTree createDependencyTree(GoDriver goDriver, Log logger, boolean verbose) throws IOException {
    // Run go mod graph.
    CommandResults goGraphResult = goDriver.modGraph(verbose);
    String[] dependenciesGraph = goGraphResult.getRes().split("\\r?\\n");
    // Run go list -f "{{with .Module}}{{.Path}} {{.Version}}{{end}}" all
    CommandResults usedModulesResults;
    try {
        usedModulesResults = goDriver.getUsedModules(false, false);
    } catch (IOException e) {
        // Errors occurred during running "go list". Run again and this time ignore errors.
        usedModulesResults = goDriver.getUsedModules(false, true);
        logger.warn("Errors occurred during building the Go dependency tree. The dependency tree may be incomplete:" + System.lineSeparator() + ExceptionUtils.getRootCauseMessage(e));
    }
    Set<String> usedDependencies = Arrays.stream(usedModulesResults.getRes().split("\\r?\\n")).map(String::trim).map(usedModule -> usedModule.replace(" ", "@")).collect(Collectors.toSet());
    // Create root node.
    String rootPackageName = goDriver.getModuleName();
    DependencyTree rootNode = new DependencyTree(rootPackageName);
    rootNode.setMetadata(true);
    // Build dependency tree.
    Map<String, List<String>> dependenciesMap = new HashMap<>();
    populateDependenciesMap(dependenciesGraph, usedDependencies, dependenciesMap);
    populateDependencyTree(rootNode, rootPackageName, dependenciesMap, logger);
    return rootNode;
}
Also used : Log(org.jfrog.build.api.util.Log) java.util(java.util) DependencyTree(org.jfrog.build.extractor.scan.DependencyTree) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) CommandResults(org.jfrog.build.extractor.executor.CommandResults) Collectors(java.util.stream.Collectors) GoDriver(org.jfrog.build.extractor.go.GoDriver) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) DependencyTree(org.jfrog.build.extractor.scan.DependencyTree) IOException(java.io.IOException) CommandResults(org.jfrog.build.extractor.executor.CommandResults)

Example 5 with DependencyTree

use of org.jfrog.build.extractor.scan.DependencyTree 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

DependencyTree (org.jfrog.build.extractor.scan.DependencyTree)5 NpmPackageInfo (org.jfrog.build.extractor.npm.types.NpmPackageInfo)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 ExceptionUtils (org.apache.commons.lang3.exception.ExceptionUtils)1 Log (org.jfrog.build.api.util.Log)1 CommandResults (org.jfrog.build.extractor.executor.CommandResults)1 GoDriver (org.jfrog.build.extractor.go.GoDriver)1 GoDependencyTree.createDependencyTree (org.jfrog.build.extractor.go.extractor.GoDependencyTree.createDependencyTree)1