Search in sources :

Example 1 with YarnWorkspace

use of com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace in project synopsys-detect by blackducksoftware.

the class YarnTransformerTest method addWorkspacePackageJson.

private void addWorkspacePackageJson(Collection<YarnWorkspace> workspacesByName, NameVersion workspaceNameVersion, String workspaceDepName, String workspaceDevDepName) {
    PackageJson rawWorkspacePackageJson = new PackageJson();
    rawWorkspacePackageJson.name = workspaceNameVersion.getName();
    rawWorkspacePackageJson.version = workspaceNameVersion.getVersion();
    rawWorkspacePackageJson.dependencies = new HashMap<>();
    rawWorkspacePackageJson.dependencies.put(workspaceDepName, workspaceNameVersion.getVersion());
    rawWorkspacePackageJson.devDependencies.put(workspaceDevDepName, workspaceNameVersion.getVersion());
    NullSafePackageJson workspacePackageJson = new NullSafePackageJson(rawWorkspacePackageJson);
    WorkspacePackageJson locatedWorkspacePackageJson = new WorkspacePackageJson(null, workspacePackageJson, "packages/" + workspaceNameVersion.getName());
    YarnWorkspace workspace = new YarnWorkspace(locatedWorkspacePackageJson);
    workspacesByName.add(workspace);
}
Also used : WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)

Example 2 with YarnWorkspace

use of com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace in project synopsys-detect by blackducksoftware.

the class YarnTransformerTest method buildTestYarnLockResult.

@NotNull
private YarnLockResult buildTestYarnLockResult(List<NameVersion> workspacesThatAreDependencies, List<NameVersion> workspacesThatAreNotDependencies, boolean yarn1project) {
    PackageJson rawPackageJson = new PackageJson();
    rawPackageJson.dependencies = new HashMap<>();
    rawPackageJson.dependencies.put("foo", "fooFuzzyVersion-1.0");
    for (NameVersion workspace : workspacesThatAreDependencies) {
        rawPackageJson.dependencies.put(workspace.getName(), workspace.getVersion());
    }
    rawPackageJson.devDependencies.put("bar", "barFuzzyVersion-1.0");
    NullSafePackageJson packageJson = new NullSafePackageJson(rawPackageJson);
    // yarn.lock: foo and bar both depend on yarn
    List<YarnLockEntryId> yarnLockEntryIdsFoo = Collections.singletonList(new YarnLockEntryId("foo", "fooFuzzyVersion-1.0"));
    List<YarnLockEntryId> yarnLockEntryIdsBar = Collections.singletonList(new YarnLockEntryId("bar", "barFuzzyVersion-1.0"));
    List<YarnLockEntryId> yarnLockEntryIdsYarn = Collections.singletonList(new YarnLockEntryId("yarn", "^1.22.4"));
    List<YarnLockDependency> dependencyRefToYarn = Collections.singletonList(new YarnLockDependency("yarn", "^1.22.4", false));
    List<YarnLockEntry> yarnLockEntries = new LinkedList<>();
    if (!yarn1project) {
        List<YarnLockEntryId> projectEntryIds = Collections.singletonList(new YarnLockEntryId("project", "1.0.0"));
        List<YarnLockDependency> projectDependencies = new LinkedList<>();
        projectDependencies.add(new YarnLockDependency("foo", "fooFuzzyVersion-1.0", false));
        projectDependencies.add(new YarnLockDependency("bar", "barFuzzyVersion-1.0", false));
        for (NameVersion workspaceThatIsDependency : workspacesThatAreDependencies) {
            projectDependencies.add(new YarnLockDependency(workspaceThatIsDependency.getName(), workspaceThatIsDependency.getVersion(), false));
        }
        yarnLockEntries.add(new YarnLockEntry(false, projectEntryIds, "1.0.0", projectDependencies));
    }
    Collection<YarnWorkspace> workspacesByName = new LinkedList<>();
    List<NameVersion> allWorkspaces = new LinkedList<>(workspacesThatAreDependencies);
    allWorkspaces.addAll(workspacesThatAreNotDependencies);
    for (NameVersion workspace : allWorkspaces) {
        String workspaceDepName = workspace.getName() + WORKSPACE_DEP_SUFFIX;
        String workspaceDevDepName = workspace.getName() + "-dev" + WORKSPACE_DEP_SUFFIX;
        addWorkspacePackageJson(workspacesByName, workspace, workspaceDepName, workspaceDevDepName);
        if (!yarn1project) {
            addWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
        }
        addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDepName);
        addDependencyOfWorkspaceToYarnLockEntries(yarnLockEntries, workspace, workspaceDevDepName);
    }
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsFoo, "1.0", dependencyRefToYarn));
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsBar, "1.0", dependencyRefToYarn));
    yarnLockEntries.add(new YarnLockEntry(false, yarnLockEntryIdsYarn, "1.22.5", new LinkedList<>()));
    String yarnLockVersion = null;
    if (!yarn1project) {
        yarnLockVersion = "4";
    }
    YarnLock yarnLock = new YarnLock(yarnLockVersion, yarn1project, yarnLockEntries);
    YarnWorkspaces workspaceData = new YarnWorkspaces(workspacesByName);
    return new YarnLockResult(packageJson, workspaceData, yarnLock);
}
Also used : NameVersion(com.synopsys.integration.util.NameVersion) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) LinkedList(java.util.LinkedList) YarnLock(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock) YarnLockResult(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult) YarnLockEntryId(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId) YarnLockEntry(com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry) YarnLockDependency(com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency) YarnWorkspaces(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces) NullSafePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson) WorkspacePackageJson(com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson) PackageJson(com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with YarnWorkspace

use of com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace in project synopsys-detect by blackducksoftware.

the class YarnTransformer method generateCodeLocations.

public List<CodeLocation> generateCodeLocations(YarnLockResult yarnLockResult, List<NameVersion> externalDependencies, @Nullable ExcludedIncludedWildcardFilter workspaceFilter) throws MissingExternalIdException {
    List<CodeLocation> codeLocations = new LinkedList<>();
    logger.debug("Adding root dependencies for project: {}:{}", yarnLockResult.getRootPackageJson().getNameString(), yarnLockResult.getRootPackageJson().getVersionString());
    DependencyGraph rootProjectGraph = buildGraphForProjectOrWorkspace(yarnLockResult, yarnLockResult.getRootPackageJson(), externalDependencies);
    codeLocations.add(new CodeLocation(rootProjectGraph));
    for (YarnWorkspace workspace : yarnLockResult.getWorkspaceData().getWorkspaces()) {
        if ((workspaceFilter == null) || workspaceFilter.shouldInclude(workspace.getWorkspacePackageJson().getDirRelativePath())) {
            logger.debug("Adding root dependencies for workspace: {}", workspace.getWorkspacePackageJson().getDirRelativePath());
            DependencyGraph workspaceGraph = buildGraphForProjectOrWorkspace(yarnLockResult, workspace.getWorkspacePackageJson().getPackageJson(), externalDependencies);
            ExternalId workspaceExternalId = externalIdFactory.createNameVersionExternalId(Forge.NPMJS, workspace.getWorkspacePackageJson().getDirRelativePath(), "local");
            codeLocations.add(new CodeLocation(workspaceGraph, workspaceExternalId));
        }
    }
    return codeLocations;
}
Also used : CodeLocation(com.synopsys.integration.detectable.detectable.codelocation.CodeLocation) ExternalId(com.synopsys.integration.bdio.model.externalid.ExternalId) DependencyGraph(com.synopsys.integration.bdio.graph.DependencyGraph) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) LinkedList(java.util.LinkedList)

Example 4 with YarnWorkspace

use of com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace in project synopsys-detect by blackducksoftware.

the class YarnTransformer method addRootDependenciesToGraph.

private void addRootDependenciesToGraph(LazyExternalIdDependencyGraphBuilder graphBuilder, Map<String, String> rootDependenciesToAdd, YarnWorkspaces workspaceData) {
    for (Map.Entry<String, String> rootDependency : rootDependenciesToAdd.entrySet()) {
        Optional<YarnWorkspace> dependencyWorkspace = workspaceData.lookup(rootDependency.getKey(), rootDependency.getValue());
        if (dependencyWorkspace.isPresent()) {
            logger.trace("Omitting dependency {}/{} because it's a workspace", rootDependency.getKey(), rootDependency.getValue());
        } else {
            LazyId stringDependencyId = generateComponentDependencyId(rootDependency.getKey(), rootDependency.getValue());
            logger.debug("Adding root dependency to graph: stringDependencyId: {}", stringDependencyId);
            graphBuilder.addChildToRoot(stringDependencyId);
        }
    }
}
Also used : YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) Map(java.util.Map) LazyId(com.synopsys.integration.bdio.graph.builder.LazyId)

Example 5 with YarnWorkspace

use of com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace in project synopsys-detect by blackducksoftware.

the class PackageJsonFiles method readWorkspacePackageJsonFiles.

@NotNull
public Collection<YarnWorkspace> readWorkspacePackageJsonFiles(File workspaceDir) throws IOException {
    String forwardSlashedWorkspaceDirPath = deriveForwardSlashedPath(workspaceDir);
    File packageJsonFile = new File(workspaceDir, YarnLockDetectable.YARN_PACKAGE_JSON);
    List<String> workspaceDirPatterns = extractWorkspaceDirPatterns(packageJsonFile);
    Collection<YarnWorkspace> workspaces = new LinkedList<>();
    for (String workspaceSubdirPattern : workspaceDirPatterns) {
        logger.trace("workspaceSubdirPattern: {}", workspaceSubdirPattern);
        String globString = String.format("glob:%s/%s/package.json", forwardSlashedWorkspaceDirPath, workspaceSubdirPattern);
        logger.trace("workspace subdir globString: {}", globString);
        PathMatcher matcher = FileSystems.getDefault().getPathMatcher(globString);
        Files.walkFileTree(workspaceDir.toPath(), new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (matcher.matches(file)) {
                    logger.trace("\tFound a match: {}", file);
                    NullSafePackageJson packageJson = read(file.toFile());
                    Path rel = workspaceDir.toPath().relativize(file.getParent());
                    WorkspacePackageJson workspacePackageJson = new WorkspacePackageJson(file.toFile(), packageJson, rel.toString());
                    YarnWorkspace workspace = new YarnWorkspace(workspacePackageJson);
                    workspaces.add(workspace);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) {
                return FileVisitResult.CONTINUE;
            }
        });
    }
    if (!workspaceDirPatterns.isEmpty()) {
        logger.debug("Found {} matching workspace package.json files for workspaces listed in {}", workspaces.size(), packageJsonFile.getAbsolutePath());
    }
    return workspaces;
}
Also used : Path(java.nio.file.Path) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) LinkedList(java.util.LinkedList) PathMatcher(java.nio.file.PathMatcher) YarnWorkspace(com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

YarnWorkspace (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspace)6 LinkedList (java.util.LinkedList)4 NotNull (org.jetbrains.annotations.NotNull)3 PackageJson (com.synopsys.integration.detectable.detectables.npm.packagejson.model.PackageJson)2 NullSafePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.NullSafePackageJson)2 WorkspacePackageJson (com.synopsys.integration.detectable.detectables.yarn.packagejson.WorkspacePackageJson)2 YarnWorkspaces (com.synopsys.integration.detectable.detectables.yarn.workspace.YarnWorkspaces)2 DependencyGraph (com.synopsys.integration.bdio.graph.DependencyGraph)1 LazyId (com.synopsys.integration.bdio.graph.builder.LazyId)1 ExternalId (com.synopsys.integration.bdio.model.externalid.ExternalId)1 CodeLocation (com.synopsys.integration.detectable.detectable.codelocation.CodeLocation)1 YarnLock (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLock)1 YarnLockDependency (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockDependency)1 YarnLockResult (com.synopsys.integration.detectable.detectables.yarn.parse.YarnLockResult)1 YarnLockEntry (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntry)1 YarnLockEntryId (com.synopsys.integration.detectable.detectables.yarn.parse.entry.YarnLockEntryId)1 NameVersion (com.synopsys.integration.util.NameVersion)1 File (java.io.File)1 IOException (java.io.IOException)1 FileVisitResult (java.nio.file.FileVisitResult)1