Skip to content

Commit

Permalink
Add excludes support
Browse files Browse the repository at this point in the history
  • Loading branch information
funbiscuit committed May 24, 2024
1 parent 13e2b7e commit fcd187b
Showing 1 changed file with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public class FormatCommand implements Callable<Integer> {
description = "A comma-separated list of file masks")
private List<String> masks = List.of();

@Option(names = {"-e", "--exclude"}, paramLabel = "<exclude>",
description = "File mask to exclude")
private List<String> excludes = List.of();

@Option(names = {"-d", "--dry"}, description = "Perform a dry run: no file modifications, only exit status")
private boolean dry;

Expand Down Expand Up @@ -77,7 +81,18 @@ private static Reporter createReporterForFile(Path file) {

@Override
public Integer call() throws Exception {
Predicate<String> fileNamePredicate = masks.stream()
Predicate<String> fileNameExclude = excludes.stream()
.map(m -> m
.replace(".", "\\.")
.replace("*", ".*")
.replace("?", ".")
.replace("+", "\\+"))
.map(Pattern::compile)
.map(Pattern::asMatchPredicate)
.reduce(Predicate::or)
.orElse(p -> false);

Predicate<String> fileNameInclude = masks.stream()
.map(m -> m
.replace(".", "\\.")
.replace("*", ".*")
Expand Down Expand Up @@ -110,7 +125,8 @@ public Integer call() throws Exception {
try (var stream = Files.walk(path, recursive ? Integer.MAX_VALUE : 1)) {
List<Path> dirFiles = stream
.filter(Files::isRegularFile)
.filter(p -> fileNamePredicate.test(p.toString()))
.filter(p -> !fileNameExclude.test(p.toString()))
.filter(p -> fileNameInclude.test(p.toString()))
.toList();
allFiles.addAll(dirFiles);
}
Expand Down

0 comments on commit fcd187b

Please sign in to comment.