Skip to content

Commit cd3fbd1

Browse files
committed
Fix exclusion of whole project directories, allow "**/" patterns to match files in the root project
1 parent bd6372d commit cd3fbd1

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
import java.nio.file.Files;
8181
import java.nio.file.Path;
8282
import java.nio.file.PathMatcher;
83+
import java.nio.file.Paths;
8384
import java.util.*;
8485
import java.util.concurrent.atomic.AtomicBoolean;
8586
import java.util.function.Consumer;
@@ -614,7 +615,7 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
614615
Collection<PathMatcher> exclusions = extension.getExclusions().stream()
615616
.map(pattern -> subproject.getProjectDir().toPath().getFileSystem().getPathMatcher("glob:" + pattern))
616617
.collect(toList());
617-
if (isExcluded(exclusions, subproject.getProjectDir().toPath())) {
618+
if (isExcluded(exclusions, baseDir.relativize(subproject.getProjectDir().toPath()))) {
618619
logger.lifecycle("Skipping project {} because it is excluded", subproject.getPath());
619620
return Stream.empty();
620621
}
@@ -909,31 +910,35 @@ private SourceFileStream parseGradleFiles(
909910
if (gradleParser == null) {
910911
gradleParser = gradleParser();
911912
}
912-
sourceFiles = Stream.concat(
913-
sourceFiles,
914-
gradleParser
915-
.parse(singleton(settingsGradleFile.toPath()), baseDir, ctx)
916-
.map(sourceFile -> {
917-
if (finalGs == null) {
918-
return sourceFile;
919-
}
920-
return sourceFile.withMarkers(sourceFile.getMarkers().add(finalGs));
921-
}));
922-
gradleFileCount++;
913+
if(!isExcluded(exclusions, settingsPath)) {
914+
sourceFiles = Stream.concat(
915+
sourceFiles,
916+
gradleParser
917+
.parse(singleton(settingsGradleFile.toPath()), baseDir, ctx)
918+
.map(sourceFile -> {
919+
if (finalGs == null) {
920+
return sourceFile;
921+
}
922+
return sourceFile.withMarkers(sourceFile.getMarkers().add(finalGs));
923+
}));
924+
gradleFileCount++;
925+
}
923926
alreadyParsed.add(settingsGradleFile.toPath());
924927
} else if (settingsGradleKtsFile.exists()) {
925928
Path settingsPath = baseDir.relativize(settingsGradleKtsFile.toPath());
926-
sourceFiles = Stream.concat(
927-
sourceFiles,
928-
PlainTextParser.builder().build()
929-
.parse(singleton(settingsGradleKtsFile.toPath()), baseDir, ctx)
930-
.map(sourceFile -> {
931-
if (finalGs == null) {
932-
return sourceFile;
933-
}
934-
return sourceFile.withMarkers(sourceFile.getMarkers().add(finalGs));
935-
}));
936-
gradleFileCount++;
929+
if(!isExcluded(exclusions, settingsPath)) {
930+
sourceFiles = Stream.concat(
931+
sourceFiles,
932+
PlainTextParser.builder().build()
933+
.parse(singleton(settingsGradleKtsFile.toPath()), baseDir, ctx)
934+
.map(sourceFile -> {
935+
if (finalGs == null) {
936+
return sourceFile;
937+
}
938+
return sourceFile.withMarkers(sourceFile.getMarkers().add(finalGs));
939+
}));
940+
gradleFileCount++;
941+
}
937942
alreadyParsed.add(settingsGradleKtsFile.toPath());
938943
}
939944
}
@@ -1099,6 +1104,11 @@ private boolean isExcluded(Collection<PathMatcher> exclusions, Path path) {
10991104
return true;
11001105
}
11011106
}
1107+
// PathMather will not evaluate the path "build.gradle" to be matched by the pattern "**/build.gradle"
1108+
// This is counter-intuitive for most users and would otherwise require separate exclusions for files at the root and files in subdirectories
1109+
if(!path.isAbsolute() && !path.startsWith(File.separator)) {
1110+
return isExcluded(exclusions, Paths.get("/" + path));
1111+
}
11021112
return false;
11031113
}
11041114

0 commit comments

Comments
 (0)