|
23 | 23 |
|
24 | 24 | import java.io.File; |
25 | 25 | import java.io.FileFilter; |
| 26 | +import java.io.IOException; |
| 27 | +import java.nio.file.DirectoryStream; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.attribute.BasicFileAttributes; |
26 | 31 | import java.util.Arrays; |
27 | 32 | import java.util.List; |
| 33 | +import java.util.function.BiPredicate; |
28 | 34 | import java.util.stream.Collectors; |
| 35 | +import java.util.stream.Stream; |
29 | 36 |
|
30 | 37 | public abstract class ValueSourceDirectoryListing implements ValueSource<List<String>, ValueSourceDirectoryListing.DirectoryListingParameter> { |
31 | 38 |
|
32 | 39 |
|
33 | 40 | @Override |
34 | 41 | public List<String> obtain() { |
| 42 | + Path path = getParameters().getDir().get().toPath(); |
35 | 43 | File file = getParameters().getDir().get(); |
36 | | - File[] list = file.listFiles(new FileFilter() { |
| 44 | + try (Stream<Path> directoryStream = Files.find(path, 1, new BiPredicate<Path, BasicFileAttributes>() { |
37 | 45 | @Override |
38 | | - public boolean accept(File file) { |
39 | | - return file.isDirectory(); |
| 46 | + public boolean test(Path path, BasicFileAttributes basicFileAttributes) { |
| 47 | + return basicFileAttributes.isDirectory(); |
40 | 48 | } |
41 | | - }); |
42 | | - if (list == null) { |
43 | | - throw new RuntimeException("Failed to inspect: " + file.getAbsolutePath()); |
| 49 | + })) { |
| 50 | + return directoryStream.filter(x -> !getParameters().getExclusions().get().contains(x.getFileName().toString())) |
| 51 | + .filter(x -> getParameters().getRegexExclusions().get().stream().noneMatch(r -> x.getFileName().toString().matches(r))) |
| 52 | + .filter(x -> checkBuildFile(x, getParameters())) |
| 53 | + .map(x -> x.getFileName().toString()) |
| 54 | + .sorted() |
| 55 | + .collect(Collectors.toList()); |
| 56 | + |
| 57 | + } catch (IOException e) { |
| 58 | + throw new RuntimeException("Failed on " + file, e); |
44 | 59 | } |
45 | | - return Arrays.stream(list) |
46 | | - .map(File::getName) |
47 | | - .filter(x -> !getParameters().getExclusions().get().contains(x)) |
48 | | - .filter(x -> getParameters().getRegexExclusions().get().stream().noneMatch(x::matches)) |
49 | | - .sorted() |
50 | | - .collect(Collectors.toList()); |
51 | 60 |
|
52 | 61 |
|
53 | 62 | } |
54 | 63 |
|
| 64 | + private boolean checkBuildFile(Path x, DirectoryListingParameter parameters) { |
| 65 | + if (!parameters.getRequiresBuildFile().get()) { |
| 66 | + return true; |
| 67 | + } |
| 68 | + return Files.isRegularFile(x.resolve("build.gradle.kts")) || Files.isRegularFile(x.resolve("build.gradle")); |
| 69 | + } |
| 70 | + |
55 | 71 | interface DirectoryListingParameter extends ValueSourceParameters { |
56 | 72 |
|
| 73 | + Property<Boolean> getRequiresBuildFile(); |
| 74 | + |
57 | 75 | Property<File> getDir(); |
58 | 76 |
|
59 | 77 | SetProperty<String> getExclusions(); |
|
0 commit comments