diff --git a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java index 27f1f8a60..eee2b7b14 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java +++ b/plugin/src/main/java/org/openrewrite/gradle/isolated/DefaultProjectParser.java @@ -888,12 +888,11 @@ private SourceFileStream parseGradleFiles( if (buildScriptPath.toString().endsWith(".gradle")) { gradleParser = gradleParser(); sourceFiles = gradleParser.parse(singleton(buildGradleFile.toPath()), baseDir, ctx); - gradleFileCount++; } else { sourceFiles = PlainTextParser.builder().build() .parse(singleton(buildGradleFile.toPath()), baseDir, ctx); - gradleFileCount++; } + gradleFileCount++; sourceFiles = sourceFiles.map(sourceFile -> sourceFile.withMarkers(sourceFile.getMarkers().add(gp))); alreadyParsed.add(project.getBuildscript().getSourceFile().toPath()); } diff --git a/plugin/src/main/kotlin/org/openrewrite/gradle/GradleProjectSpec.kt b/plugin/src/main/kotlin/org/openrewrite/gradle/GradleProjectSpec.kt index 6b147d45e..92620ec2f 100644 --- a/plugin/src/main/kotlin/org/openrewrite/gradle/GradleProjectSpec.kt +++ b/plugin/src/main/kotlin/org/openrewrite/gradle/GradleProjectSpec.kt @@ -22,10 +22,10 @@ import java.io.File * Utility to help with writing gradle projects to disk to assist with plugin testing */ class GradleProjectSpec( - val dir: File + private val dir: File ) { - val subprojects: MutableList = mutableListOf() - val sourceSets: MutableList = mutableListOf() + private val subprojects: MutableList = mutableListOf() + private val sourceSets: MutableList = mutableListOf() @Language("groovy") var groovyBuildScript: String? = null @@ -37,7 +37,7 @@ class GradleProjectSpec( @Language("groovy") var settingsGradle: String? = null fun settingsGradle(@Language("groovy") text: String) { - settingsGradle = text.trimIndent(); + settingsGradle = text.trimIndent() } @Language("yaml") @@ -52,12 +52,12 @@ class GradleProjectSpec( checkstyleXml = text.trimIndent() } - val propertiesFiles: MutableMap = mutableMapOf() + private val propertiesFiles: MutableMap = mutableMapOf() fun propertiesFile(name: String, @Language("properties") text: String) { propertiesFiles[name] = text } - val textFiles: MutableMap = mutableMapOf() + private val textFiles: MutableMap = mutableMapOf() fun textFile(name: String, text: String) { textFiles[name] = text } @@ -77,8 +77,17 @@ class GradleProjectSpec( fun build(): GradleProjectSpec { dir.mkdirs() - if (settingsGradle != null) { - File(dir, "settings.gradle").writeText(settingsGradle!!) + val settings = File(dir, "settings.gradle") + if(settingsGradle == null) { + val settingsText = "rootProject.name = \"${dir.name}\"\n" + if (subprojects.isEmpty()) { + settings.writeText("rootProject.name = \"${dir.name}\"\n") + } else { + val subprojectsDeclarations = subprojects.joinToString("\n") { subproject -> "include('${subproject.dir.name}')" } + settings.writeText(settingsText + subprojectsDeclarations) + } + } else { + settings.writeText(settingsGradle!!) } if (groovyBuildScript != null) { @@ -90,21 +99,21 @@ class GradleProjectSpec( } if (checkstyleXml != null) { - File(dir, "config/checkstyle/checkstyle.xml").apply{ + File(dir, "config/checkstyle/checkstyle.xml").apply{ parentFile.mkdirs() writeText(checkstyleXml!!) } } for (props in propertiesFiles.entries) { - File(dir, props.key).apply{ + File(dir, props.key).apply{ parentFile.mkdirs() writeText(props.value) } } for (text in textFiles.entries) { - File(dir, text.key).apply{ + File(dir, text.key).apply{ parentFile.mkdirs() writeText(text.value) } @@ -113,46 +122,37 @@ class GradleProjectSpec( for (sourceSet in sourceSets) { sourceSet.build(File(dir, "src")) } - - val settings = File(dir, "settings.gradle") - val settingsText = "rootProject.name = \"${dir.name}\"\n" - if (subprojects.isEmpty()) { - settings.writeText("rootProject.name = \"${dir.name}\"\n") - } else { - val subprojectsDeclarations = subprojects.joinToString("\n") { subproject -> "include('${subproject.dir.name}')" } - settings.writeText(settingsText + subprojectsDeclarations) - for (subproject in subprojects) { - subproject.build() - } + for (subproject in subprojects) { + subproject.build() } return this } } class GradleSourceSetSpec( - val name: String + private val name: String ) { - val javaSources: MutableList = mutableListOf() + private val javaSources: MutableList = mutableListOf() fun java(@Language("java") source: String) { javaSources.add(source.trimIndent()) } - val kotlinSources: MutableList = mutableListOf() + private val kotlinSources: MutableList = mutableListOf() fun kotlin(@Language("kotlin") source: String) { kotlinSources.add(source.trimIndent()) } - val propertiesFiles: MutableMap = mutableMapOf() + private val propertiesFiles: MutableMap = mutableMapOf() fun propertiesFile(name: String, @Language("properties") text: String) { propertiesFiles[name] = text } - val yamlFiles: MutableMap = mutableMapOf() + private val yamlFiles: MutableMap = mutableMapOf() fun yamlFile(name: String, @Language("yaml") text: String) { yamlFiles[name] = text } - val groovyClasses: MutableList = mutableListOf() + private val groovyClasses: MutableList = mutableListOf() fun groovyClass(@Language("groovy") source: String) { groovyClasses.add(source.trimIndent()) } @@ -162,7 +162,7 @@ class GradleSourceSetSpec( fun build(dir: File): GradleSourceSetSpec { dir.mkdirs() for (javaSource in javaSources) { - val peckage = if (javaSource.startsWith("package")) { + val packageDecl = if (javaSource.startsWith("package")) { "package\\s+([a-zA-Z0-9.]+);".toRegex(RegexOption.MULTILINE) .find(javaSource)!! .groupValues[1] @@ -170,18 +170,18 @@ class GradleSourceSetSpec( "" }.replace(".", "/") val clazz = ".*(class|interface|enum)\\s+([a-zA-Z0-9-_]+)".toRegex(RegexOption.MULTILINE).find(javaSource)!!.groupValues[2] - val path = if (peckage.isEmpty()) { + val path = if (packageDecl.isEmpty()) { "$name/java/$clazz.java" } else { - "$name/java/$peckage/$clazz.java" + "$name/java/$packageDecl/$clazz.java" } - File(dir, path).apply{ + File(dir, path).apply{ parentFile.mkdirs() writeText(javaSource) } } for (kotlinSource in kotlinSources) { - val peckage = if (kotlinSource.startsWith("package")) { + val packageDecl = if (kotlinSource.startsWith("package")) { "package\\s+([a-zA-Z0-9.]+)".toRegex(RegexOption.MULTILINE) .find(kotlinSource)!! .groupValues[1] @@ -189,18 +189,18 @@ class GradleSourceSetSpec( "" }.replace(".", "/") val clazz = ".*(class|interface|enum)\\s+([a-zA-Z0-9-_]+)".toRegex(RegexOption.MULTILINE).find(kotlinSource)!!.groupValues[2] - val path = if (peckage.isEmpty()) { + val path = if (packageDecl.isEmpty()) { "$name/kotlin/$clazz.kt" } else { - "$name/kotlin/$peckage/$clazz.kt" + "$name/kotlin/$packageDecl/$clazz.kt" } - File(dir, path).apply{ + File(dir, path).apply{ parentFile.mkdirs() writeText(kotlinSource) } } for (groovySource in groovyClasses) { - val peckage = if (groovySource.startsWith("package")) { + val packageDecl = if (groovySource.startsWith("package")) { "package\\s+([a-zA-Z0-9.]+);?".toRegex(RegexOption.MULTILINE) .find(groovySource)!! .groupValues[1] @@ -208,19 +208,19 @@ class GradleSourceSetSpec( "" }.replace(".", "/") val clazz = ".*(class|interface|enum)\\s+([a-zA-Z0-9-_]+)".toRegex(RegexOption.MULTILINE).find(groovySource)!!.groupValues[2] - val path = if (peckage.isEmpty()) { + val path = if (packageDecl.isEmpty()) { "$name/groovy/$clazz.groovy" } else { - "$name/groovy/$peckage/$clazz.groovy" + "$name/groovy/$packageDecl/$clazz.groovy" } - File(dir, path).apply{ + File(dir, path).apply{ parentFile.mkdirs() writeText(groovySource) } } if (propertiesFiles.isNotEmpty()) { for (props in propertiesFiles.entries) { - File(dir, "$name/resources/${props.key}").apply{ + File(dir, "$name/resources/${props.key}").apply{ parentFile.mkdirs() writeText(props.value) } @@ -228,7 +228,7 @@ class GradleSourceSetSpec( } if (yamlFiles.isNotEmpty()) { for (yaml in yamlFiles.entries) { - File(dir, "$name/resources/${yaml.key}").apply{ + File(dir, "$name/resources/${yaml.key}").apply{ parentFile.mkdirs() writeText(yaml.value) } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt index d2b7f9e9b..d149186c5 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt @@ -29,7 +29,7 @@ interface RewritePluginTest { GradleRunner.create() .withDebug(ManagementFactory.getRuntimeMXBean().inputArguments.toString().indexOf("-agentlib:jdwp") > 0) .withProjectDir(testDir) - .apply{ + .apply{ if (gradleVersion != null) { withGradleVersion(gradleVersion) } @@ -44,4 +44,9 @@ interface RewritePluginTest { val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) return currentVersion < GradleVersion.version("6.1") } + + fun lessThanGradle6_8(): Boolean { + val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) + return currentVersion < GradleVersion.version("6.8") + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 8aee56aaa..4ba59e8d8 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -21,7 +21,6 @@ package org.openrewrite.gradle import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.TaskOutcome -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.DisabledIf import org.junit.jupiter.api.condition.DisabledOnOs @@ -446,8 +445,8 @@ class RewriteRunTest : RewritePluginTest { rewriteYaml(""" type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.test.RemoveJacksonCore - displayName: Rename build.gradle to build.gradle.kts - description: Rename build.gradle to build.gradle.kts + displayName: Remove jackson-core + description: Remove jackson-core recipeList: - org.openrewrite.gradle.RemoveDependency: groupId: com.fasterxml.jackson.core @@ -516,6 +515,73 @@ class RewriteRunTest : RewritePluginTest { """.trimIndent()) } + + @DisabledIf("lessThanGradle6_8") + @Test + fun dependencyRepositoriesDeclaredInSettings( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + rewriteYaml(""" + type: specs.openrewrite.org/v1beta/recipe + name: org.openrewrite.test.UpgradeJacksonCore + displayName: Remove jackson-core + description: Remove jackson-core + recipeList: + - org.openrewrite.gradle.UpgradeDependencyVersion: + groupId: com.fasterxml.jackson.core + artifactId: jackson-core + nevVersion: 2.16.0 + """) + settingsGradle(""" + dependencyResolutionManagement { + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + } + """) + buildGradle(""" + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + dependencies { + implementation("com.fasterxml.jackson.core:jackson-core:2.15.1") + } + + rewrite { + activeRecipe("org.openrewrite.test.UpgradeJacksonCore") + } + """) + } + + val result = runGradle(projectDir, "rewriteRun") + val rewriteRunResult = result.task(":rewriteRun")!! + assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + + assertThat(projectDir.resolve("build.gradle").readText()) + //language=groovy + .isEqualTo(""" + plugins { + id("java") + id("org.openrewrite.rewrite") + } + + dependencies { + implementation("com.fasterxml.jackson.core:jackson-core:2.16.0") + } + + rewrite { + activeRecipe("org.openrewrite.test.UpgradeJacksonCore") + } + """.trimIndent()) + } + @Test fun mergeConfiguredAndAutodetectedStyles(@TempDir projectDir: File) { gradleProject(projectDir) { @@ -913,7 +979,6 @@ class RewriteRunTest : RewritePluginTest { ) } - @Disabled("Applicability tests are no longer supported for YAML recipes") @DisabledOnOs(OS.WINDOWS) // A file handle I haven't been able to track down is left open, causing JUnit to fail to clean up the directory on Windows @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/176") @Test @@ -937,10 +1002,9 @@ class RewriteRunTest : RewritePluginTest { name: com.example.TextToSam displayName: Changes contents of sam.txt description: Change contents of sam.txt to "sam" - applicability: - singleSource: - - org.openrewrite.FindSourceFiles: - filePattern: "**/sam.txt" + preconditions: + - org.openrewrite.FindSourceFiles: + filePattern: "**/sam.txt" recipeList: - org.openrewrite.text.ChangeText: toText: sam