Search in sources :

Example 1 with SbtProject

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject 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 2 with SbtProject

use of com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject 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)

Aggregations

CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)2 SbtDependencyModule (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtDependencyModule)2 SbtProject (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtProject)2 Extraction (com.synopsys.integration.detectable.extraction.Extraction)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXException (org.xml.sax.SAXException)2 ExternalIdFactory (com.synopsys.integration.bdio.model.externalid.ExternalIdFactory)1 FileFinder (com.synopsys.integration.common.util.finder.FileFinder)1 SbtReport (com.synopsys.integration.detectable.detectables.sbt.parse.model.SbtReport)1 ExcludedIncludedWildcardFilter (com.synopsys.integration.util.ExcludedIncludedWildcardFilter)1 File (java.io.File)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1