diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java index 0f20aeb2c..f62abef5e 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteDiscoverTask.java @@ -41,7 +41,7 @@ public RewriteDiscoverTask(SourceSet sourceSet, RewriteExtension extension) { } @TaskAction - public void execute() { + public void run() { Environment env = environment(); Set activeRecipes = getActiveRecipes(); Map recipesByName = env.getRecipesByName(); diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java index a98a68ae2..64ac1d8ae 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteFixTask.java @@ -43,7 +43,7 @@ protected Logger getLog() { } @TaskAction - public void execute() { + public void run() { ChangesContainer changes = listChanges(); if (changes.isNotEmpty()) { diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java index 47b499ccf..a8a32c142 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java @@ -15,13 +15,14 @@ */ package org.openrewrite.gradle; +import groovy.lang.Closure; +import org.gradle.api.Action; import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.plugins.JavaPluginConvention; import org.gradle.api.tasks.SourceSetContainer; import org.gradle.api.tasks.TaskContainer; -import org.gradle.api.tasks.TaskProvider; /** * Adds the RewriteExtension to the current project and registers tasks per-sourceSet that implement rewrite fixing and @@ -30,6 +31,15 @@ */ public class RewritePlugin implements Plugin { + /* + Note on compatibility: + Since we're in the software modernization and improvement business we want to support old versions of Gradle. + As written this project doesn't use any APIs not present as of Gradle 4.7. + That predates Gradle supporting Java 11, which came in Gradle 5.0. + So our automated tests wont currently _enforce_ this compatibility guarantee. + Until that changes, tread carefully and test manually if you introduce any new usage of any Gradle API. + */ + @Override public void apply(Project project) { RewriteExtension maybeExtension = project.getExtensions().findByType(RewriteExtension.class); @@ -44,39 +54,50 @@ public void apply(Project project) { SourceSetContainer sourceSets = javaPlugin.getSourceSets(); // Fix is meant to be invoked manually and so is not made a dependency of any existing task - TaskProvider rewriteFixAllProvider = tasks.register("rewriteFix"); - rewriteFixAllProvider.configure(it -> { - it.setGroup("rewrite"); - it.setDescription("Apply the active refactoring recipes to all sources"); - }); + Task rewriteFixAll = tasks.create("rewriteFix", + taskClosure(task -> { + task.setGroup("rewrite"); + task.setDescription("Apply the active refactoring recipes to all sources"); + }) + ); + // Warn hooks into the regular Java build by becoming a dependency of "check", the same way that checkstyle or unit tests do - TaskProvider rewriteWarnAllProvider = tasks.register("rewriteWarn"); - rewriteWarnAllProvider.configure(it -> { - it.setGroup("rewrite"); - it.setDescription("Dry run the active refactoring recipes to all sources. No changes will be made."); - }); - TaskProvider checkTaskProvider = tasks.named("check"); - checkTaskProvider.configure(checkTask -> checkTask.dependsOn(rewriteWarnAllProvider)); + Task rewriteWarnAll = tasks.create("rewriteWarn", taskClosure(task -> { + task.setGroup("rewrite"); + task.setDescription("Dry run the active refactoring recipes to all sources. No changes will be made."); + }) + ); - sourceSets.configureEach(sourceSet -> { + Task checkTask = tasks.getByName("check"); + checkTask.dependsOn(rewriteWarnAll); + sourceSets.all(sourceSet -> { String rewriteFixTaskName = "rewriteFix" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1); - TaskProvider rewriteFixProvider = tasks.register(rewriteFixTaskName, RewriteFixTask.class, sourceSet, extension); - rewriteFixAllProvider.configure(it -> it.dependsOn(rewriteFixProvider)); + + RewriteFixTask rewriteFix = tasks.create(rewriteFixTaskName, RewriteFixTask.class, sourceSet, extension); + rewriteFixAll.configure(taskClosure(it -> it.dependsOn(rewriteFix))); String rewriteDiscoverTaskName = "rewriteDiscover" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1); - tasks.register(rewriteDiscoverTaskName, RewriteDiscoverTask.class, sourceSet, extension); + tasks.create(rewriteDiscoverTaskName, RewriteDiscoverTask.class, sourceSet, extension); String compileTaskName = sourceSet.getCompileTaskName("java"); - TaskProvider compileTaskProvider = tasks.named(compileTaskName); - compileTaskProvider.configure(compileTask -> compileTask.mustRunAfter(rewriteFixProvider)); + Task compileTask = tasks.getByName(compileTaskName); + compileTask.configure(taskClosure(it -> it.mustRunAfter(rewriteFix))); String rewriteWarnTaskName = "rewriteWarn" + sourceSet.getName().substring(0, 1).toUpperCase() + sourceSet.getName().substring(1); - TaskProvider rewriteWarnProvider = tasks.register(rewriteWarnTaskName, RewriteWarnTask.class, sourceSet, extension); - rewriteWarnAllProvider.configure(it -> it.dependsOn(rewriteWarnProvider)); + RewriteWarnTask rewriteWarn = tasks.create(rewriteWarnTaskName, RewriteWarnTask.class, sourceSet, extension); + rewriteWarnAll.configure(taskClosure(it -> it.dependsOn(rewriteWarn))); }); } + private Closure taskClosure(Action configFun) { + return new Closure(this) { + public void doCall(Task arg) { + configFun.execute(arg); + } + }; + } + // void configureMetrics(RewriteTask task) { // Project project = task.getProject(); // diff --git a/plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java b/plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java index af82ae768..b7867c31b 100644 --- a/plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java +++ b/plugin/src/main/java/org/openrewrite/gradle/RewriteWarnTask.java @@ -39,7 +39,7 @@ protected Logger getLog() { } @TaskAction - public void execute() { + public void run() { ChangesContainer changes = listChanges(); if (changes.isNotEmpty()) { diff --git a/plugin/src/test/groovy/org/openrewrite/gradle/RewritePluginTest.groovy b/plugin/src/test/groovy/org/openrewrite/gradle/RewritePluginTest.groovy index 2e9c256c2..0405ae9a0 100644 --- a/plugin/src/test/groovy/org/openrewrite/gradle/RewritePluginTest.groovy +++ b/plugin/src/test/groovy/org/openrewrite/gradle/RewritePluginTest.groovy @@ -16,7 +16,7 @@ package org.openrewrite.gradle import org.gradle.testkit.runner.TaskOutcome -import spock.lang.Ignore +import spock.lang.Unroll /** * Because of how the Gradle Test Kit manages the classpath of the project under test, these may fail when run from an IDE. @@ -27,6 +27,7 @@ import spock.lang.Ignore * If breakpoints within your plugin aren't being hit try adding -Dorg.gradle.testkit.debug=true to the arguments and * connecting a remote debugger on port 5005. */ +@Unroll class RewritePluginTest extends RewriteTestBase { String rewriteYamlText = """\ @@ -90,7 +91,7 @@ class RewritePluginTest extends RewriteTestBase { File sourceFile = writeSource(HelloWorldJavaBeforeRefactor) when: - def result = gradleRunner("6.5.1", "build").build() + def result = gradleRunner(gradleVersion, "build").build() def rewriteWarnMainResult = result.task(":rewriteWarnMain") def rewriteWarnTestResult = result.task(":rewriteWarnTest") @@ -103,6 +104,9 @@ class RewritePluginTest extends RewriteTestBase { // With no test source in this project any of these are potentially reasonable results // Ultimately NO_SOURCE is probably the most appropriate, but in this early stage of development SUCCESS is acceptable rewriteWarnTestResult.outcome == TaskOutcome.SUCCESS || rewriteWarnTestResult.outcome == TaskOutcome.NO_SOURCE || rewriteWarnTestResult.outcome == TaskOutcome.SKIPPED + + where: + gradleVersion << GRADLE_VERSIONS_UNDER_TEST } def "rewriteFix will alter the source file according to the provided active recipe"() { @@ -116,12 +120,15 @@ class RewritePluginTest extends RewriteTestBase { File sourceFile = writeSource(HelloWorldJavaBeforeRefactor) when: - def result = gradleRunner("6.5.1", "rewriteFixMain").build() + def result = gradleRunner(gradleVersion, "rewriteFixMain").build() def rewriteFixMainResult = result.task(":rewriteFixMain") then: rewriteFixMainResult.outcome == TaskOutcome.SUCCESS sourceFile.text == HelloWorldJavaAfterRefactor + + where: + gradleVersion << GRADLE_VERSIONS_UNDER_TEST } def "rewriteFix works on multi-project builds"() { @@ -180,7 +187,7 @@ class RewritePluginTest extends RewriteTestBase { } """.stripIndent() when: - def result = gradleRunner("6.5.1", "rewriteFix").build() + def result = gradleRunner(gradleVersion, "rewriteFix").build() def aRewriteFixnResult = result.task(":a:rewriteFixTest") def bRewriteFixResult = result.task(":b:rewriteFixTest") String bTestClassExpected = """\ @@ -208,8 +215,12 @@ class RewritePluginTest extends RewriteTestBase { bRewriteFixResult.outcome == TaskOutcome.SUCCESS aTestClass.text == aTestClassExpected bTestClass.text == bTestClassExpected + + where: + gradleVersion << GRADLE_VERSIONS_UNDER_TEST } + @spock.lang.Ignore def "rewriteDiscover will print some stuff"() { given: projectDir.newFile("settings.gradle") @@ -221,10 +232,13 @@ class RewritePluginTest extends RewriteTestBase { File sourceFile = writeSource(HelloWorldJavaBeforeRefactor) when: - def result = gradleRunner("6.5.1", "rewriteDiscoverMain").build() + def result = gradleRunner(gradleVersion, "rewriteDiscoverMain").build() def rewriteDiscoverResult = result.task(":rewriteDiscoverMain") then: rewriteDiscoverResult.outcome == TaskOutcome.SUCCESS + + where: + gradleVersion << GRADLE_VERSIONS_UNDER_TEST } } diff --git a/plugin/src/test/groovy/org/openrewrite/gradle/RewriteTestBase.groovy b/plugin/src/test/groovy/org/openrewrite/gradle/RewriteTestBase.groovy index 706faf126..dfa7dd659 100644 --- a/plugin/src/test/groovy/org/openrewrite/gradle/RewriteTestBase.groovy +++ b/plugin/src/test/groovy/org/openrewrite/gradle/RewriteTestBase.groovy @@ -52,7 +52,9 @@ class RewriteTestBase extends Specification { .withPluginClasspath() .forwardOutput() .tap { - gradleVersion == GradleVersion.current().toString() ? null : it.withGradleVersion(gradleVersion) + if(gradleVersion != null) { + withGradleVersion(gradleVersion) + } } } @@ -61,7 +63,7 @@ class RewriteTestBase extends Specification { if (explicitGradleVersions) { return Arrays.asList(explicitGradleVersions.split("\\|")) } else { - [GradleVersion.current().toString()] + [GradleVersion.current().version] } } }