Skip to content

Commit

Permalink
Add GradleProject marker to gradle.properties (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jan 23, 2024
1 parent c9ce053 commit 4272fe2
Showing 1 changed file with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import org.openrewrite.marker.*;
import org.openrewrite.marker.ci.BuildEnvironment;
import org.openrewrite.polyglot.*;
import org.openrewrite.properties.PropertiesParser;
import org.openrewrite.quark.Quark;
import org.openrewrite.quark.QuarkParser;
import org.openrewrite.remote.Remote;
Expand Down Expand Up @@ -822,8 +823,6 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe

SourceFileStream gradleWrapperFiles = parseGradleWrapperFiles(exclusions, alreadyParsed, ctx);
sourceFileStream = sourceFileStream.concat(gradleWrapperFiles, gradleWrapperFiles.size());
SourceFileStream gradlePropertiesFiles = parseGradlePropertiesFiles(subproject, alreadyParsed, ctx);
sourceFileStream = sourceFileStream.concat(gradlePropertiesFiles, gradlePropertiesFiles.size());

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

// build.gradle
GradleParser gradleParser = null;
GradleProject gradleProject = null;
File buildGradleFile = subproject.getBuildscript().getSourceFile();
if (buildGradleFile != null) {
Path buildScriptPath = baseDir.relativize(buildGradleFile.toPath());
if (!isExcluded(exclusions, buildScriptPath) && buildGradleFile.exists()) {
alreadyParsed.add(buildScriptPath);
GradleProject gp = GradleProjectBuilder.gradleProject(project);
gradleProject = GradleProjectBuilder.gradleProject(project);
if (buildScriptPath.toString().endsWith(".gradle")) {
gradleParser = gradleParser();
sourceFiles = gradleParser.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
Expand All @@ -887,11 +888,13 @@ private SourceFileStream parseGradleFiles(
.parse(singleton(buildGradleFile.toPath()), baseDir, ctx);
}
gradleFileCount++;
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(gp)));
final GradleProject finalGradleProject = gradleProject;
sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(finalGradleProject)));
alreadyParsed.add(buildGradleFile.toPath());
}
}

// settings.gradle
if (subproject == project.getRootProject()) {
File settingsGradleFile = subproject.file("settings.gradle");
File settingsGradleKtsFile = subproject.file("settings.gradle.kts");
Expand Down Expand Up @@ -938,6 +941,22 @@ private SourceFileStream parseGradleFiles(
}
}

// gradle.properties
File gradlePropertiesFile = subproject.file("gradle.properties");
if (gradlePropertiesFile.exists() && gradleProject != null) {
Path gradlePropertiesPath = baseDir.relativize(gradlePropertiesFile.toPath());
if (!isExcluded(exclusions, gradlePropertiesPath)) {
final GradleProject finalGradleProject = gradleProject;
sourceFiles = Stream.concat(
sourceFiles,
new PropertiesParser()
.parse(singleton(gradlePropertiesFile.toPath()), baseDir, ctx)
.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(finalGradleProject))));
gradleFileCount++;
}
alreadyParsed.add(gradlePropertiesFile.toPath());
}

return SourceFileStream.build("", s -> {
}).concat(sourceFiles, gradleFileCount);
}
Expand Down Expand Up @@ -967,20 +986,6 @@ private SourceFileStream parseGradleWrapperFiles(Collection<PathMatcher> exclusi
}).concat(sourceFiles, fileCount);
}

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

protected SourceFileStream parseNonProjectResources(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx) {
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
OmniParser omniParser = omniParser(alreadyParsed, subproject);
Expand Down

0 comments on commit 4272fe2

Please sign in to comment.