Search in sources :

Example 1 with SbtDependencyModule

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule in project synopsys-detect by blackducksoftware.

the class SbtDependencyResolver method resolveReport.

public SbtDependencyModule resolveReport(SbtReport report) {
    ExternalId rootId = externalIdFactory.createMavenExternalId(report.getOrganisation(), report.getModule(), report.getRevision());
    logger.debug("Created external id: {}", rootId);
    BasicDependencyGraph graph = new BasicDependencyGraph();
    logger.debug("Dependencies found: " + report.getDependencies().size());
    report.getDependencies().forEach(module -> {
        logger.debug("Revisions found: " + module.getRevisions().size());
        module.getRevisions().forEach(revision -> {
            logger.debug("Callers found: " + revision.getCallers().size());
            ExternalId id = externalIdFactory.createMavenExternalId(module.getOrganisation(), module.getName(), revision.getName());
            Dependency child = new Dependency(module.getName(), revision.getName(), id);
            revision.getCallers().forEach(caller -> {
                ExternalId parentId = externalIdFactory.createMavenExternalId(caller.getOrganisation(), caller.getName(), caller.getRevision());
                Dependency parent = new Dependency(caller.getName(), caller.getRevision(), parentId);
                logger.debug("Caller id: " + parentId.toString());
                if (rootId.equals(parentId)) {
                    graph.addChildToRoot(child);
                } else {
                    graph.addParentWithChild(parent, child);
                }
            });
        });
    });
    SbtDependencyModule module = new SbtDependencyModule();
    module.setName(report.getModule());
    module.setVersion(report.getRevision());
    module.setOrg(report.getOrganisation());
    module.setGraph(graph);
    module.setConfiguration(report.getConfiguration());
    return module;
}
Also used : ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) Dependency(com.synopsys.integration.bdio.model.dependency.Dependency)

Example 2 with SbtDependencyModule

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule in project synopsys-detect by blackducksoftware.

the class SbtResolutionCacheExtractor method extractProject.

private SbtProject extractProject(File path, boolean followSymLinks, int depth, List<String> included, List<String> excluded) throws IOException, SAXException, ParserConfigurationException {
    List<SbtDependencyModule> rawModules = extractModules(path, followSymLinks, depth, included, excluded);
    List<SbtDependencyModule> modules = rawModules.stream().filter(it -> it.getGraph() != null).collect(Collectors.toList());
    int skipped = rawModules.size() - modules.size();
    if (skipped > 0) {
        logger.error(String.format("Skipped %s", skipped));
    }
    SbtProject result = new SbtProject();
    result.setModules(modules);
    if (modules.isEmpty()) {
        logger.warn("Unable to create an sbt project, no sbt modules were found.");
    } else if (modules.size() == 1) {
        logger.warn("Found exactly one root module, using it's name and version.");
        result.setProjectName(modules.get(0).getName());
        result.setProjectVersion(modules.get(0).getVersion());
        result.setProjectExternalId(externalIdFactory.createMavenExternalId(modules.get(0).getOrg(), modules.get(0).getName(), modules.get(0).getVersion()));
    } else {
        logger.warn("Unable to find exactly one root module. Using source path for root project name - will not set an external id.");
        result.setProjectName(path.getName());
        result.setProjectVersion(findFirstModuleVersion(modules, result.getProjectName(), "root"));
        result.setProjectExternalId(null);
        if (result.getProjectVersion() == null && modules.size() > 1) {
            logger.warn(String.format("Getting version from first project: %s", modules.get(0).getName()));
            result.setProjectVersion(modules.get(0).getVersion());
        }
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) IOException(java.io.IOException) SbtProject(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) File(java.io.File) ExcludedIncludedWildcardFilter(com.synopsys.integration.util.ExcludedIncludedWildcardFilter) ArrayList(java.util.ArrayList) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SbtReport(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtReport) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) SbtProject(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject)

Example 3 with SbtDependencyModule

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule in project synopsys-detect by blackducksoftware.

the class SbtResolutionCacheExtractor method makeModuleAggregate.

private List<SbtDependencyModule> makeModuleAggregate(List<File> reportFiles, List<String> include, List<String> exclude) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    SbtReportParser parser = new SbtReportParser();
    SbtDependencyResolver resolver = new SbtDependencyResolver(externalIdFactory);
    ExcludedIncludedWildcardFilter filter = ExcludedIncludedWildcardFilter.fromCollections(exclude, include);
    SbtModuleAggregator aggregator = new SbtModuleAggregator();
    List<SbtDependencyModule> modules = new ArrayList<>();
    for (File reportFile : reportFiles) {
        Document xml = builder.parse(reportFile);
        logger.debug(String.format("Parsing SBT report file: %s", reportFile.getCanonicalPath()));
        SbtReport report = parser.parseReportFromXml(xml);
        SbtDependencyModule tree = resolver.resolveReport(report);
        modules.add(tree);
    }
    List<SbtDependencyModule> includedModules = modules.stream().filter(module -> filter.shouldInclude(module.getConfiguration())).collect(Collectors.toList());
    if (modules.isEmpty()) {
        logger.warn("No sbt configurations were found in report folder.");
        return null;
    } else if (includedModules.isEmpty()) {
        logger.warn(String.format("Although %s configs were found, none were included.", modules.size()));
        return null;
    }
    return aggregator.aggregateModules(includedModules);
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Extraction(com.synopsys.integration.detectable.extraction.Extraction) LoggerFactory(org.slf4j.LoggerFactory) ExternalIdFactory(com.synopsys.integration.bdio.model.externalid.ExternalIdFactory) IOException(java.io.IOException) SbtProject(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject) Collectors(java.util.stream.Collectors) StringUtils(org.apache.commons.lang3.StringUtils) FileFinder(com.synopsys.integration.common.util.finder.FileFinder) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) File(java.io.File) ExcludedIncludedWildcardFilter(com.synopsys.integration.util.ExcludedIncludedWildcardFilter) ArrayList(java.util.ArrayList) List(java.util.List) CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXException(org.xml.sax.SAXException) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SbtReport(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtReport) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) ExcludedIncludedWildcardFilter(com.synopsys.integration.util.ExcludedIncludedWildcardFilter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) SbtReport(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtReport)

Example 4 with SbtDependencyModule

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule in project synopsys-detect by blackducksoftware.

the class SbtResolutionCacheExtractor method extract.

public Extraction extract(File directory, SbtResolutionCacheOptions sbtResolutionCacheOptions) {
    // TODO: Extractor should not use DetectableOptions
    try {
        // TODO: Handle null better.
        List<String> included = sbtResolutionCacheOptions.getIncludedConfigurations();
        List<String> excluded = sbtResolutionCacheOptions.getExcludedConfigurations();
        int depth = sbtResolutionCacheOptions.getReportDepth();
        SbtProject project = extractProject(directory, sbtResolutionCacheOptions.isFollowSymLinks(), depth, included, excluded);
        List<CodeLocation> codeLocations = new ArrayList<>();
        String projectName = null;
        String projectVersion = null;
        for (SbtDependencyModule module : project.getModules()) {
            CodeLocation codeLocation;
            if (project.getProjectExternalId() != null) {
                codeLocation = new CodeLocation(module.getGraph(), project.getProjectExternalId());
            } else {
                codeLocation = new CodeLocation(module.getGraph());
            }
            if (projectName == null) {
                projectName = project.getProjectName();
                projectVersion = project.getProjectVersion();
            }
            codeLocations.add(codeLocation);
        }
        if (codeLocations.size() > 0) {
            return new Extraction.Builder().success(codeLocations).projectName(projectName).projectVersion(projectVersion).build();
        } else {
            logger.error("Unable to find any dependency information.");
            return new Extraction.Builder().failure("Unable to find any dependency information.").build();
        }
    } catch (Exception e) {
        return new Extraction.Builder().exception(e).build();
    }
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) SbtProject(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArrayList(java.util.ArrayList) Extraction(com.synopsys.integration.detectable.extraction.Extraction) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 5 with SbtDependencyModule

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule in project synopsys-detect by blackducksoftware.

the class SbtModuleAggregator method aggregateModules.

public List<SbtDependencyModule> aggregateModules(List<SbtDependencyModule> modules) {
    Set<SbtAggregate> aggregates = uniqueAggregates(modules);
    logger.debug("Found unique aggregates: " + aggregates.size());
    return aggregates.stream().map(aggregate -> {
        SbtDependencyModule aggregated = new SbtDependencyModule();
        aggregated.setName(aggregate.getName());
        aggregated.setVersion(aggregate.getVersion());
        aggregated.setOrg(aggregate.getOrg());
        BasicDependencyGraph graph = new BasicDependencyGraph();
        aggregated.setGraph(graph);
        modules.forEach(module -> {
            if (moduleEqualsAggregate(module, aggregate)) {
                logger.debug("Combining '" + module.getName() + "' with '" + aggregate.getName() + "'");
                graph.copyGraphToRoot((BasicDependencyGraph) module.getGraph());
            }
        });
        return aggregated;
    }).collect(Collectors.toList());
}
Also used : List(java.util.List) Logger(org.slf4j.Logger) SbtAggregate(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtAggregate) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph) SbtAggregate(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtAggregate) SbtDependencyModule(com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule) BasicDependencyGraph(com.synopsys.integration.bdio.graph.BasicDependencyGraph)

Aggregations

SbtDependencyModule (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule)7 ArrayList (java.util.ArrayList)5 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)3 SbtProject (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject)3 Extraction (com.synopsys.integration.detectable.extraction.Extraction)3 File (java.io.File)3 IOException (java.io.IOException)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 SAXException (org.xml.sax.SAXException)3 BasicDependencyGraph (com.synopsys.integration.bdio.graph.BasicDependencyGraph)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)2 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)2 SbtReport (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtReport)2 ExcludedIncludedWildcardFilter (com.synopsys.integration.util.ExcludedIncludedWildcardFilter)2 Arrays (java.util.Arrays)2