Search in sources :

Example 26 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class BazelCodeLocationBuilderTest method test.

@Test
public void test() {
    BazelCodeLocationBuilder bdioBuilder = new BazelCodeLocationBuilder(new ExternalIdFactory());
    final List<DetectCodeLocation> codeLocations = bdioBuilder.setWorkspaceDir(new File("src/test/resources/bazel/multiLevel")).addDependency(BazelExternalId.fromBazelArtifactString("testGroup:testArtifact:testVersion", ":")).build();
    assertEquals(1, codeLocations.size());
    assertEquals("src/test/resources/bazel/multiLevel", codeLocations.get(0).getExternalId().path);
    assertEquals(1, codeLocations.get(0).getDependencyGraph().getRootDependencies().size());
    Dependency dep = codeLocations.get(0).getDependencyGraph().getRootDependencies().iterator().next();
    assertEquals("testArtifact", dep.name);
    assertEquals("testVersion", dep.version);
    assertEquals("testGroup", dep.externalId.group);
}
Also used : DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) File(java.io.File) Test(org.junit.Test)

Example 27 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class DependencyGraphAssertions method assertDoesNotHave.

private static void assertDoesNotHave(final DependencyGraph dependencyGraph, final String target, final ExternalId current) {
    if (current == null) {
        for (final Dependency dep : dependencyGraph.getRootDependencies()) {
            assertDoesNotHave(dep, target);
            assertDoesNotHave(dependencyGraph, target, dep.externalId);
        }
    } else {
        for (final Dependency dep : dependencyGraph.getChildrenForParent(current)) {
            assertDoesNotHave(dep, target);
            assertDoesNotHave(dependencyGraph, target, dep.externalId);
        }
    }
}
Also used : Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 28 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class DependencyGraphAssertions method assertHasMavenGav.

public static void assertHasMavenGav(final DependencyGraph dependencyGraph, final String org, final String name, final String version) {
    final ExternalIdFactory externalIdFactory = new ExternalIdFactory();
    final ExternalId id = externalIdFactory.createMavenExternalId(org, name, version);
    final Dependency dep = dependencyGraph.getDependency(id);
    assertNotNull("Expected dependency '" + org + ":" + name + ":" + version + "' to exist in graph but it was null.", dep);
}
Also used : ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 29 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class NpmCliParser method populateChildren.

private void populateChildren(final MutableDependencyGraph graph, final Dependency parentDependency, final JsonObject parentNodeChildren, final Boolean root) {
    if (parentNodeChildren == null) {
        return;
    }
    final Set<Entry<String, JsonElement>> elements = parentNodeChildren.entrySet();
    elements.forEach(it -> {
        if (it.getValue() != null && it.getValue().isJsonObject()) {
        }
        final JsonObject element = it.getValue().getAsJsonObject();
        final String name = it.getKey();
        String version = null;
        final JsonPrimitive versionPrimitive = element.getAsJsonPrimitive(JSON_VERSION);
        if (versionPrimitive != null && versionPrimitive.isString()) {
            version = versionPrimitive.getAsString();
        }
        final JsonObject children = element.getAsJsonObject(JSON_DEPENDENCIES);
        if (name != null && version != null) {
            final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPM, name, version);
            final Dependency child = new Dependency(name, version, externalId);
            populateChildren(graph, child, children, false);
            if (root) {
                graph.addChildToRoot(child);
            } else {
                graph.addParentWithChild(parentDependency, child);
            }
        }
    });
}
Also used : Entry(java.util.Map.Entry) JsonPrimitive(com.google.gson.JsonPrimitive) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) JsonObject(com.google.gson.JsonObject) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 30 with Dependency

use of com.synopsys.integration.bdio.model.dependency.Dependency in project hub-detect by blackducksoftware.

the class GradleReportParser method parseDependencies.

public Optional<DetectCodeLocation> parseDependencies(final File codeLocationFile) {
    DetectCodeLocation codeLocation = null;
    String projectSourcePath = "";
    String projectGroup = "";
    String projectName = "";
    String projectVersionName = "";
    boolean processingMetaData = false;
    final MutableDependencyGraph graph = new MutableMapDependencyGraph();
    final DependencyHistory history = new DependencyHistory();
    try (FileInputStream dependenciesInputStream = new FileInputStream(codeLocationFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(dependenciesInputStream, StandardCharsets.UTF_8))) {
        while (reader.ready()) {
            final String line = reader.readLine();
            /**
             * The meta data section will be at the end of the file after all of the "gradle dependencies" output
             */
            if (line.startsWith(DETECT_META_DATA_HEADER)) {
                processingMetaData = true;
                continue;
            }
            if (line.startsWith(DETECT_META_DATA_FOOTER)) {
                processingMetaData = false;
                continue;
            }
            if (processingMetaData) {
                if (line.startsWith(PROJECT_PATH_PREFIX)) {
                    projectSourcePath = line.substring(PROJECT_PATH_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_GROUP_PREFIX)) {
                    projectGroup = line.substring(PROJECT_GROUP_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_NAME_PREFIX)) {
                    projectName = line.substring(PROJECT_NAME_PREFIX.length()).trim();
                } else if (line.startsWith(PROJECT_VERSION_PREFIX)) {
                    projectVersionName = line.substring(PROJECT_VERSION_PREFIX.length()).trim();
                }
                continue;
            }
            if (StringUtils.isBlank(line)) {
                history.clear();
                gradleReportConfigurationParser = new GradleReportConfigurationParser();
                continue;
            }
            final Dependency dependency = gradleReportConfigurationParser.parseDependency(externalIdFactory, line);
            if (dependency == null) {
                continue;
            }
            final int lineTreeLevel = gradleReportConfigurationParser.getTreeLevel();
            try {
                history.clearDependenciesDeeperThan(lineTreeLevel);
            } catch (final IllegalStateException e) {
                logger.warn(String.format("Problem parsing line '%s': %s", line, e.getMessage()));
            }
            if (history.isEmpty()) {
                graph.addChildToRoot(dependency);
            } else {
                graph.addChildWithParents(dependency, history.getLastDependency());
            }
            history.add(dependency);
        }
        final ExternalId id = externalIdFactory.createMavenExternalId(projectGroup, projectName, projectVersionName);
        codeLocation = new DetectCodeLocation.Builder(DetectCodeLocationType.GRADLE, projectSourcePath, id, graph).build();
    } catch (final IOException e) {
        codeLocation = null;
    }
    return Optional.ofNullable(codeLocation);
}
Also used : MutableDependencyGraph(com.synopsys.integration.bdio.graph.MutableDependencyGraph) InputStreamReader(java.io.InputStreamReader) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) MutableMapDependencyGraph(com.synopsys.integration.bdio.graph.MutableMapDependencyGraph) DependencyHistory(com.blackducksoftware.integration.hub.detect.util.DependencyHistory) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) DetectCodeLocation(com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation) BufferedReader(java.io.BufferedReader)

Aggregations

Dependency (com.synopsys.integration.bdio.model.dependency.Dependency)46 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)23 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)12 MutableMapDependencyGraph (com.synopsys.integration.bdio.graph.MutableMapDependencyGraph)11 Test (org.junit.Test)11 MutableDependencyGraph (com.synopsys.integration.bdio.graph.MutableDependencyGraph)10 DetectCodeLocation (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocation)9 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)5 File (java.io.File)5 DependencyHistory (com.blackducksoftware.integration.hub.detect.util.DependencyHistory)4 ArrayList (java.util.ArrayList)3 Extraction (com.blackducksoftware.integration.hub.detect.workflow.extraction.Extraction)2 Gson (com.google.gson.Gson)2 Forge (com.synopsys.integration.bdio.model.Forge)2 NpmDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.NpmDependency)1 PackageLockDependency (com.blackducksoftware.integration.hub.detect.detector.npm.model.PackageLockDependency)1 DetectUserFriendlyException (com.blackducksoftware.integration.hub.detect.exception.DetectUserFriendlyException)1 DetectCodeLocationType (com.blackducksoftware.integration.hub.detect.workflow.codelocation.DetectCodeLocationType)1 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1