From 77bf015a49d7f135148e041a1209bae7d64dbdbc Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Mon, 4 Sep 2023 22:43:54 +0100 Subject: [PATCH 01/13] Add a few failing tests --- .../openrewrite/gradle/RewriteDryRunTask.java | 2 -- .../ResolveRewriteDependenciesTaskTest.kt | 34 +++++++++++++++++++ .../openrewrite/gradle/RewriteDiscoverTest.kt | 25 ++++++++++++++ .../openrewrite/gradle/RewriteDryRunTest.kt | 25 ++++++++++++++ .../org/openrewrite/gradle/RewriteRunTest.kt | 25 ++++++++++++++ 5 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java index be87cf955..b8eec3f16 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java @@ -18,13 +18,11 @@ import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.specs.Specs; -import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.OutputFile; import org.gradle.api.tasks.TaskAction; import javax.inject.Inject; import java.io.File; -import java.nio.file.Path; public class RewriteDryRunTask extends AbstractRewriteTask { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt new file mode 100644 index 000000000..c7e35b370 --- /dev/null +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt @@ -0,0 +1,34 @@ +package org.openrewrite.gradle + +import org.assertj.core.api.Assertions.assertThat +import org.gradle.testkit.runner.TaskOutcome +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.File + +class ResolveRewriteDependenciesTaskTest : RewritePluginTest { + // TODO: Extract out into RewritePluginTest? Does JUnit support that? + @Test + fun `rewriteResolveDependencies satisfies the configuration cache`( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + buildGradle(""" + plugins { + id("org.openrewrite.rewrite") + } + + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + """) + } + val result = runGradle(projectDir, "rewriteResolveDependencies", "--configuration-cache") + val rewriteDryRunResult = result.task(":rewriteResolveDependencies")!! + assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + } +} \ No newline at end of file diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt index f2bd14a59..d9dfd6411 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt @@ -61,4 +61,29 @@ class RewriteDiscoverTest : RewritePluginTest { assertThat(result.output).contains("Configured with 2 active recipes and 1 active styles.") } + + // TODO: Extract out into RewritePluginTest? Does JUnit support that? + @Test + fun `rewriteDiscover satisfies the configuration cache`( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + buildGradle(""" + plugins { + id("org.openrewrite.rewrite") + } + + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + """) + } + val result = runGradle(projectDir, "rewriteDiscover", "--configuration-cache") + val rewriteDryRunResult = result.task(":rewriteDiscover")!! + assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index 7a2d37da7..c034e599d 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -193,4 +193,29 @@ class RewriteDryRunTest : RewritePluginTest { assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(File(projectDir, "build/reports/rewrite/rewrite.patch").exists()).isTrue } + + // TODO: Extract out into RewritePluginTest? Does JUnit support that? + @Test + fun `rewriteDryRun satisfies the configuration cache`( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + buildGradle(""" + plugins { + id("org.openrewrite.rewrite") + } + + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + """) + } + val result = runGradle(projectDir, "rewriteDryRun", "--configuration-cache") + val rewriteDryRunResult = result.task(":rewriteDryRun")!! + assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index a86891f7d..acd4e0931 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -972,4 +972,29 @@ class RewriteRunTest : RewritePluginTest { .`as`("Applicability test should have prevented this file from being altered") .isEqualTo("jonathan") } + + // TODO: Extract out into RewritePluginTest? Does JUnit support that? + @Test + fun `rewriteRun satisfies the configuration cache`( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + buildGradle(""" + plugins { + id("org.openrewrite.rewrite") + } + + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + """) + } + val result = runGradle(projectDir, "rewriteRun", "--configuration-cache") + val rewriteDryRunResult = result.task(":rewriteRun")!! + assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + } } From e8e0402533571b651b611a89f92160aba7bf9844 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Tue, 5 Sep 2023 22:09:19 +0100 Subject: [PATCH 02/13] Bump minimum Gradle version to 6.8.3 to allow for configuration cache compatibility and to avoid bugs with dependency resolution on Gradle 6.6, which is the first version that the config cache is introduced --- .../rewrite/build/GradleVersionData.groovy | 2 +- plugin/build.gradle.kts | 13 ++++++----- .../gradle/DelegatingRewriteExtension.java | 2 +- .../ResolveRewriteDependenciesTask.java | 22 ++++++++++++++----- .../openrewrite/gradle/RewriteExtension.java | 2 +- .../org/openrewrite/gradle/RewritePlugin.java | 2 ++ .../openrewrite/gradle/RewriteDiscoverTest.kt | 4 +++- .../openrewrite/gradle/RewriteDryRunTest.kt | 4 +++- .../openrewrite/gradle/RewritePluginTest.kt | 1 - ...t.kt => RewriteResolveDependenciesTest.kt} | 21 ++++++++++++++++-- .../org/openrewrite/gradle/RewriteRunTest.kt | 3 ++- 11 files changed, 56 insertions(+), 20 deletions(-) rename plugin/src/test/kotlin/org/openrewrite/gradle/{ResolveRewriteDependenciesTaskTest.kt => RewriteResolveDependenciesTest.kt} (56%) diff --git a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy index b6aefc183..81ecc28e3 100644 --- a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy +++ b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy @@ -24,7 +24,7 @@ class GradleVersionData { .findAll { !it.rcFor || it.activeRc } // filter out inactive rcs .findAll { !it.milestoneFor } // filter out milestones . collectEntries { [(it.version): VersionNumber.parse(it.version as String)] } - .findAll { it.value.major >= 5 } // only 5.0 and above + .findAll { it.value >= VersionNumber.parse("6.8.3") } // only 6.8.3 and above .inject([] as List>) { releasesToTest, version -> // only test against latest patch versions if (!releasesToTest.any { it.value.major == version.value.major && it.value.minor == version.value.minor }) { releasesToTest + version diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 194c0a184..6fb2615d7 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -140,6 +140,7 @@ dependencies { project.rootProject.tasks.getByName("postRelease").dependsOn(project.tasks.getByName("publishPlugins")) +// TODO: Run this task and confirm it passes tasks.register("testGradleReleases") { jvmArgumentProviders.add(GradleVersionsCommandLineArgumentProvider(GradleVersionData::getReleasedVersions)) } @@ -148,7 +149,7 @@ tasks.register("testGradleNightlies") { jvmArgumentProviders.add(GradleVersionsCommandLineArgumentProvider(GradleVersionData::getNightlyVersions)) } -tasks.withType() { +tasks.withType { useJUnitPlatform() // Remove this once we've fixed https://github.com/openrewrite/rewrite-gradle-plugin/issues/132 setForkEvery(1) @@ -193,16 +194,16 @@ tasks.named("test") { ) } -val testGradle4 = tasks.register("testGradle4") { - systemProperty("org.openrewrite.test.gradleVersion", "4.0") +val testGradle6Dot8Dot3 = tasks.register("testGradle6Dot8Dot3") { + systemProperty("org.openrewrite.test.gradleVersion", "6.8.3") systemProperty("jarLocationForTest", tasks.named("jar").get().archiveFile.get().asFile.absolutePath) - // Gradle 4.0 predates support for Java 11 + // Gradle 6.8.3 predates support for Java 17 javaLauncher.set(javaToolchains.launcherFor { - languageVersion.set(JavaLanguageVersion.of(8)) + languageVersion.set(JavaLanguageVersion.of(11)) }) } tasks.named("check").configure { - dependsOn(testGradle4) + dependsOn(testGradle6Dot8Dot3) } configure { diff --git a/plugin/src/main/java/org/openrewrite/gradle/DelegatingRewriteExtension.java b/plugin/src/main/java/org/openrewrite/gradle/DelegatingRewriteExtension.java index dcb4a26d0..de2a5dc57 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/DelegatingRewriteExtension.java +++ b/plugin/src/main/java/org/openrewrite/gradle/DelegatingRewriteExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright ${year} the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java b/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java index 2cd0f2858..dd8918648 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java @@ -15,11 +15,13 @@ */ package org.openrewrite.gradle; +import groovy.lang.Closure; import org.gradle.api.DefaultTask; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.Dependency; import org.gradle.api.artifacts.dsl.DependencyHandler; +import org.gradle.api.attributes.Attribute; import org.gradle.api.attributes.Bundling; import org.gradle.api.attributes.Category; import org.gradle.api.attributes.LibraryElements; @@ -29,6 +31,7 @@ import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.TaskAction; +import javax.inject.Inject; import java.io.File; import java.util.Arrays; import java.util.Set; @@ -37,7 +40,7 @@ import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; import static org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE; -public class ResolveRewriteDependenciesTask extends DefaultTask { +public abstract class ResolveRewriteDependenciesTask extends DefaultTask { private Set resolvedDependencies; private Configuration configuration; protected RewriteExtension extension; @@ -52,6 +55,9 @@ public ResolveRewriteDependenciesTask setExtension(RewriteExtension extension) { return this; } + @Inject + public abstract ObjectFactory getObjectFactory(); + @Internal public Set getResolvedDependencies() { if (resolvedDependencies == null) { @@ -94,17 +100,23 @@ public Set getResolvedDependencies() { Configuration detachedConf = project.getConfigurations().detachedConfiguration(dependencies); try { - ObjectFactory objectFactory = project.getObjects(); + ObjectFactory objectFactory = getObjectFactory(); detachedConf.attributes(attributes -> { - // Taken from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails + // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails attributes.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objectFactory.named(LibraryElements.class, LibraryElements.JAR)); attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); - attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); + try { + attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); + } catch (NoClassDefFoundError e) { + // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always + // try this attribute instead + attributes.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm"); + } }); } catch (NoClassDefFoundError e) { - // Old versions of gradle don't have all of these attributes and that's OK + // Old versions of Gradle don't have all of these attributes and that's OK } resolvedDependencies = detachedConf.resolve(); diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java index 0fc682431..f22c9b7df 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2023 the original author or authors. + * Copyright ${year} the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 8949fe13f..8356dd9d4 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -48,6 +48,7 @@ public void apply(Project project) { // We use this method of task creation because it works on old versions of Gradle // Don't replace with TaskContainer.register() (introduced in 4.9), or another overload of create() (introduced in 4.7) + // TODO: Replace with getTasks().register() ResolveRewriteDependenciesTask resolveRewriteDependenciesTask = project.getTasks().create("rewriteResolveDependencies", ResolveRewriteDependenciesTask.class) .setExtension(extension) .setConfiguration(rewriteConf); @@ -91,6 +92,7 @@ private static void configureProject(Project project, RewriteExtension extension //Collect Java metadata for each project (used for Java Provenance) //Using the older javaConvention because we need to support older versions of gradle. + // TODO: Can we migrate away fron JavaPluginConvention? @SuppressWarnings("deprecation") JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); javaConvention.getSourceSets().all(sourceSet -> { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt index d9dfd6411..2d7ba3e57 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt @@ -17,6 +17,7 @@ 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.io.TempDir import java.io.File @@ -64,7 +65,8 @@ class RewriteDiscoverTest : RewritePluginTest { // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - fun `rewriteDiscover satisfies the configuration cache`( + @Disabled + fun `rewriteDiscover is compatible with the configuration cache`( @TempDir projectDir: File ) { gradleProject(projectDir) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index c034e599d..ecd0fb46e 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -17,6 +17,7 @@ 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.io.TempDir @@ -196,7 +197,8 @@ class RewriteDryRunTest : RewritePluginTest { // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - fun `rewriteDryRun satisfies the configuration cache`( + @Disabled + fun `rewriteDryRun is compatible with the configuration cache`( @TempDir projectDir: File ) { gradleProject(projectDir) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt index 7ff56fb66..06ec0fee1 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt @@ -39,7 +39,6 @@ interface RewritePluginTest { .forwardOutput() .build() - fun lessThanGradle6_1(): Boolean { val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) return currentVersion < GradleVersion.version("6.1") diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt similarity index 56% rename from plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt rename to plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt index c7e35b370..3c306cf28 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/ResolveRewriteDependenciesTaskTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt @@ -1,15 +1,32 @@ +/* + * Copyright ${year} the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 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.io.TempDir import java.io.File -class ResolveRewriteDependenciesTaskTest : RewritePluginTest { +class RewriteResolveDependenciesTest : RewritePluginTest { // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - fun `rewriteResolveDependencies satisfies the configuration cache`( + @Disabled + fun `rewriteResolveDependencies is compatible with the configuration cache`( @TempDir projectDir: File ) { gradleProject(projectDir) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index acd4e0931..e37de46d8 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -975,7 +975,8 @@ class RewriteRunTest : RewritePluginTest { // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - fun `rewriteRun satisfies the configuration cache`( + @Disabled + fun `rewriteRun is compatible with the configuration cache`( @TempDir projectDir: File ) { gradleProject(projectDir) { From aa4a2589c4cb845a267c1db92ffedc68cd087b1a Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Wed, 6 Sep 2023 22:43:18 +0100 Subject: [PATCH 03/13] Remove ResolveRewriteDependenciesTask and fix one config cache problem w/ RewriteDryRunTask --- .../rewrite/build/GradleVersionData.groovy | 1 + plugin/build.gradle.kts | 1 + .../gradle/AbstractRewriteTask.java | 19 ++- .../ResolveRewriteDependenciesTask.java | 131 ------------------ .../org/openrewrite/gradle/RewritePlugin.java | 99 +++++++++++-- .../openrewrite/gradle/RewriteDryRunTest.kt | 1 - .../gradle/RewriteResolveDependenciesTest.kt | 51 ------- 7 files changed, 103 insertions(+), 200 deletions(-) delete mode 100755 plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java delete mode 100644 plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt diff --git a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy index 81ecc28e3..65b97622d 100644 --- a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy +++ b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy @@ -24,6 +24,7 @@ class GradleVersionData { .findAll { !it.rcFor || it.activeRc } // filter out inactive rcs .findAll { !it.milestoneFor } // filter out milestones . collectEntries { [(it.version): VersionNumber.parse(it.version as String)] } + // TODO: Downgrade to 4.3 (or even 4.0 is Provider.map isn't needed) as per https://github.com/openrewrite/rewrite-gradle-plugin/issues/227#issuecomment-1707455588 unless future comments change this .findAll { it.value >= VersionNumber.parse("6.8.3") } // only 6.8.3 and above .inject([] as List>) { releasesToTest, version -> // only test against latest patch versions if (!releasesToTest.any { it.value.major == version.value.major && it.value.minor == version.value.minor }) { diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 6fb2615d7..63387828f 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -194,6 +194,7 @@ tasks.named("test") { ) } +// TODO: Downgrade to 4.3 (or even 4.0 is Provider.map isn't needed) as per https://github.com/openrewrite/rewrite-gradle-plugin/issues/227#issuecomment-1707455588 unless future comments change this val testGradle6Dot8Dot3 = tasks.register("testGradle6Dot8Dot3") { systemProperty("org.openrewrite.test.gradleVersion", "6.8.3") systemProperty("jarLocationForTest", tasks.named("jar").get().archiveFile.get().asFile.absolutePath) diff --git a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java index 8ac585073..54ec6beab 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java @@ -16,18 +16,20 @@ package org.openrewrite.gradle; import org.gradle.api.DefaultTask; +import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.options.Option; import java.io.File; import java.nio.file.Path; +import java.util.Collections; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public abstract class AbstractRewriteTask extends DefaultTask { - protected ResolveRewriteDependenciesTask resolveDependenciesTask; + protected Provider> resolvedDependencies; protected boolean dumpGcActivity; protected GradleProjectParser gpp; protected RewriteExtension extension; @@ -38,9 +40,8 @@ public T setExtension(RewriteExtension extension return (T) this; } - public T setResolveDependenciesTask(ResolveRewriteDependenciesTask resolveDependenciesTask) { - this.resolveDependenciesTask = resolveDependenciesTask; - this.dependsOn(resolveDependenciesTask); + public T setResolvedDependencies(Provider> resolvedDependencies) { + this.resolvedDependencies = resolvedDependencies; //noinspection unchecked return (T) this; } @@ -61,10 +62,14 @@ protected T getProjectParser() { if(extension == null) { throw new IllegalArgumentException("Must configure extension"); } - if (resolveDependenciesTask == null) { - throw new IllegalArgumentException("Must configure resolveDependenciesTask"); + if (resolvedDependencies == null) { + throw new IllegalArgumentException("Must configure resolvedDependencies"); } - Set classpath = resolveDependenciesTask.getResolvedDependencies().stream() + Set deps = resolvedDependencies.getOrNull(); + if (deps == null) { + deps = Collections.emptySet(); + } + Set classpath = deps.stream() .map(File::toPath) .collect(Collectors.toSet()); gpp = new DelegatingProjectParser(getProject(), extension, classpath); diff --git a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java b/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java deleted file mode 100755 index dd8918648..000000000 --- a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright ${year} the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle; - -import groovy.lang.Closure; -import org.gradle.api.DefaultTask; -import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.artifacts.Dependency; -import org.gradle.api.artifacts.dsl.DependencyHandler; -import org.gradle.api.attributes.Attribute; -import org.gradle.api.attributes.Bundling; -import org.gradle.api.attributes.Category; -import org.gradle.api.attributes.LibraryElements; -import org.gradle.api.attributes.Usage; -import org.gradle.api.attributes.java.TargetJvmEnvironment; -import org.gradle.api.model.ObjectFactory; -import org.gradle.api.tasks.Internal; -import org.gradle.api.tasks.TaskAction; - -import javax.inject.Inject; -import java.io.File; -import java.util.Arrays; -import java.util.Set; -import java.util.stream.Stream; - -import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; -import static org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE; - -public abstract class ResolveRewriteDependenciesTask extends DefaultTask { - private Set resolvedDependencies; - private Configuration configuration; - protected RewriteExtension extension; - - public ResolveRewriteDependenciesTask setConfiguration(Configuration configuration) { - this.configuration = configuration; - return this; - } - - public ResolveRewriteDependenciesTask setExtension(RewriteExtension extension) { - this.extension = extension; - return this; - } - - @Inject - public abstract ObjectFactory getObjectFactory(); - - @Internal - public Set getResolvedDependencies() { - if (resolvedDependencies == null) { - String rewriteVersion = extension.getRewriteVersion(); - Project project = getProject(); - DependencyHandler deps = project.getDependencies(); - Dependency[] dependencies = new Dependency[]{ - deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-groovy:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-gradle:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-hcl:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), - deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-maven:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-properties:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-protobuf:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-xml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-yaml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-polyglot:" + extension.getRewritePolyglotVersion()), - deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), - - // This is an optional dependency of rewrite-java needed when projects also apply the checkstyle plugin - deps.create("com.puppycrawl.tools:checkstyle:" + extension.getCheckstyleToolsVersion()), - deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), - deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) - }; - if (configuration != null) { - dependencies = Stream.concat( - Arrays.stream(dependencies), - configuration.getDependencies().stream() - ).toArray(Dependency[]::new); - } - // By using a detached configuration, we separate this dependency resolution from the rest of the project's - // configuration. This also means that Gradle has no criteria with which to select between variants of - // dependencies which expose differing capabilities. So those must be manually configured - Configuration detachedConf = project.getConfigurations().detachedConfiguration(dependencies); - - try { - ObjectFactory objectFactory = getObjectFactory(); - detachedConf.attributes(attributes -> { - // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails - attributes.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); - attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); - attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objectFactory.named(LibraryElements.class, LibraryElements.JAR)); - attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); - try { - attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); - } catch (NoClassDefFoundError e) { - // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always - // try this attribute instead - attributes.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm"); - } - }); - } catch (NoClassDefFoundError e) { - // Old versions of Gradle don't have all of these attributes and that's OK - } - - resolvedDependencies = detachedConf.resolve(); - } - return resolvedDependencies; - } - - @TaskAction - void run() { - getResolvedDependencies(); - } -} diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 8356dd9d4..d7432d3fc 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -15,12 +15,32 @@ */ package org.openrewrite.gradle; -import org.gradle.api.*; +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.Task; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.artifacts.dsl.DependencyHandler; +import org.gradle.api.attributes.Attribute; +import org.gradle.api.attributes.Bundling; +import org.gradle.api.attributes.Category; +import org.gradle.api.attributes.LibraryElements; +import org.gradle.api.attributes.Usage; +import org.gradle.api.attributes.java.TargetJvmEnvironment; +import org.gradle.api.model.ObjectFactory; import org.gradle.api.plugins.JavaBasePlugin; import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.plugins.quality.CheckstyleExtension; import org.gradle.api.plugins.quality.CheckstylePlugin; +import org.gradle.api.provider.Provider; + +import java.io.File; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Stream; + +import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; +import static org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE; /** * When applied to the root project of a multi-project build, applies to all subprojects. @@ -32,6 +52,8 @@ @SuppressWarnings("unused") public class RewritePlugin implements Plugin { + private Set resolvedDependencies; + public void apply(Project project) { boolean isRootProject = project == project.getRootProject(); if (!isRootProject && project.getRootProject().getPluginManager().hasPlugin("org.openrewrite.rewrite")) { @@ -46,26 +68,21 @@ public void apply(Project project) { // Rewrite module dependencies put here will be available to all rewrite tasks Configuration rewriteConf = project.getConfigurations().maybeCreate("rewrite"); - // We use this method of task creation because it works on old versions of Gradle - // Don't replace with TaskContainer.register() (introduced in 4.9), or another overload of create() (introduced in 4.7) - // TODO: Replace with getTasks().register() - ResolveRewriteDependenciesTask resolveRewriteDependenciesTask = project.getTasks().create("rewriteResolveDependencies", ResolveRewriteDependenciesTask.class) - .setExtension(extension) - .setConfiguration(rewriteConf); + Provider> resolvedDependenciesProvider = project.provider(() -> getResolvedDependencies(project, extension, rewriteConf)); RewriteRunTask rewriteRun = project.getTasks().create("rewriteRun", RewriteRunTask.class) .setExtension(extension) - .setResolveDependenciesTask(resolveRewriteDependenciesTask); + .setResolvedDependencies(resolvedDependenciesProvider); rewriteRun.dependsOn(rewriteConf); RewriteDryRunTask rewriteDryRun = project.getTasks().create("rewriteDryRun", RewriteDryRunTask.class) .setExtension(extension) - .setResolveDependenciesTask(resolveRewriteDependenciesTask); + .setResolvedDependencies(resolvedDependenciesProvider); rewriteDryRun.dependsOn(rewriteConf); RewriteDiscoverTask rewriteDiscover = project.getTasks().create("rewriteDiscover", RewriteDiscoverTask.class) .setExtension(extension) - .setResolveDependenciesTask(resolveRewriteDependenciesTask); + .setResolvedDependencies(resolvedDependenciesProvider); rewriteDiscover.dependsOn(rewriteConf); if(isRootProject) { @@ -103,4 +120,66 @@ private static void configureProject(Project project, RewriteExtension extension }); }); } + + private Set getResolvedDependencies(Project project, RewriteExtension extension, Configuration rewriteConf) { + if (resolvedDependencies == null) { + String rewriteVersion = extension.getRewriteVersion(); + DependencyHandler deps = project.getDependencies(); + Dependency[] dependencies = new Dependency[]{ + deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-groovy:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-gradle:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-hcl:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), + deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-maven:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-properties:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-protobuf:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-xml:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-yaml:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-polyglot:" + extension.getRewritePolyglotVersion()), + deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), + + // This is an optional dependency of rewrite-java needed when projects also apply the checkstyle plugin + deps.create("com.puppycrawl.tools:checkstyle:" + extension.getCheckstyleToolsVersion()), + deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), + deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) + }; + dependencies = Stream.concat( + Arrays.stream(dependencies), + rewriteConf.getDependencies().stream() + ).toArray(Dependency[]::new); + // By using a detached configuration, we separate this dependency resolution from the rest of the project's + // configuration. This also means that Gradle has no criteria with which to select between variants of + // dependencies which expose differing capabilities. So those must be manually configured + Configuration detachedConf = project.getConfigurations().detachedConfiguration(dependencies); + + try { + ObjectFactory objectFactory = project.getObjects(); + detachedConf.attributes(attributes -> { + // Adapted from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails + attributes.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); + attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); + attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objectFactory.named(LibraryElements.class, LibraryElements.JAR)); + attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); + try { + attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); + } catch (NoClassDefFoundError e) { + // Old versions of Gradle don't have the class TargetJvmEnvironment and that's OK, we can always + // try this attribute instead + attributes.attribute(Attribute.of("org.gradle.jvm.environment", String.class), "standard-jvm"); + } + }); + } catch (NoClassDefFoundError e) { + // Old versions of Gradle don't have all of these attributes and that's OK + } + + resolvedDependencies = detachedConf.resolve(); + } + return resolvedDependencies; + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index ecd0fb46e..6bea64338 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -197,7 +197,6 @@ class RewriteDryRunTest : RewritePluginTest { // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - @Disabled fun `rewriteDryRun is compatible with the configuration cache`( @TempDir projectDir: File ) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt deleted file mode 100644 index 3c306cf28..000000000 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright ${year} the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -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.io.TempDir -import java.io.File - -class RewriteResolveDependenciesTest : RewritePluginTest { - // TODO: Extract out into RewritePluginTest? Does JUnit support that? - @Test - @Disabled - fun `rewriteResolveDependencies is compatible with the configuration cache`( - @TempDir projectDir: File - ) { - gradleProject(projectDir) { - buildGradle(""" - plugins { - id("org.openrewrite.rewrite") - } - - repositories { - mavenLocal() - mavenCentral() - maven { - url = uri("https://oss.sonatype.org/content/repositories/snapshots") - } - } - """) - } - val result = runGradle(projectDir, "rewriteResolveDependencies", "--configuration-cache") - val rewriteDryRunResult = result.task(":rewriteResolveDependencies")!! - assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) - } -} \ No newline at end of file From 2a4cf10937713843d94b201d13135f96b8b67420 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Wed, 6 Sep 2023 23:26:33 +0100 Subject: [PATCH 04/13] Fix another config cache problem w/ RewriteDryRunTask --- .../openrewrite/gradle/RewriteDryRunTask.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java index b8eec3f16..2592bc39d 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java @@ -15,6 +15,7 @@ */ package org.openrewrite.gradle; +import org.gradle.api.file.ProjectLayout; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.specs.Specs; @@ -24,15 +25,26 @@ import javax.inject.Inject; import java.io.File; -public class RewriteDryRunTask extends AbstractRewriteTask { +public abstract class RewriteDryRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteDryRunTask.class); + @Inject + public abstract ProjectLayout getProjectLayout(); + // This must return File, rather than Path. // On Gradle 4.0 annotating something returning a Path with @OutputFile triggers a bug that deadlocks Gradle @OutputFile public File getReportPath() { - return getProject().getBuildDir().toPath().resolve("reports").resolve("rewrite").resolve("rewrite.patch").toFile(); + return getProjectLayout() + .getBuildDirectory() + .get() + .getAsFile() + .toPath() + .resolve("reports") + .resolve("rewrite") + .resolve("rewrite.patch") + .toFile(); } @Inject From 8dbf2803ab9264db430210a5e951efa4ec19eefb Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Mon, 15 Jan 2024 00:08:24 +0000 Subject: [PATCH 05/13] More work done; tests are still failing... --- .tool-versions | 1 - .../gradle/rewrite/build/GradleVersionData.groovy | 3 +-- plugin/build.gradle.kts | 12 +++++------- .../org/openrewrite/gradle/AbstractRewriteTask.java | 9 +++++++++ .../org/openrewrite/gradle/RewriteDiscoverTask.java | 2 +- .../org/openrewrite/gradle/RewriteDryRunTask.java | 4 ---- .../java/org/openrewrite/gradle/RewritePlugin.java | 1 - .../java/org/openrewrite/gradle/RewriteRunTask.java | 2 +- .../org/openrewrite/gradle/RewriteDiscoverTest.kt | 2 -- .../org/openrewrite/gradle/RewriteDryRunTest.kt | 1 - .../kotlin/org/openrewrite/gradle/RewriteRunTest.kt | 2 -- 11 files changed, 17 insertions(+), 22 deletions(-) delete mode 100644 .tool-versions diff --git a/.tool-versions b/.tool-versions deleted file mode 100644 index 2b5bddd9e..000000000 --- a/.tool-versions +++ /dev/null @@ -1 +0,0 @@ -java zulu-17.38.21 diff --git a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy index 65b97622d..b6aefc183 100644 --- a/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy +++ b/buildSrc/src/main/groovy/org/gradle/rewrite/build/GradleVersionData.groovy @@ -24,8 +24,7 @@ class GradleVersionData { .findAll { !it.rcFor || it.activeRc } // filter out inactive rcs .findAll { !it.milestoneFor } // filter out milestones . collectEntries { [(it.version): VersionNumber.parse(it.version as String)] } - // TODO: Downgrade to 4.3 (or even 4.0 is Provider.map isn't needed) as per https://github.com/openrewrite/rewrite-gradle-plugin/issues/227#issuecomment-1707455588 unless future comments change this - .findAll { it.value >= VersionNumber.parse("6.8.3") } // only 6.8.3 and above + .findAll { it.value.major >= 5 } // only 5.0 and above .inject([] as List>) { releasesToTest, version -> // only test against latest patch versions if (!releasesToTest.any { it.value.major == version.value.major && it.value.minor == version.value.minor }) { releasesToTest + version diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index 18d993678..b348e5b79 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -141,7 +141,6 @@ dependencies { project.rootProject.tasks.getByName("postRelease").dependsOn(project.tasks.getByName("publishPlugins")) -// TODO: Run this task and confirm it passes tasks.register("testGradleReleases") { jvmArgumentProviders.add(GradleVersionsCommandLineArgumentProvider(GradleVersionData::getReleasedVersions)) } @@ -195,17 +194,16 @@ tasks.named("test") { ) } -// TODO: Downgrade to 4.3 (or even 4.0 is Provider.map isn't needed) as per https://github.com/openrewrite/rewrite-gradle-plugin/issues/227#issuecomment-1707455588 unless future comments change this -val testGradle6Dot8Dot3 = tasks.register("testGradle6Dot8Dot3") { - systemProperty("org.openrewrite.test.gradleVersion", "6.8.3") +val testGradle4Dot10Dot0 = tasks.register("testGradle4Dot10Dot0") { + systemProperty("org.openrewrite.test.gradleVersion", "4.10.0") systemProperty("jarLocationForTest", tasks.named("jar").get().archiveFile.get().asFile.absolutePath) - // Gradle 6.8.3 predates support for Java 17 + // Gradle 4.10.0 predates support for Java 11 javaLauncher.set(javaToolchains.launcherFor { - languageVersion.set(JavaLanguageVersion.of(11)) + languageVersion.set(JavaLanguageVersion.of(8)) }) } tasks.named("check").configure { - dependsOn(testGradle6Dot8Dot3) + dependsOn(testGradle4Dot10Dot0) } configure { diff --git a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java index 2865a40af..f387b96b2 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java @@ -16,11 +16,13 @@ package org.openrewrite.gradle; import org.gradle.api.DefaultTask; +import org.gradle.api.file.ProjectLayout; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.options.Option; +import javax.inject.Inject; import java.io.File; import java.nio.file.Path; import java.util.Collections; @@ -34,6 +36,10 @@ public abstract class AbstractRewriteTask extends DefaultTask { protected GradleProjectParser gpp; protected RewriteExtension extension; + protected AbstractRewriteTask() { + notCompatibleWithConfigurationCache("rewrite needs to parse the whole project"); + } + public T setExtension(RewriteExtension extension) { this.extension = extension; //noinspection unchecked @@ -56,6 +62,9 @@ public boolean isDumpGcActivity() { return dumpGcActivity; } + @Inject + public abstract ProjectLayout getProjectLayout(); + @Internal protected T getProjectParser() { if (gpp == null) { diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java index 667a1ffcf..c2706d709 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java @@ -21,7 +21,7 @@ import javax.inject.Inject; -public class RewriteDiscoverTask extends AbstractRewriteTask { +public abstract class RewriteDiscoverTask extends AbstractRewriteTask { private boolean interactive; @Option(description = "Whether to enter an interactive shell to explore available recipes.", option = "interactive") diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java index 2592bc39d..5240f0835 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java @@ -15,7 +15,6 @@ */ package org.openrewrite.gradle; -import org.gradle.api.file.ProjectLayout; import org.gradle.api.logging.Logger; import org.gradle.api.logging.Logging; import org.gradle.api.specs.Specs; @@ -29,9 +28,6 @@ public abstract class RewriteDryRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteDryRunTask.class); - @Inject - public abstract ProjectLayout getProjectLayout(); - // This must return File, rather than Path. // On Gradle 4.0 annotating something returning a Path with @OutputFile triggers a bug that deadlocks Gradle @OutputFile diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index e5c650df1..ac9c349a8 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -114,7 +114,6 @@ private static void configureProject(Project project, RewriteExtension extension //Collect Java metadata for each project (used for Java Provenance) //Using the older javaConvention because we need to support older versions of gradle. - // TODO: Can we migrate away fron JavaPluginConvention? @SuppressWarnings("deprecation") JavaPluginConvention javaConvention = project.getConvention().getPlugin(JavaPluginConvention.class); javaConvention.getSourceSets().all(sourceSet -> { diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java index 6ee57fd63..81bfbb687 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java @@ -23,7 +23,7 @@ import javax.inject.Inject; -public class RewriteRunTask extends AbstractRewriteTask { +public abstract class RewriteRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteRunTask.class); diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt index b1a28b9fe..89f414f68 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt @@ -63,9 +63,7 @@ class RewriteDiscoverTest : RewritePluginTest { assertThat(result.output).contains("Configured with 2 active recipes and 1 active styles.") } - // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - @Disabled fun `rewriteDiscover is compatible with the configuration cache`( @TempDir projectDir: File ) { diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index c948c3427..d2f96b0f1 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -195,7 +195,6 @@ class RewriteDryRunTest : RewritePluginTest { assertThat(File(projectDir, "build/reports/rewrite/rewrite.patch").exists()).isTrue } - // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test fun `rewriteDryRun is compatible with the configuration cache`( @TempDir projectDir: File diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 55cdd28b3..8bd72ac0f 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -1243,9 +1243,7 @@ class RewriteRunTest : RewritePluginTest { ) } - // TODO: Extract out into RewritePluginTest? Does JUnit support that? @Test - @Disabled fun `rewriteRun is compatible with the configuration cache`( @TempDir projectDir: File ) { From 235486c1615c10f8ceb25a912b872d7e06ab09c5 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Sun, 21 Jan 2024 18:17:52 +0000 Subject: [PATCH 06/13] Delete redundant 'resolve dependencies' task --- .../ResolveRewriteDependenciesTask.java | 120 ------------------ .../gradle/RewriteResolveDependenciesTest.kt | 55 -------- 2 files changed, 175 deletions(-) delete mode 100755 plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java delete mode 100644 plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt diff --git a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java b/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java deleted file mode 100755 index 1425eb65b..000000000 --- a/plugin/src/main/java/org/openrewrite/gradle/ResolveRewriteDependenciesTask.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright ${year} the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle; - -import org.gradle.api.DefaultTask; -import org.gradle.api.Project; -import org.gradle.api.artifacts.Configuration; -import org.gradle.api.artifacts.Dependency; -import org.gradle.api.artifacts.dsl.DependencyHandler; -import org.gradle.api.attributes.Bundling; -import org.gradle.api.attributes.Category; -import org.gradle.api.attributes.LibraryElements; -import org.gradle.api.attributes.Usage; -import org.gradle.api.attributes.java.TargetJvmEnvironment; -import org.gradle.api.model.ObjectFactory; -import org.gradle.api.tasks.Internal; -import org.gradle.api.tasks.TaskAction; - -import java.io.File; -import java.util.Arrays; -import java.util.Set; -import java.util.stream.Stream; - -import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; -import static org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE; - -public class ResolveRewriteDependenciesTask extends DefaultTask { - private Set resolvedDependencies; - private Configuration configuration; - protected RewriteExtension extension; - - public ResolveRewriteDependenciesTask setConfiguration(Configuration configuration) { - this.configuration = configuration; - return this; - } - - public ResolveRewriteDependenciesTask setExtension(RewriteExtension extension) { - this.extension = extension; - return this; - } - - @Internal - public Set getResolvedDependencies() { - if (resolvedDependencies == null) { - String rewriteVersion = extension.getRewriteVersion(); - Project project = getProject(); - DependencyHandler deps = project.getDependencies(); - Dependency[] dependencies = new Dependency[]{ - deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-groovy:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-gradle:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-hcl:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), - deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-21:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-maven:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-properties:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-protobuf:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-xml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-yaml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-polyglot:" + extension.getRewritePolyglotVersion()), - deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), - - // This is an optional dependency of rewrite-java needed when projects also apply the checkstyle plugin - deps.create("com.puppycrawl.tools:checkstyle:" + extension.getCheckstyleToolsVersion()), - deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), - deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) - }; - if (configuration != null) { - dependencies = Stream.concat( - Arrays.stream(dependencies), - configuration.getDependencies().stream() - ).toArray(Dependency[]::new); - } - // By using a detached configuration, we separate this dependency resolution from the rest of the project's - // configuration. This also means that Gradle has no criteria with which to select between variants of - // dependencies which expose differing capabilities. So those must be manually configured - Configuration detachedConf = project.getConfigurations().detachedConfiguration(dependencies); - - try { - ObjectFactory objectFactory = project.getObjects(); - detachedConf.attributes(attributes -> { - // Taken from org.gradle.api.plugins.jvm.internal.DefaultJvmEcosystemAttributesDetails - attributes.attribute(Category.CATEGORY_ATTRIBUTE, objectFactory.named(Category.class, Category.LIBRARY)); - attributes.attribute(Usage.USAGE_ATTRIBUTE, objectFactory.named(Usage.class, Usage.JAVA_RUNTIME)); - attributes.attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objectFactory.named(LibraryElements.class, LibraryElements.JAR)); - attributes.attribute(BUNDLING_ATTRIBUTE, objectFactory.named(Bundling.class, Bundling.EXTERNAL)); - attributes.attribute(TARGET_JVM_ENVIRONMENT_ATTRIBUTE, objectFactory.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); - }); - } catch (NoClassDefFoundError e) { - // Old versions of gradle don't have all of these attributes and that's OK - } - - resolvedDependencies = detachedConf.resolve(); - } - return resolvedDependencies; - } - - @TaskAction - void run() { - getResolvedDependencies(); - } -} diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt deleted file mode 100644 index 009734cd1..000000000 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteResolveDependenciesTest.kt +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright ${year} the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.gradle - -import org.assertj.core.api.Assertions.assertThat -import org.gradle.testkit.runner.TaskOutcome -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.io.TempDir -import java.io.File - -class RewriteResolveDependenciesTest : RewritePluginTest { - @Test - fun `Specifying a rewriteVersion does not cause build failures`( - @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 { - rewriteVersion = "8.8.0" - } - """) - } - - val result = runGradle(projectDir, "rewriteResolveDependencies") - val rewriteDryRunResult = result.task(":rewriteResolveDependencies")!! - assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) - } - -} From c7c44b079d1a0f58f7506fd9c170a6abb9d36bb4 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Sun, 21 Jan 2024 22:55:02 +0000 Subject: [PATCH 07/13] Use notCompatibleWithConfigurationCache on Gradle 7.4+ --- plugin/build.gradle.kts | 8 +-- .../gradle/AbstractRewriteTask.java | 9 ++- .../gradle/RewriteDiscoverTask.java | 2 +- .../openrewrite/gradle/RewriteDryRunTask.java | 2 +- .../org/openrewrite/gradle/RewritePlugin.java | 66 +++++++++---------- .../openrewrite/gradle/RewriteRunTask.java | 2 +- .../openrewrite/gradle/RewriteDiscoverTest.kt | 9 ++- .../openrewrite/gradle/RewriteDryRunTest.kt | 5 ++ .../openrewrite/gradle/RewritePluginTest.kt | 5 ++ .../org/openrewrite/gradle/RewriteRunTest.kt | 4 ++ 10 files changed, 67 insertions(+), 45 deletions(-) diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index b348e5b79..17f9d93bf 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -194,16 +194,16 @@ tasks.named("test") { ) } -val testGradle4Dot10Dot0 = tasks.register("testGradle4Dot10Dot0") { - systemProperty("org.openrewrite.test.gradleVersion", "4.10.0") +val testGradle4Dot10 = tasks.register("testGradle4Dot10") { + systemProperty("org.openrewrite.test.gradleVersion", "4.10") systemProperty("jarLocationForTest", tasks.named("jar").get().archiveFile.get().asFile.absolutePath) - // Gradle 4.10.0 predates support for Java 11 + // Gradle 4.10 predates support for Java 11 javaLauncher.set(javaToolchains.launcherFor { languageVersion.set(JavaLanguageVersion.of(8)) }) } tasks.named("check").configure { - dependsOn(testGradle4Dot10Dot0) + dependsOn(testGradle4Dot10) } configure { diff --git a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java index f387b96b2..84bf9e111 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/AbstractRewriteTask.java @@ -21,6 +21,7 @@ import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.options.Option; +import org.gradle.util.GradleVersion; import javax.inject.Inject; import java.io.File; @@ -37,7 +38,9 @@ public abstract class AbstractRewriteTask extends DefaultTask { protected RewriteExtension extension; protected AbstractRewriteTask() { - notCompatibleWithConfigurationCache("rewrite needs to parse the whole project"); + if (GradleVersion.current().compareTo(GradleVersion.version("7.4")) >= 0) { + notCompatibleWithConfigurationCache("org.openrewrite.rewrite needs to parse the whole project"); + } } public T setExtension(RewriteExtension extension) { @@ -63,7 +66,9 @@ public boolean isDumpGcActivity() { } @Inject - public abstract ProjectLayout getProjectLayout(); + public ProjectLayout getProjectLayout() { + throw new AssertionError("unexpected; getProjectLayout() should be overridden by Gradle"); + } @Internal protected T getProjectParser() { diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java index c2706d709..667a1ffcf 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java @@ -21,7 +21,7 @@ import javax.inject.Inject; -public abstract class RewriteDiscoverTask extends AbstractRewriteTask { +public class RewriteDiscoverTask extends AbstractRewriteTask { private boolean interactive; @Option(description = "Whether to enter an interactive shell to explore available recipes.", option = "interactive") diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java index 5240f0835..a0ade4ae1 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java @@ -24,7 +24,7 @@ import javax.inject.Inject; import java.io.File; -public abstract class RewriteDryRunTask extends AbstractRewriteTask { +public class RewriteDryRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteDryRunTask.class); diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index ac9c349a8..4a535b8a4 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -35,18 +35,14 @@ import org.gradle.api.provider.Provider; import java.io.File; -import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; import java.util.Set; import java.util.stream.Stream; import static org.gradle.api.attributes.Bundling.BUNDLING_ATTRIBUTE; import static org.gradle.api.attributes.java.TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE; -import java.io.File; -import java.util.Comparator; -import java.util.HashSet; -import java.util.Set; - /** * When applied to the root project of a multi-project build, applies to all subprojects. * When applied to the root project the "rewrite" configuration and "rewrite" DSL created in the root project apply to @@ -148,34 +144,8 @@ private static void configureProject(Project project, RewriteExtension extension private Set getResolvedDependencies(Project project, RewriteExtension extension, Configuration rewriteConf) { if (resolvedDependencies == null) { - String rewriteVersion = extension.getRewriteVersion(); - DependencyHandler deps = project.getDependencies(); - Dependency[] dependencies = new Dependency[]{ - deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-groovy:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-gradle:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-hcl:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), - deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-maven:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-properties:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-protobuf:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-xml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-yaml:" + rewriteVersion), - deps.create("org.openrewrite:rewrite-polyglot:" + extension.getRewritePolyglotVersion()), - deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), - - // This is an optional dependency of rewrite-java needed when projects also apply the checkstyle plugin - deps.create("com.puppycrawl.tools:checkstyle:" + extension.getCheckstyleToolsVersion()), - deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), - deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) - }; - dependencies = Stream.concat( - Arrays.stream(dependencies), + Dependency[] dependencies = Stream.concat( + knownRewriteDependencies(extension, project.getDependencies()), rewriteConf.getDependencies().stream() ).toArray(Dependency[]::new); // By using a detached configuration, we separate this dependency resolution from the rest of the project's @@ -207,4 +177,32 @@ private Set getResolvedDependencies(Project project, RewriteExtension exte } return resolvedDependencies; } + + private static Stream knownRewriteDependencies(RewriteExtension extension, DependencyHandler deps) { + String rewriteVersion = extension.getRewriteVersion(); + return Stream.of( + deps.create("org.openrewrite:rewrite-core:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-groovy:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-gradle:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-hcl:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), + deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-maven:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-properties:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-protobuf:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-xml:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-yaml:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-polyglot:" + extension.getRewritePolyglotVersion()), + deps.create("org.openrewrite.gradle.tooling:model:" + extension.getRewriteGradleModelVersion()), + + // This is an optional dependency of rewrite-java needed when projects also apply the checkstyle plugin + deps.create("com.puppycrawl.tools:checkstyle:" + extension.getCheckstyleToolsVersion()), + deps.create("com.fasterxml.jackson.module:jackson-module-kotlin:" + extension.getJacksonModuleKotlinVersion()), + deps.create("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:" + extension.getJacksonModuleKotlinVersion()) + ); + } } diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java index 81bfbb687..6ee57fd63 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteRunTask.java @@ -23,7 +23,7 @@ import javax.inject.Inject; -public abstract class RewriteRunTask extends AbstractRewriteTask { +public class RewriteRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteRunTask.class); diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt index 89f414f68..fa44cf9c9 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt @@ -17,14 +17,15 @@ 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.io.TempDir +import org.openrewrite.Issue import java.io.File class RewriteDiscoverTest : RewritePluginTest { - // "https://github.com/openrewrite/rewrite-gradle-plugin/issues/33" + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/33") @Test fun `rewriteDiscover prints recipes from external dependencies`( @TempDir projectDir: File @@ -63,6 +64,10 @@ class RewriteDiscoverTest : RewritePluginTest { assertThat(result.output).contains("Configured with 2 active recipes and 1 active styles.") } + // The configuration cache works on Gradle 6.6+, but rewrite-gradle-plugin uses notCompatibleWithConfigurationCache, + // which is only available on Gradle 7.4+. + @DisabledIf("lessThanGradle7_4") + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/227") @Test fun `rewriteDiscover is compatible with the configuration cache`( @TempDir projectDir: File diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index d2f96b0f1..957d80016 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.Test import org.junit.jupiter.api.condition.DisabledIf import org.junit.jupiter.api.io.TempDir +import org.openrewrite.Issue import java.io.File class RewriteDryRunTest : RewritePluginTest { @@ -195,6 +196,10 @@ class RewriteDryRunTest : RewritePluginTest { assertThat(File(projectDir, "build/reports/rewrite/rewrite.patch").exists()).isTrue } + // The configuration cache works on Gradle 6.6+, but rewrite-gradle-plugin uses notCompatibleWithConfigurationCache, + // which is only available on Gradle 7.4+. + @DisabledIf("lessThanGradle7_4") + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/227") @Test fun `rewriteDryRun is compatible with the configuration cache`( @TempDir projectDir: File diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt index 79fbfb88e..42c52d528 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt @@ -48,4 +48,9 @@ interface RewritePluginTest { val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) return currentVersion < GradleVersion.version("6.8") } + + fun lessThanGradle7_4(): Boolean { + val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) + return currentVersion < GradleVersion.version("7.4") + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 8bd72ac0f..20cfc0417 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -1243,6 +1243,10 @@ class RewriteRunTest : RewritePluginTest { ) } + // The configuration cache works on Gradle 6.6+, but rewrite-gradle-plugin uses notCompatibleWithConfigurationCache, + // which is only available on Gradle 7.4+. + @DisabledIf("lessThanGradle7_4") + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/227") @Test fun `rewriteRun is compatible with the configuration cache`( @TempDir projectDir: File From ba42ebfa52caa0422578df853e447e889f06a17b Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Sat, 3 Feb 2024 00:07:49 +0000 Subject: [PATCH 08/13] Remove invalid comment --- .../org/openrewrite/gradle/RewriteDryRunTask.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java index a0ade4ae1..f7e07aba5 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDryRunTask.java @@ -22,16 +22,14 @@ import org.gradle.api.tasks.TaskAction; import javax.inject.Inject; -import java.io.File; +import java.nio.file.Path; public class RewriteDryRunTask extends AbstractRewriteTask { private static final Logger logger = Logging.getLogger(RewriteDryRunTask.class); - // This must return File, rather than Path. - // On Gradle 4.0 annotating something returning a Path with @OutputFile triggers a bug that deadlocks Gradle @OutputFile - public File getReportPath() { + public Path getReportPath() { return getProjectLayout() .getBuildDirectory() .get() @@ -39,8 +37,7 @@ public File getReportPath() { .toPath() .resolve("reports") .resolve("rewrite") - .resolve("rewrite.patch") - .toFile(); + .resolve("rewrite.patch"); } @Inject @@ -52,6 +49,6 @@ public RewriteDryRunTask() { @TaskAction public void run() { - getProjectParser().dryRun(getReportPath().toPath(), dumpGcActivity, throwable -> logger.info("Error during rewrite dry run", throwable)); + getProjectParser().dryRun(getReportPath(), dumpGcActivity, throwable -> logger.info("Error during rewrite dry run", throwable)); } } From c575ef43feff779e259a4235f8a90c408522ce7b Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Sat, 3 Feb 2024 13:33:37 +0000 Subject: [PATCH 09/13] Cleanup --- .../src/main/java/org/openrewrite/gradle/RewriteExtension.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java index f8597c375..e29e62c5a 100755 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteExtension.java @@ -1,5 +1,5 @@ /* - * Copyright ${year} the original author or authors. + * Copyright 2023 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From dcb1306fe17d840a77c47d1aea375f32fe56146d Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Sat, 3 Feb 2024 14:26:05 +0000 Subject: [PATCH 10/13] Extract common config-cache test into base test class --- .../openrewrite/gradle/RewriteDiscoverTest.kt | 34 +------- .../openrewrite/gradle/RewriteDryRunTest.kt | 20 ++--- .../openrewrite/gradle/RewritePluginTest.kt | 36 +++++++++ .../org/openrewrite/gradle/RewriteRunTest.kt | 79 ++++++++++--------- 4 files changed, 91 insertions(+), 78 deletions(-) diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt index fa44cf9c9..fb13318c6 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDiscoverTest.kt @@ -25,6 +25,8 @@ import java.io.File class RewriteDiscoverTest : RewritePluginTest { + override fun taskName(): String = "rewriteDiscover" + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/33") @Test fun `rewriteDiscover prints recipes from external dependencies`( @@ -57,38 +59,10 @@ class RewriteDiscoverTest : RewritePluginTest { """) } - val result = runGradle(projectDir, "rewriteDiscover") - val rewriteDiscoverResult = result.task(":rewriteDiscover")!! + val result = runGradle(projectDir, taskName()) + val rewriteDiscoverResult = result.task(":${taskName()}")!! assertThat(rewriteDiscoverResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(result.output).contains("Configured with 2 active recipes and 1 active styles.") } - - // The configuration cache works on Gradle 6.6+, but rewrite-gradle-plugin uses notCompatibleWithConfigurationCache, - // which is only available on Gradle 7.4+. - @DisabledIf("lessThanGradle7_4") - @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/227") - @Test - fun `rewriteDiscover is compatible with the configuration cache`( - @TempDir projectDir: File - ) { - gradleProject(projectDir) { - buildGradle(""" - plugins { - id("org.openrewrite.rewrite") - } - - repositories { - mavenLocal() - mavenCentral() - maven { - url = uri("https://oss.sonatype.org/content/repositories/snapshots") - } - } - """) - } - val result = runGradle(projectDir, "rewriteDiscover", "--configuration-cache") - val rewriteDryRunResult = result.task(":rewriteDiscover")!! - assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) - } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt index 957d80016..1aa0b2ac4 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteDryRunTest.kt @@ -17,7 +17,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.io.TempDir @@ -25,6 +24,9 @@ import org.openrewrite.Issue import java.io.File class RewriteDryRunTest : RewritePluginTest { + + override fun taskName(): String = "rewriteDryRun" + @Test fun `rewriteDryRun runs successfully without modifying source files`( @TempDir projectDir: File @@ -71,8 +73,8 @@ class RewriteDryRunTest : RewritePluginTest { java(helloWorld) } } - val result = runGradle(projectDir, "rewriteDryRun") - val rewriteDryRunResult = result.task(":rewriteDryRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteDryRunResult = result.task(":${taskName()}")!! assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(File(projectDir, "src/main/java/org/openrewrite/before/HelloWorld.java") @@ -120,8 +122,8 @@ class RewriteDryRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteDryRun", "-DactiveRecipe=org.openrewrite.java.OrderImports") - val rewriteDryRunResult = result.task(":rewriteDryRun")!! + val result = runGradle(projectDir, taskName(), "-DactiveRecipe=org.openrewrite.java.OrderImports") + val rewriteDryRunResult = result.task(":${taskName()}")!! assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(File(projectDir, "build/reports/rewrite/rewrite.patch").exists()).isTrue } @@ -189,8 +191,8 @@ class RewriteDryRunTest : RewritePluginTest { """) } } - val result = runGradle(projectDir, "rewriteDryRun", "-DactiveRecipe=org.openrewrite.kotlin.FindKotlinSources") - val rewriteDryRunResult = result.task(":rewriteDryRun")!! + val result = runGradle(projectDir, taskName(), "-DactiveRecipe=org.openrewrite.kotlin.FindKotlinSources") + val rewriteDryRunResult = result.task(":${taskName()}")!! assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(File(projectDir, "build/reports/rewrite/rewrite.patch").exists()).isTrue @@ -219,8 +221,8 @@ class RewriteDryRunTest : RewritePluginTest { } """) } - val result = runGradle(projectDir, "rewriteDryRun", "--configuration-cache") - val rewriteDryRunResult = result.task(":rewriteDryRun")!! + val result = runGradle(projectDir, taskName(), "--configuration-cache") + val rewriteDryRunResult = result.task(":${taskName()}")!! assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt index 42c52d528..b5c5ee9c4 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewritePluginTest.kt @@ -15,9 +15,15 @@ */ package org.openrewrite.gradle +import org.assertj.core.api.Assertions.assertThat import org.gradle.testkit.runner.BuildResult import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.TaskOutcome import org.gradle.util.GradleVersion +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.DisabledIf +import org.junit.jupiter.api.io.TempDir +import org.openrewrite.Issue import java.io.File import java.lang.management.ManagementFactory @@ -25,6 +31,8 @@ val gradleVersion: String? = System.getProperty("org.openrewrite.test.gradleVers interface RewritePluginTest { + fun taskName(): String + fun runGradle(testDir: File, vararg args: String): BuildResult = GradleRunner.create() .withDebug(ManagementFactory.getRuntimeMXBean().inputArguments.toString().indexOf("-agentlib:jdwp") > 0) @@ -53,4 +61,32 @@ interface RewritePluginTest { val currentVersion = if (gradleVersion == null) GradleVersion.current() else GradleVersion.version(gradleVersion) return currentVersion < GradleVersion.version("7.4") } + + // The configuration cache works on Gradle 6.6+, but rewrite-gradle-plugin uses notCompatibleWithConfigurationCache, + // which is only available on Gradle 7.4+. + @DisabledIf("lessThanGradle7_4") + @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/227") + @Test + fun `task is compatible with the configuration cache`( + @TempDir projectDir: File + ) { + gradleProject(projectDir) { + buildGradle(""" + plugins { + id("org.openrewrite.rewrite") + } + + repositories { + mavenLocal() + mavenCentral() + maven { + url = uri("https://oss.sonatype.org/content/repositories/snapshots") + } + } + """) + } + val result = runGradle(projectDir, taskName(), "--configuration-cache") + val taskResult = result.task(":${taskName()}")!! + assertThat(taskResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + } } diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index 20cfc0417..a18c4e2d2 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 @@ -32,6 +31,8 @@ import java.io.File class RewriteRunTest : RewritePluginTest { + override fun taskName(): String = "rewriteRun" + @Test fun `rewrite is isolated from conflicting versions of jackson on the classpath`( @TempDir projectDir: File @@ -80,8 +81,8 @@ class RewriteRunTest : RewritePluginTest { } } - val buildResult = runGradle(projectDir, "rewriteRun") - val taskResult = buildResult.task(":rewriteRun")!! + val buildResult = runGradle(projectDir, taskName()) + val taskResult = buildResult.task(":${taskName()}")!! assertThat(taskResult.outcome).isEqualTo(TaskOutcome.SUCCESS) } @@ -130,8 +131,8 @@ class RewriteRunTest : RewritePluginTest { } } assertThat(File(projectDir, "build.gradle").exists()).isTrue - val buildResult = runGradle(projectDir, "rewriteRun") - val taskResult = buildResult.task(":rewriteRun")!! + val buildResult = runGradle(projectDir, taskName()) + val taskResult = buildResult.task(":${taskName()}")!! assertThat(taskResult.outcome).isEqualTo(TaskOutcome.SUCCESS) @@ -235,8 +236,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) //language=java val aTestClassExpected = """ @@ -312,8 +313,8 @@ class RewriteRunTest : RewritePluginTest { } commitFilesToGitRepo(projectDir) - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val propertiesFile = File(projectDir, "a/src/main/resources/test.properties") @@ -374,8 +375,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val aFile = File(projectDir, "src/main/java/com/foo/A.java") @@ -432,8 +433,8 @@ class RewriteRunTest : RewritePluginTest { propertiesFile("in-sourceset.properties", "sam=true\n") } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val propertiesTextExpected = "samuel=true\n" @@ -475,8 +476,8 @@ class RewriteRunTest : RewritePluginTest { """) } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val gradlew = File(projectDir, "gradlew") @@ -542,8 +543,8 @@ class RewriteRunTest : RewritePluginTest { """) } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(projectDir.resolve("build.gradle").readText()) @@ -621,8 +622,8 @@ class RewriteRunTest : RewritePluginTest { """) } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) assertThat(projectDir.resolve("build.gradle").readText()) @@ -688,8 +689,8 @@ class RewriteRunTest : RewritePluginTest { """) } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val aFile = projectDir.resolve("src/main/java/com/foo/A.java") @@ -757,8 +758,8 @@ class RewriteRunTest : RewritePluginTest { """) } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val aFile = projectDir.resolve("src/main/java/com/foo/A.java") @@ -844,8 +845,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val bFile = projectDir.resolve("src/main/groovy/com/foo/B.groovy") @@ -905,8 +906,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val aFile = projectDir.resolve("src/main/kotlin/com/foo/A.kt") @@ -947,8 +948,8 @@ class RewriteRunTest : RewritePluginTest { """) } - val result = runGradle(projectDir, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val fooDir = projectDir.resolve("foo") @@ -1018,8 +1019,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(buildRoot, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(buildRoot, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val javaFile = buildRoot.resolve("src/main/java/org/openrewrite/before/HelloWorld.java") assertThat(javaFile.readText()) @@ -1098,7 +1099,7 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(projectDir, "rewriteRun") + val result = runGradle(projectDir, taskName()) val rewriteRunResult = result.task(":product:rewriteRun")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) @@ -1169,8 +1170,8 @@ class RewriteRunTest : RewritePluginTest { } } - val result = runGradle(buildRoot, "rewriteRun") - val rewriteRunResult = result.task(":rewriteRun")!! + val result = runGradle(buildRoot, taskName()) + val rewriteRunResult = result.task(":${taskName()}")!! assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) val javaFile = buildRoot.resolve("src/test/java/com/foo/ATest.java") assertThat(javaFile.readText()) @@ -1228,8 +1229,8 @@ class RewriteRunTest : RewritePluginTest { propertyKey: distributionUrl """.trimIndent()) } - val result = runGradle(projectDir, "rewriteRun") - val task = result.task(":rewriteRun")!! + val result = runGradle(projectDir, taskName()) + val task = result.task(":${taskName()}")!! assertThat(task.outcome).isEqualTo(TaskOutcome.SUCCESS) val propertiesFile = projectDir.resolve("gradle/wrapper/gradle-wrapper.properties") assertThat(propertiesFile.readText()) @@ -1265,8 +1266,8 @@ class RewriteRunTest : RewritePluginTest { } """) } - val result = runGradle(projectDir, "rewriteRun", "--configuration-cache") - val rewriteDryRunResult = result.task(":rewriteRun")!! - assertThat(rewriteDryRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) + val result = runGradle(projectDir, taskName(), "--configuration-cache") + val rewriteRunResult = result.task(":${taskName()}")!! + assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) } } From 0fe96a7884bde332b01dd1736b4b733770cfcc12 Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Fri, 7 Jun 2024 11:05:33 +0100 Subject: [PATCH 11/13] Cleanup after merge conflict resolution --- plugin/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/build.gradle.kts b/plugin/build.gradle.kts index e1a117083..44a85bf8d 100644 --- a/plugin/build.gradle.kts +++ b/plugin/build.gradle.kts @@ -204,7 +204,7 @@ val testGradle4 = tasks.register("testGradle4") { }) } tasks.named("check").configure { - dependsOn(testGradle4Dot10) + dependsOn(testGradle4) } configure { From aa6ba1c17c77a97b2299a881cbc0fa14088d808b Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Fri, 7 Jun 2024 11:09:20 +0100 Subject: [PATCH 12/13] Cleanup after merge conflict resolution --- .../org/openrewrite/gradle/RewriteRunTest.kt | 48 ------------------- 1 file changed, 48 deletions(-) diff --git a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt index bb8d28554..ebe663c3c 100644 --- a/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt +++ b/plugin/src/test/kotlin/org/openrewrite/gradle/RewriteRunTest.kt @@ -997,54 +997,6 @@ class RewriteRunTest : RewritePluginTest { assertThat(aFile.readText().contains("/*~~>*/")).isTrue } - @Issue("https://github.com/openrewrite/rewrite-gradle-plugin/issues/128") - @Test - fun deleteEmptyDirectory(@TempDir projectDir: File) { - gradleProject(projectDir) { - rewriteYaml(""" - type: specs.openrewrite.org/v1beta/recipe - name: org.openrewrite.test.DeleteFoo - displayName: Delete foo/foo.properties - description: After deleting the file foo.properties, the newly empty foo directory should also be deleted - recipeList: - - org.openrewrite.DeleteSourceFiles: - filePattern: foo/foo.properties - """) - propertiesFile("foo/foo.properties", "foo = bar") - buildGradle(""" - plugins { - id("groovy") - id("org.openrewrite.rewrite") - } - - repositories { - mavenLocal() - mavenCentral() - maven { - url = uri("https://oss.sonatype.org/content/repositories/snapshots") - } - } - - rewrite { - activeRecipe("org.openrewrite.test.DeleteFoo") - } - """) - } - - val result = runGradle(projectDir, taskName()) - val rewriteRunResult = result.task(":${taskName()}")!! - assertThat(rewriteRunResult.outcome).isEqualTo(TaskOutcome.SUCCESS) - - val fooDir = projectDir.resolve("foo") - val fooProperties = fooDir.resolve("foo.properties") - assertThat(!fooProperties.exists()) - .`as`("Recipe should have deleted foo/foo.properties, but it still exists") - .isTrue() - assertThat(!fooDir.exists()) - .`as`("Plugin should have cleaned up empty directory when no files remained within it") - .isTrue() - } - @Test fun `build root and repository root do not need to be the same`(@TempDir repositoryRoot: File) { repositoryRoot.apply{ From c87161525021b64fda2f102553100043092bba79 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sat, 3 Aug 2024 12:23:58 +0200 Subject: [PATCH 13/13] Add `rewrite-java-21` to `knownRewriteDependencies` --- plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 0a25424ba..9b983a02a 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -197,6 +197,7 @@ private static Stream knownRewriteDependencies(RewriteExtension exte deps.create("org.openrewrite:rewrite-json:" + rewriteVersion), deps.create("org.openrewrite:rewrite-kotlin:" + extension.getRewriteKotlinVersion()), deps.create("org.openrewrite:rewrite-java:" + rewriteVersion), + deps.create("org.openrewrite:rewrite-java-21:" + rewriteVersion), deps.create("org.openrewrite:rewrite-java-17:" + rewriteVersion), deps.create("org.openrewrite:rewrite-java-11:" + rewriteVersion), deps.create("org.openrewrite:rewrite-java-8:" + rewriteVersion),