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);
}
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);
}
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;
}
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);
}
}
}
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;
}
Aggregations