diff --git a/build.gradle b/build.gradle index 9e24ad29ed..0246571d7e 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/detectable/src/main/java/com/blackduck/integration/detectable/detectables/yarn/packagejson/PackageJsonFiles.java b/detectable/src/main/java/com/blackduck/integration/detectable/detectables/yarn/packagejson/PackageJsonFiles.java index ccb6e653e8..3e5fabf347 100644 --- a/detectable/src/main/java/com/blackduck/integration/detectable/detectables/yarn/packagejson/PackageJsonFiles.java +++ b/detectable/src/main/java/com/blackduck/integration/detectable/detectables/yarn/packagejson/PackageJsonFiles.java @@ -38,42 +38,61 @@ public NullSafePackageJson read(File packageJsonFile) throws IOException { @NotNull public Collection readWorkspacePackageJsonFiles(File workspaceDir) throws IOException { - String forwardSlashedWorkspaceDirPath = deriveForwardSlashedPath(workspaceDir); File packageJsonFile = new File(workspaceDir, YarnLockDetectable.YARN_PACKAGE_JSON); List workspaceDirPatterns = extractWorkspaceDirPatterns(packageJsonFile); + if (workspaceDirPatterns.isEmpty()) { + logger.debug("No workspace patterns found in {}", packageJsonFile.getAbsolutePath()); + return new LinkedList<>(); + } + + List matchers = convertWorkspaceDirPatternsToPathMatchers(workspaceDirPatterns, workspaceDir); + Collection 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() { - @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() { + @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 convertWorkspaceDirPatternsToPathMatchers(List workspaceDirPatterns, File workspaceDir) { + List 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;