Skip to content

Optimize yarn workspace discovery #1430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {

group = 'com.blackduck.integration'

version = '10.5.0-SIGQA4-SNAPSHOT'
version = '10.5.0-SIGQA5-andrians.IDETECT-4713-yarn-workspace-glob-optimization-SNAPSHOT'

apply plugin: 'com.blackduck.integration.solution'
apply plugin: 'org.springframework.boot'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,61 @@ public NullSafePackageJson read(File packageJsonFile) throws IOException {

@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);

if (workspaceDirPatterns.isEmpty()) {
logger.debug("No workspace patterns found in {}", packageJsonFile.getAbsolutePath());
return new LinkedList<>();
}

List<PathMatcher> matchers = convertWorkspaceDirPatternsToPathMatchers(workspaceDirPatterns, workspaceDir);

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

Files.walkFileTree(workspaceDir.toPath(), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (file.getFileName().toString().equals(YarnLockDetectable.YARN_PACKAGE_JSON)) { // no need to try matching if not package.json
for (PathMatcher matcher : matchers) {
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);
break; // no need to match the same file multiple times
}
}
return FileVisitResult.CONTINUE;
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
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;
}

private List<PathMatcher> convertWorkspaceDirPatternsToPathMatchers(List<String> workspaceDirPatterns, File workspaceDir) {
List<PathMatcher> matchers = new LinkedList<>();
String forwardSlashedWorkspaceDirPath = deriveForwardSlashedPath(workspaceDir);
for (String workspaceDirPattern : workspaceDirPatterns) {
String globString = String.format("glob:%s/%s/package.json", forwardSlashedWorkspaceDirPath, workspaceDirPattern);
logger.trace("workspace subdir globString: {}", globString);
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(globString);
matchers.add(matcher);
}
return matchers;
}

@NotNull
private String deriveForwardSlashedPath(File file) {
String forwardSlashWorkspaceDirPath;
Expand Down