From 989f12a306f9f6719e3d32d75ab8d4a27f488465 Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Mon, 8 Jun 2020 12:59:47 -0600 Subject: [PATCH 1/2] Convert `SpotlessTaskModern` to use `InputChanges` API This minimal change updates the task to use the non-deprecated `InputChanges` API, making it compatible with an upcoming Gradle 7.0 release. --- .../gradle/spotless/SpotlessTaskModern.java | 58 ++++++++++++------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskModern.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskModern.java index 3c4028bff6..5f51ef2726 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskModern.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskModern.java @@ -24,9 +24,16 @@ import java.util.stream.Collectors; import org.gradle.api.GradleException; +import org.gradle.api.file.FileCollection; import org.gradle.api.tasks.CacheableTask; +import org.gradle.api.tasks.InputFiles; +import org.gradle.api.tasks.PathSensitive; +import org.gradle.api.tasks.PathSensitivity; import org.gradle.api.tasks.TaskAction; -import org.gradle.api.tasks.incremental.IncrementalTaskInputs; +import org.gradle.work.ChangeType; +import org.gradle.work.FileChange; +import org.gradle.work.Incremental; +import org.gradle.work.InputChanges; import com.diffplug.common.base.Errors; import com.diffplug.common.base.Preconditions; @@ -35,10 +42,17 @@ @CacheableTask public class SpotlessTaskModern extends SpotlessTask { - @TaskAction - public void performAction(IncrementalTaskInputs inputs) throws Exception { - // TODO: implement using the InputChanges api + @PathSensitive(PathSensitivity.RELATIVE) + @Incremental + @InputFiles + @Override + public FileCollection getTarget() { + return super.getTarget(); + } + + @TaskAction + public void performAction(InputChanges inputs) throws Exception { if (target == null) { throw new GradleException("You must specify 'Iterable target'"); } @@ -68,27 +82,27 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception { } try (Formatter formatter = buildFormatter()) { - inputs.outOfDate(inputDetails -> { - File input = inputDetails.getFile(); - try { - if (shouldInclude.test(input) && input.isFile()) { - processInputFile(formatter, input); + for (FileChange fileChange : inputs.getFileChanges(target)) { + File input = fileChange.getFile(); + if (fileChange.getChangeType() == ChangeType.REMOVED) { + try { + if (shouldInclude.test(input)) { + deletePreviousResult(input); + } + } catch (IOException e) { + throw Errors.asRuntime(e); } - } catch (IOException e) { - throw Errors.asRuntime(e); - } - }); - } - inputs.removed(removedDetails -> { - File input = removedDetails.getFile(); - try { - if (shouldInclude.test(input)) { - deletePreviousResult(input); + } else { + try { + if (shouldInclude.test(input) && input.isFile()) { + processInputFile(formatter, input); + } + } catch (IOException e) { + throw Errors.asRuntime(e); + } } - } catch (IOException e) { - throw Errors.asRuntime(e); } - }); + } } } From 536709bbd973c7102918b398037802eef6ef6d9a Mon Sep 17 00:00:00 2001 From: Daz DeBoer Date: Wed, 10 Jun 2020 11:40:34 -0600 Subject: [PATCH 2/2] Centralize the Gradle versions used in tests and adapt for JDK11 --- .../gradle/spotless/ConfigAvoidanceTest.java | 2 +- .../spotless/ErrorShouldRethrowJre8Test.java | 4 +- .../spotless/GradleIntegrationHarness.java | 53 ++++++++++++------- .../diffplug/gradle/spotless/IdeHookTest.java | 2 +- .../spotless/MultiProjectAfterEvaluate.java | 2 +- .../gradle/spotless/RatchetFromTest.java | 4 +- .../RegisterDependenciesTaskTest.java | 2 +- .../gradle/spotless/SpecificFilesTest.java | 2 +- 8 files changed, 44 insertions(+), 27 deletions(-) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigAvoidanceTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigAvoidanceTest.java index 37594b20f0..822abe8779 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigAvoidanceTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ConfigAvoidanceTest.java @@ -23,7 +23,7 @@ public class ConfigAvoidanceTest extends GradleIntegrationHarness { protected final GradleRunner gradleRunnerConfigAvoidance() throws IOException { - return gradleRunner().withGradleVersion(SpotlessPluginPreConfigAvoidance.CONFIG_AVOIDANCE_INTRODUCED.getVersion()); + return gradleRunner().withGradleVersion(GradleVersionSupport.CONFIG_AVOIDANCE.version); } @Test diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowJre8Test.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowJre8Test.java index 59e088bafb..128315b7bb 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowJre8Test.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/ErrorShouldRethrowJre8Test.java @@ -123,7 +123,7 @@ private void runWithSuccess(String... messages) throws Exception { if (JreVersion.thisVm() != JreVersion._8) { return; } - BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").build(); + BuildResult result = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version).withArguments("check").build(); assertResultAndMessages(result, TaskOutcome.SUCCESS, messages); } @@ -131,7 +131,7 @@ private void runWithFailure(String... messages) throws Exception { if (JreVersion.thisVm() != JreVersion._8) { return; } - BuildResult result = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE).withArguments("check").buildAndFail(); + BuildResult result = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version).withArguments("check").buildAndFail(); assertResultAndMessages(result, TaskOutcome.FAILED, messages); } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java index 2c11eba67f..ff1bfacc8f 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationHarness.java @@ -39,6 +39,39 @@ import com.diffplug.spotless.ResourceHarness; public class GradleIntegrationHarness extends ResourceHarness { + public enum GradleVersionSupport { + LEGACY("2.14"), KOTLIN("4.0"), CONFIG_AVOIDANCE("4.9"), MODERN(SpotlessPluginModern.MINIMUM_GRADLE), SETTINGS_PLUGINS("6.0"); + + final String version; + + GradleVersionSupport(String version) { + this.version = adaptGradleVersionForJdk(adaptGradleVersionForModern(version)); + } + } + + protected static String adaptGradleVersionForModern(String ver) { + if ("true".equals(System.getProperty(SpotlessPluginModern.SPOTLESS_MODERN))) { + return Double.parseDouble(ver) < Double.parseDouble(SpotlessPluginModern.MINIMUM_GRADLE) ? SpotlessPluginModern.MINIMUM_GRADLE : ver; + } + return ver; + } + + /** + * For Java 11+, Gradle 5 is the minimum. + * So if you ask for less than Gradle 5, you get it on Java 8, but on Java 11 you get promoted to Gradle 5. + * If you ask for more than Gradle 5, you'll definitely get it. + */ + protected static String adaptGradleVersionForJdk(String ver) { + JreVersion jre = JreVersion.thisVm(); + // @formatter:off + switch (jre) { + case _8: return ver; + case _11: return Double.parseDouble(ver) < 5.0 ? "5.0" : ver; + default: throw new IllegalStateException("Spotless build is only supported on Java 8 and Java 11"); + } + // @formatter:on + } + /** * Each test gets its own temp folder, and we create a gradle * build there and run it. @@ -58,30 +91,14 @@ public void gitAttributes() throws IOException { setFile(".gitattributes").toContent("* text eol=lf"); } - /** - * For Java 11+, Gradle 5 is the minimum. - * So if you ask for less than Gradle 5, you get it on Java 8, but on Java 11 you get promoted to Gradle 5. - * If you ask for more than Gradle 5, you'll definitely get it. - */ - protected static String requestGradleForJre8and11(String ver) { - JreVersion jre = JreVersion.thisVm(); - // @formatter:off - switch (jre) { - case _8: return ver; - case _11: return Double.parseDouble(ver) < 5.0 ? "5.0" : ver; - default: throw new IllegalStateException("Spotless build is only supported on Java 8 and Java 11"); - } - // @formatter:on - } - protected final GradleRunner gradleRunner() throws IOException { GradleRunner runner = GradleRunner.create() - .withGradleVersion(requestGradleForJre8and11("2.14")) + .withGradleVersion(GradleVersionSupport.LEGACY.version) .withProjectDir(rootFolder()) .withPluginClasspath(); if ("true".equals(System.getProperty(SpotlessPluginModern.SPOTLESS_MODERN))) { runner.withEnvironment(ImmutableMap.of("ORG_GRADLE_PROJECT_" + SpotlessPluginModern.SPOTLESS_MODERN, "true")); - runner.withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE); + runner.withGradleVersion(GradleVersionSupport.MODERN.version); } return runner; } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java index c8f6bee1b3..1c8e3df814 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/IdeHookTest.java @@ -70,7 +70,7 @@ private void runWith(String... arguments) throws IOException { // gradle 4.7 -> 5.1 don't work in tooling API because of https://github.com/gradle/gradle/issues/7617 // gradle 5.1 -> current confirmed to work gradleRunner() - .withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE) + .withGradleVersion(GradleVersionSupport.MODERN.version) .withProjectDir(rootFolder()) .withPluginClasspath() .withArguments(arguments) diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectAfterEvaluate.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectAfterEvaluate.java index 1f6cf7a703..47b8802e7d 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectAfterEvaluate.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/MultiProjectAfterEvaluate.java @@ -39,6 +39,6 @@ public void failureDoesntTriggerAll() throws IOException { } private final GradleRunner gradleRunner6() throws IOException { - return gradleRunner().withGradleVersion("6.0"); + return gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version); } } diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RatchetFromTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RatchetFromTest.java index 1910a2aec6..ac09f4e854 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RatchetFromTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RatchetFromTest.java @@ -116,11 +116,11 @@ private void assertDirty() throws Exception { } private BuildResultAssertion assertPass(String... tasks) throws Exception { - return new BuildResultAssertion(gradleRunner().withGradleVersion("6.0").withArguments(tasks).build()); + return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).build()); } private BuildResultAssertion assertFail(String... tasks) throws Exception { - return new BuildResultAssertion(gradleRunner().withGradleVersion("6.0").withArguments(tasks).buildAndFail()); + return new BuildResultAssertion(gradleRunner().withGradleVersion(GradleVersionSupport.SETTINGS_PLUGINS.version).withArguments(tasks).buildAndFail()); } private static final String BASELINE_ROOT = "ebb03d6940ee0254010e71917735efa203c27e16"; diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskTest.java index daf63085dc..d015a0d4a5 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/RegisterDependenciesTaskTest.java @@ -39,7 +39,7 @@ public void registerDependencies() throws IOException { "}"); setFile("gradle.properties").toLines(); - String newestSupported = gradleRunner().withGradleVersion(SpotlessPluginModern.MINIMUM_GRADLE) + String newestSupported = gradleRunner().withGradleVersion(GradleVersionSupport.MODERN.version) .withArguments("spotlessCheck").build().getOutput(); Assertions.assertThat(newestSupported.replace("\r", "")) .startsWith("> Task :spotlessCheck UP-TO-DATE\n" + diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java index 693dab350e..5e82543f65 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/SpecificFilesTest.java @@ -116,7 +116,7 @@ private void integration(String patterns, GradleRunner runner = gradleRunner() .withArguments("spotlessApply", "-PspotlessFiles=" + patterns); if (isKotlin) { - runner.withGradleVersion(requestGradleForJre8and11("4.0")); + runner.withGradleVersion(GradleVersionSupport.KOTLIN.version); } runner.build();