From e97a580229601ec031c551c46a23217245117888 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 29 Sep 2019 23:15:02 -0700 Subject: [PATCH] Move the `FILES_PROPERTY` support into the task creation point, so that we can avoid `getTasks().withType(SpotlessTask.class`. Allegedly `withType` triggers task configuration, although our test indicates that it doesn't at least in 4.9. Better to use the recommended API regardless, especially since we can do so without breaking 2.x support. --- .../gradle/spotless/SpotlessExtension.java | 14 ++++++++++++++ .../diffplug/gradle/spotless/SpotlessPlugin.java | 12 ------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 1eba446222..7d34a93caa 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -48,6 +48,8 @@ public class SpotlessExtension { private static final String CHECK_DESCRIPTION = "Checks that sourcecode satisfies formatting steps."; private static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place."; + private static final String FILES_PROPERTY = "spotlessFiles"; + public SpotlessExtension(Project project) { this.project = requireNonNull(project); rootCheckTask = project.task(EXTENSION + CHECK); @@ -268,6 +270,18 @@ public Object doCall(TaskExecutionGraph graph) { } }); + // set the filePatterns property + project.afterEvaluate(unused -> { + String filePatterns; + if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) { + filePatterns = (String) project.property(FILES_PROPERTY); + } else { + // needs to be non-null since it is an @Input property of the task + filePatterns = ""; + } + spotlessTask.setFilePatterns(filePatterns); + }); + // the root tasks depend on the control tasks rootCheckTask.dependsOn(checkTask); rootApplyTask.dependsOn(applyTask); diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java index 43d593284c..43874e1f35 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessPlugin.java @@ -26,8 +26,6 @@ public class SpotlessPlugin implements Plugin { SpotlessExtension spotlessExtension; - private static final String FILES_PROPERTY = "spotlessFiles"; - @Override public void apply(Project project) { // make sure there's a `clean` task @@ -41,16 +39,6 @@ public void apply(Project project) { clean.doLast(unused -> SpotlessCache.clear()); project.afterEvaluate(unused -> { - // set the filePatterns property - String filePatterns; - if (project.hasProperty(FILES_PROPERTY) && project.property(FILES_PROPERTY) instanceof String) { - filePatterns = (String) project.property(FILES_PROPERTY); - } else { - // needs to be non-null since it is an @Input property of the task - filePatterns = ""; - } - project.getTasks().withType(SpotlessTask.class, task -> task.setFilePatterns(filePatterns)); - // Add our check task as a dependency on the global check task // getTasks() returns a "live" collection, so this works even if the // task doesn't exist at the time this call is made