Skip to content

Commit

Permalink
Reduce API usage to levels compatible with Gradle 4.7
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Jan 6, 2021
1 parent 28dae14 commit 4afea67
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public RewriteDiscoverTask(SourceSet sourceSet, RewriteExtension extension) {
}

@TaskAction
public void execute() {
public void run() {
Environment env = environment();
Set<String> activeRecipes = getActiveRecipes();
Map<String, Recipe> recipesByName = env.getRecipesByName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected Logger getLog() {
}

@TaskAction
public void execute() {
public void run() {
ChangesContainer changes = listChanges();

if (changes.isNotEmpty()) {
Expand Down
63 changes: 42 additions & 21 deletions plugin/src/main/java/org/openrewrite/gradle/RewritePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +31,15 @@
*/
public class RewritePlugin implements Plugin<Project> {

/*
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);
Expand All @@ -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<Task> 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<Task> 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<RewriteFixTask> 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<RewriteWarnTask> 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<Task> taskClosure(Action<Task> configFun) {
return new Closure<Task>(this) {
public void doCall(Task arg) {
configFun.execute(arg);
}
};
}

// void configureMetrics(RewriteTask task) {
// Project project = task.getProject();
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected Logger getLog() {
}

@TaskAction
public void execute() {
public void run() {
ChangesContainer changes = listChanges();

if (changes.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = """\
Expand Down Expand Up @@ -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")
Expand All @@ -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"() {
Expand All @@ -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"() {
Expand Down Expand Up @@ -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 = """\
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ class RewriteTestBase extends Specification {
.withPluginClasspath()
.forwardOutput()
.tap {
gradleVersion == GradleVersion.current().toString() ? null : it.withGradleVersion(gradleVersion)
if(gradleVersion != null) {
withGradleVersion(gradleVersion)
}
}
}

Expand All @@ -61,7 +63,7 @@ class RewriteTestBase extends Specification {
if (explicitGradleVersions) {
return Arrays.asList(explicitGradleVersions.split("\\|"))
} else {
[GradleVersion.current().toString()]
[GradleVersion.current().version]
}
}
}

0 comments on commit 4afea67

Please sign in to comment.