refactor: extract findFilesByExtensions to FileUtils
Build & Deploy / build-and-deploy (push) Successful in 1m1s
Build & Deploy / build-and-deploy (push) Successful in 1m1s
- Added FileUtils.findFilesByExtensions(dir, extensions) — generic walk-and-collect utility for matching files by extension - ExecutableDetector.findAll() now delegates to it, dropping 13 lines of boilerplate
This commit is contained in:
@@ -36,19 +36,7 @@ public class ExecutableDetector {
|
||||
);
|
||||
|
||||
public List<String> findAll(Path dir) throws IOException {
|
||||
List<String> result = new ArrayList<>();
|
||||
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public @Nonnull FileVisitResult visitFile(@Nonnull Path f, @Nonnull BasicFileAttributes a) {
|
||||
String name = f.getFileName().toString().toLowerCase();
|
||||
if (name.endsWith(".exe") || name.endsWith(".com") || name.endsWith(".bat")) {
|
||||
result.add(dir.relativize(f).toString().replace('\\', '/'));
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
result.sort(String::compareTo);
|
||||
return result;
|
||||
return FileUtils.findFilesByExtensions(dir, Set.of(".exe", ".com", ".bat"));
|
||||
}
|
||||
|
||||
public String findMain(Path dir) throws IOException {
|
||||
|
||||
@@ -8,6 +8,9 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.SimpleFileVisitor;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/** Shared file-system utilities. */
|
||||
public final class FileUtils {
|
||||
@@ -30,4 +33,28 @@ public final class FileUtils {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk a directory tree and collect relative paths of files whose extension matches
|
||||
* one of the given extensions. Extensions should include the dot, e.g. {@code ".exe"}.
|
||||
* Results are sorted alphabetically.
|
||||
*/
|
||||
public static List<String> findFilesByExtensions(Path dir, Set<String> extensions) throws IOException {
|
||||
List<String> result = new ArrayList<>();
|
||||
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path f, BasicFileAttributes a) {
|
||||
String name = f.getFileName().toString().toLowerCase();
|
||||
for (String ext : extensions) {
|
||||
if (name.endsWith(ext)) {
|
||||
result.add(dir.relativize(f).toString().replace('\\', '/'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
});
|
||||
result.sort(String::compareTo);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user