Skip to content

Commit

Permalink
Parse Gradle wrapper files separately (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Dec 1, 2023
1 parent f898978 commit 869ea13
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,9 @@ public Stream<SourceFile> parse(Project subproject, Set<Path> alreadyParsed, Exe
SourceFileStream gradleFiles = parseGradleFiles(exclusions, alreadyParsed, ctx);
sourceFileStream = sourceFileStream.concat(gradleFiles, gradleFiles.size());

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

SourceFileStream nonProjectResources = parseNonProjectResources(subproject, alreadyParsed, ctx, projectProvenance, sourceFileStream);
sourceFileStream = sourceFileStream.concat(nonProjectResources, nonProjectResources.size());

Expand Down Expand Up @@ -911,7 +914,7 @@ private SourceFileStream parseGradleFiles(
if (gradleParser == null) {
gradleParser = gradleParser();
}
if(!isExcluded(exclusions, settingsPath)) {
if (!isExcluded(exclusions, settingsPath)) {
sourceFiles = Stream.concat(
sourceFiles,
gradleParser
Expand All @@ -927,7 +930,7 @@ private SourceFileStream parseGradleFiles(
alreadyParsed.add(settingsGradleFile.toPath());
} else if (settingsGradleKtsFile.exists()) {
Path settingsPath = baseDir.relativize(settingsGradleKtsFile.toPath());
if(!isExcluded(exclusions, settingsPath)) {
if (!isExcluded(exclusions, settingsPath)) {
sourceFiles = Stream.concat(
sourceFiles,
PlainTextParser.builder().build()
Expand All @@ -948,12 +951,37 @@ private SourceFileStream parseGradleFiles(
}).concat(sourceFiles, gradleFileCount);
}

/**
* Parse Gradle wrapper files separately from other resource files, as Moderne CLI skips `parseNonProjectResources`.
*/
private SourceFileStream parseGradleWrapperFiles(Collection<PathMatcher> exclusions, Set<Path> alreadyParsed, ExecutionContext ctx) {
Stream<SourceFile> sourceFiles = Stream.empty();
int fileCount = 0;
if (project == project.getRootProject()) {
OmniParser omniParser = omniParser(alreadyParsed);
List<Path> gradleWrapperFiles = Stream.of(
"gradlew", "gradlew.bat",
"gradle/wrapper/gradle-wrapper.jar",
"gradle/wrapper/gradle-wrapper.properties")
.map(project::file)
.filter(File::exists)
.map(File::toPath)
.filter(it -> !isExcluded(exclusions, it))
.filter(omniParser::accept)
.collect(toList());
sourceFiles = omniParser.parse(gradleWrapperFiles, baseDir, ctx);
fileCount = gradleWrapperFiles.size();
}
return SourceFileStream.build("wrapper", s -> {
}).concat(sourceFiles, fileCount);
}

protected SourceFileStream parseNonProjectResources(Project subproject, Set<Path> alreadyParsed, ExecutionContext ctx, List<Marker> projectProvenance, Stream<SourceFile> sourceFiles) {
//Collect any additional yaml/properties/xml files that are NOT already in a source set.
OmniParser omniParser = omniParser(alreadyParsed);
List<Path> accepted = omniParser.acceptedPaths(baseDir, subproject.getProjectDir().toPath());
return SourceFileStream.build("", s -> {})
.concat(omniParser.parse(accepted, baseDir, ctx), accepted.size());
return SourceFileStream.build("", s -> {
}).concat(omniParser.parse(accepted, baseDir, ctx), accepted.size());
}

private OmniParser omniParser(Set<Path> alreadyParsed) {
Expand Down Expand Up @@ -1106,7 +1134,7 @@ private boolean isExcluded(Collection<PathMatcher> exclusions, Path path) {
}
// PathMather will not evaluate the path "build.gradle" to be matched by the pattern "**/build.gradle"
// This is counter-intuitive for most users and would otherwise require separate exclusions for files at the root and files in subdirectories
if(!path.isAbsolute() && !path.startsWith(File.separator)) {
if (!path.isAbsolute() && !path.startsWith(File.separator)) {
return isExcluded(exclusions, Paths.get("/" + path));
}
return false;
Expand Down
53 changes: 53 additions & 0 deletions plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1128,4 +1128,57 @@ class RewriteRunTest : RewritePluginTest {
""".trimIndent()
)
}

@Test
fun `parse gradle wrapper properties`(
@TempDir projectDir: File
) {
gradleProject(projectDir) {
buildGradle("""
plugins {
id("java")
id("org.openrewrite.rewrite")
}
repositories {
mavenLocal()
mavenCentral()
maven {
url = uri("https://oss.sonatype.org/content/repositories/snapshots")
}
}
rewrite {
activeRecipe("org.openrewrite.gradle.FindDistributionUrl")
}
""".trimIndent())
propertiesFile("gradle/wrapper/gradle-wrapper.properties", """
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""".trimIndent())
rewriteYaml("""
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.gradle.FindDistributionUrl
recipeList:
- org.openrewrite.properties.search.FindProperties:
propertyKey: distributionUrl
""".trimIndent())
}
val result = runGradle(projectDir, "rewriteRun")
val task = result.task(":rewriteRun")!!
assertThat(task.outcome).isEqualTo(TaskOutcome.SUCCESS)
val propertiesFile = projectDir.resolve("gradle/wrapper/gradle-wrapper.properties")
assertThat(propertiesFile.readText())
.isEqualTo("""
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=~~>https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
""".trimIndent()
)
}
}

0 comments on commit 869ea13

Please sign in to comment.