Skip to content

Commit 4272fe2

Browse files
authored
Add GradleProject marker to gradle.properties (#268)
1 parent c9ce053 commit 4272fe2

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

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

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import org.openrewrite.marker.*;
6565
import org.openrewrite.marker.ci.BuildEnvironment;
6666
import org.openrewrite.polyglot.*;
67+
import org.openrewrite.properties.PropertiesParser;
6768
import org.openrewrite.quark.Quark;
6869
import org.openrewrite.quark.QuarkParser;
6970
import org.openrewrite.remote.Remote;
@@ -822,8 +823,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
822823

823824
SourceFileStream gradleWrapperFiles = parseGradleWrapperFiles(exclusions, alreadyParsed, ctx);
824825
sourceFileStream = sourceFileStream.concat(gradleWrapperFiles, gradleWrapperFiles.size());
825-
SourceFileStream gradlePropertiesFiles = parseGradlePropertiesFiles(subproject, alreadyParsed, ctx);
826-
sourceFileStream = sourceFileStream.concat(gradlePropertiesFiles, gradlePropertiesFiles.size());
827826

828827
SourceFileStream nonProjectResources = parseNonProjectResources(subproject, alreadyParsed, ctx);
829828
sourceFileStream = sourceFileStream.concat(nonProjectResources, nonProjectResources.size());
@@ -872,13 +871,15 @@ private SourceFileStream parseGradleFiles(
872871
Stream<SourceFile> sourceFiles = Stream.empty();
873872
int gradleFileCount = 0;
874873

874+
// build.gradle
875875
GradleParser gradleParser = null;
876+
GradleProject gradleProject = null;
876877
File buildGradleFile = subproject.getBuildscript().getSourceFile();
877878
if (buildGradleFile != null) {
878879
Path buildScriptPath = baseDir.relativize(buildGradleFile.toPath());
879880
if (!isExcluded(exclusions, buildScriptPath) && buildGradleFile.exists()) {
880881
alreadyParsed.add(buildScriptPath);
881-
GradleProject gp = GradleProjectBuilder.gradleProject(project);
882+
gradleProject = GradleProjectBuilder.gradleProject(project);
882883
if (buildScriptPath.toString().endsWith(".gradle")) {
883884
gradleParser = gradleParser();
884885
sourceFiles = gradleParser.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
@@ -887,11 +888,13 @@ private SourceFileStream parseGradleFiles(
887888
.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
888889
}
889890
gradleFileCount++;
890-
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(gp)));
891+
final GradleProject finalGradleProject = gradleProject;
892+
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(finalGradleProject)));
891893
alreadyParsed.add(buildGradleFile.toPath());
892894
}
893895
}
894896

897+
// settings.gradle
895898
if (subproject == project.getRootProject()) {
896899
File settingsGradleFile = subproject.file("settings.gradle");
897900
File settingsGradleKtsFile = subproject.file("settings.gradle.kts");
@@ -938,6 +941,22 @@ private SourceFileStream parseGradleFiles(
938941
}
939942
}
940943

944+
// gradle.properties
945+
File gradlePropertiesFile = subproject.file("gradle.properties");
946+
if (gradlePropertiesFile.exists() && gradleProject != null) {
947+
Path gradlePropertiesPath = baseDir.relativize(gradlePropertiesFile.toPath());
948+
if (!isExcluded(exclusions, gradlePropertiesPath)) {
949+
final GradleProject finalGradleProject = gradleProject;
950+
sourceFiles = Stream.concat(
951+
sourceFiles,
952+
new PropertiesParser()
953+
.parse(singleton(gradlePropertiesFile.toPath()), baseDir, ctx)
954+
.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(finalGradleProject))));
955+
gradleFileCount++;
956+
}
957+
alreadyParsed.add(gradlePropertiesFile.toPath());
958+
}
959+
941960
return SourceFileStream.build("", s -> {
942961
}).concat(sourceFiles, gradleFileCount);
943962
}
@@ -967,20 +986,6 @@ private SourceFileStream parseGradleWrapperFiles(Collection<PathMatcher> exclusi
967986
}).concat(sourceFiles, fileCount);
968987
}
969988

970-
/**
971-
* Parse Gradle properties files such that these get the GradleProject marker used in UpgradeDependencyVersion.
972-
*/
973-
private SourceFileStream parseGradlePropertiesFiles(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx) {
974-
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
975-
OmniParser omniParser = omniParser(alreadyParsed, subproject);
976-
List<Path> accepted = omniParser.acceptedPaths(baseDir, subproject.getProjectDir().toPath())
977-
.stream() // Only parse **/gradle.properties, while still leveraging the OmniParser for ignores/excludes
978-
.filter(it -> it.endsWith("gradle.properties"))
979-
.collect(toList());
980-
return SourceFileStream.build("", s -> {
981-
}).concat(omniParser.parse(accepted, baseDir, ctx), accepted.size());
982-
}
983-
984989
protected SourceFileStream parseNonProjectResources(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx) {
985990
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
986991
OmniParser omniParser = omniParser(alreadyParsed, subproject);

0 commit comments

Comments
 (0)