From 1792d9f84021dad2b8f53c4340f777058752ed9f Mon Sep 17 00:00:00 2001 From: carbotaniuman Date: Wed, 31 Jul 2019 22:18:01 -0500 Subject: [PATCH 1/3] Added Gradle 4.9 Configuration Avoidance support --- gradle/wrapper/gradle-wrapper.properties | 5 +- .../gradle/spotless/SpotlessPlugin.java | 77 +------------- .../gradle/spotless/SpotlessTask.java | 6 +- .../spotless/SpotlessTaskConstants.java | 38 +++++++ .../gradle/spotless/SpotlessTaskSetup.java | 31 ++++++ .../SpotlessTaskSetupConfigAvoidance.java | 99 +++++++++++++++++ .../spotless/SpotlessTaskSetupLegacy.java | 100 ++++++++++++++++++ .../gradle/spotless/PaddedCellTaskTest.java | 2 +- 8 files changed, 278 insertions(+), 80 deletions(-) create mode 100644 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskConstants.java create mode 100644 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetup.java create mode 100644 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java create mode 100755 plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupLegacy.java diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ea720f986f..43ba87d178 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,6 @@ +#Wed Jul 31 18:08:12 CDT 2019 +distributionUrl=https\://services.gradle.org/distributions/gradle-4.9-all.zip distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip +zipStoreBase=GRADLE_USER_HOME 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 31cede4d52..94c86269b3 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 @@ -15,6 +15,8 @@ */ package com.diffplug.gradle.spotless; +import static com.diffplug.gradle.spotless.SpotlessTaskConstants.*; + import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.Task; @@ -30,15 +32,6 @@ public class SpotlessPlugin implements Plugin { SpotlessExtension spotlessExtension; - static final String EXTENSION = "spotless"; - static final String CHECK = "Check"; - static final String APPLY = "Apply"; - - private static final String TASK_GROUP = "Verification"; - 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"; - @Override public void apply(Project project) { // make sure there's a `clean` task @@ -56,72 +49,8 @@ public SpotlessExtension getExtension() { return spotlessExtension; } - @SuppressWarnings("rawtypes") void createTasks(Project project) { - Task rootCheckTask = project.task(EXTENSION + CHECK); - rootCheckTask.setGroup(TASK_GROUP); - rootCheckTask.setDescription(CHECK_DESCRIPTION); - Task rootApplyTask = project.task(EXTENSION + APPLY); - rootApplyTask.setGroup(TASK_GROUP); - rootApplyTask.setDescription(APPLY_DESCRIPTION); - 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 = ""; - } - - spotlessExtension.formats.forEach((key, value) -> { - // create the task that does the work - String taskName = EXTENSION + capitalize(key); - SpotlessTask spotlessTask = project.getTasks().create(taskName, SpotlessTask.class); - value.setupTask(spotlessTask); - spotlessTask.setFilePatterns(filePatterns); - - // create the check and apply control tasks - Task checkTask = project.getTasks().create(taskName + CHECK); - Task applyTask = project.getTasks().create(taskName + APPLY); - // the root tasks depend on them - rootCheckTask.dependsOn(checkTask); - rootApplyTask.dependsOn(applyTask); - // and they depend on the work task - checkTask.dependsOn(spotlessTask); - applyTask.dependsOn(spotlessTask); - - // when the task graph is ready, we'll configure the spotlessTask appropriately - project.getGradle().getTaskGraph().whenReady(new Closure(null) { - private static final long serialVersionUID = 1L; - - // called by gradle - @SuppressFBWarnings("UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS") - public Object doCall(TaskExecutionGraph graph) { - if (graph.hasTask(checkTask)) { - spotlessTask.setCheck(); - } - if (graph.hasTask(applyTask)) { - spotlessTask.setApply(); - } - return Closure.DONE; - } - }); - }); - - // 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 - if (spotlessExtension.enforceCheck) { - project.getTasks() - .matching(task -> task.getName().equals(JavaBasePlugin.CHECK_TASK_NAME)) - .all(task -> task.dependsOn(rootCheckTask)); - } - - // clear spotless' cache when the user does a clean, but only after any spotless tasks - Task clean = project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME); - clean.doLast(unused -> SpotlessCache.clear()); - project.getTasks() - .withType(SpotlessTask.class) - .all(task -> task.mustRunAfter(clean)); + SpotlessTaskSetup.getForVersion().accept(project, spotlessExtension); } static String capitalize(String input) { diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java index 80faec9d24..416b449eb4 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java @@ -157,8 +157,8 @@ public void setApply() { /** Returns the name of this format. */ String formatName() { String name = getName(); - if (name.startsWith(SpotlessPlugin.EXTENSION)) { - return name.substring(SpotlessPlugin.EXTENSION.length()).toLowerCase(Locale.ROOT); + if (name.startsWith(SpotlessTaskConstants.EXTENSION)) { + return name.substring(SpotlessTaskConstants.EXTENSION.length()).toLowerCase(Locale.ROOT); } else { return name; } @@ -170,7 +170,7 @@ public void performAction(IncrementalTaskInputs inputs) throws Exception { throw new GradleException("You must specify 'Iterable toFormat'"); } if (!check && !apply) { - throw new GradleException("Don't call " + getName() + " directly, call " + getName() + SpotlessPlugin.CHECK + " or " + getName() + SpotlessPlugin.APPLY); + throw new GradleException("Don't call " + getName() + " directly, call " + getName() + SpotlessTaskConstants.CHECK + " or " + getName() + SpotlessTaskConstants.APPLY); } Predicate shouldInclude; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskConstants.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskConstants.java new file mode 100644 index 0000000000..a57a451e3f --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskConstants.java @@ -0,0 +1,38 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import java.util.Objects; + +final class SpotlessTaskConstants { + static final String EXTENSION = "spotless"; + static final String CHECK = "Check"; + static final String APPLY = "Apply"; + + static final String TASK_GROUP = "Verification"; + static final String CHECK_DESCRIPTION = "Checks that sourcecode satisfies formatting steps."; + static final String APPLY_DESCRIPTION = "Applies code formatting steps to sourcecode in-place."; + + static final String FILES_PROPERTY = "spotlessFiles"; + + static String capitalize(String input) { + Objects.requireNonNull(input, "input"); + return Character.toUpperCase(input.charAt(0)) + input.substring(1); + } + + // prevent instantiation + private SpotlessTaskConstants() {} +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetup.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetup.java new file mode 100644 index 0000000000..051410994b --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetup.java @@ -0,0 +1,31 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import java.util.function.BiConsumer; + +import org.gradle.api.Project; +import org.gradle.util.GradleVersion; + +interface SpotlessTaskSetup extends BiConsumer { + static SpotlessTaskSetup getForVersion() { + if(GradleVersion.current().compareTo(GradleVersion.version("4.9")) >= 0) { + return new SpotlessTaskSetupConfigAvoidance(); + } else { + return new SpotlessTaskSetupLegacy(); + } + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java new file mode 100644 index 0000000000..b3ec259ec1 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java @@ -0,0 +1,99 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.plugins.JavaBasePlugin; +import org.gradle.api.tasks.TaskProvider; + +import com.diffplug.spotless.SpotlessCache; + +import static com.diffplug.gradle.spotless.SpotlessTaskConstants.*; +import static com.diffplug.gradle.spotless.SpotlessTaskConstants.FILES_PROPERTY; + +final class SpotlessTaskSetupConfigAvoidance implements SpotlessTaskSetup { + @Override + public void accept(Project project, SpotlessExtension spotlessExtension) { + TaskProvider rootCheckTaskProvider = project.getTasks().register( + EXTENSION + CHECK, + task -> { + task.setGroup(TASK_GROUP); + task.setDescription(CHECK_DESCRIPTION); + }); + TaskProvider rootApplyTaskProvider = project.getTasks().register( + EXTENSION + APPLY, + task -> { + task.setGroup(TASK_GROUP); + task.setDescription(APPLY_DESCRIPTION); + }); + + 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 = ""; + } + + spotlessExtension.formats.forEach((key, value) -> { + // create the task that does the work + String taskName = EXTENSION + capitalize(key); + TaskProvider spotlessTaskProvider = project.getTasks().register(taskName, SpotlessTask.class, task -> { + value.setupTask(task); + task.setFilePatterns(filePatterns); + }); + + // create the check and apply control tasks + TaskProvider checkTaskProvider = project.getTasks().register(taskName + CHECK); + TaskProvider applyTaskProvider = project.getTasks().register(taskName + APPLY); + // the root tasks depend on them + rootCheckTaskProvider.configure(rootCheckTask -> rootCheckTask.dependsOn(checkTaskProvider)); + rootApplyTaskProvider.configure(rootApplyTask -> rootApplyTask.dependsOn(applyTaskProvider)); + // and they depend on the work task + checkTaskProvider.configure(checkTask -> checkTask.dependsOn(spotlessTaskProvider)); + applyTaskProvider.configure(applyTask -> applyTask.dependsOn(spotlessTaskProvider)); + + // when the task graph is ready, we'll configure the spotlessTask appropriately + project.getGradle().getTaskGraph().whenReady(graph -> { + if (checkTaskProvider.isPresent() && graph.hasTask(checkTaskProvider.get())) { + spotlessTaskProvider.configure(SpotlessTask::setCheck); + } + if (applyTaskProvider.isPresent() && graph.hasTask(applyTaskProvider.get())) { + spotlessTaskProvider.configure(SpotlessTask::setApply); + } + }); + }); + + // Add our check task as a dependency on the global check task. + // getTasks() returns a "live" collection and configureEach() is lazy, + // so this works even if the task doesn't exist at the time this call + // is made. + if (spotlessExtension.enforceCheck) { + project.getTasks() + .matching(task -> task.getName().equals(JavaBasePlugin.CHECK_TASK_NAME)) + .configureEach(task -> task.dependsOn(rootCheckTaskProvider)); + } + + // clear spotless' cache when the user does a clean, but only after any spotless tasks + TaskProvider cleanTaskProvider = project.getTasks().named(BasePlugin.CLEAN_TASK_NAME); + cleanTaskProvider.configure(task -> task.doLast(unused -> SpotlessCache.clear())); + project.getTasks() + .withType(SpotlessTask.class) + .configureEach(task -> task.mustRunAfter(cleanTaskProvider)); + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupLegacy.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupLegacy.java new file mode 100755 index 0000000000..25c74e99cd --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupLegacy.java @@ -0,0 +1,100 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import static com.diffplug.gradle.spotless.SpotlessTaskConstants.*; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import groovy.lang.Closure; +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.execution.TaskExecutionGraph; +import org.gradle.api.plugins.BasePlugin; +import org.gradle.api.plugins.JavaBasePlugin; + +import com.diffplug.spotless.SpotlessCache; + +final class SpotlessTaskSetupLegacy implements SpotlessTaskSetup { + @Override + @SuppressWarnings("rawtypes") + public void accept(Project project, SpotlessExtension spotlessExtension) { + Task rootCheckTask = project.task(EXTENSION + CHECK); + rootCheckTask.setGroup(SpotlessTaskConstants.TASK_GROUP); + rootCheckTask.setDescription(CHECK_DESCRIPTION); + Task rootApplyTask = project.task(EXTENSION + APPLY); + rootApplyTask.setGroup(TASK_GROUP); + rootApplyTask.setDescription(APPLY_DESCRIPTION); + 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 = ""; + } + + spotlessExtension.formats.forEach((key, value) -> { + // create the task that does the work + String taskName = EXTENSION + capitalize(key); + SpotlessTask spotlessTask = project.getTasks().create(taskName, SpotlessTask.class); + value.setupTask(spotlessTask); + spotlessTask.setFilePatterns(filePatterns); + + // create the check and apply control tasks + Task checkTask = project.getTasks().create(taskName + CHECK); + Task applyTask = project.getTasks().create(taskName + APPLY); + // the root tasks depend on them + rootCheckTask.dependsOn(checkTask); + rootApplyTask.dependsOn(applyTask); + // and they depend on the work task + checkTask.dependsOn(spotlessTask); + applyTask.dependsOn(spotlessTask); + + //this cannot be replaced with a lambda because the Action overload does not exist in 2.14 + // when the task graph is ready, we'll configure the spotlessTask appropriately + project.getGradle().getTaskGraph().whenReady(new Closure(null) { + private static final long serialVersionUID = 1L; + + // called by gradle + @SuppressFBWarnings("UMAC_UNCALLABLE_METHOD_OF_ANONYMOUS_CLASS") + public Object doCall(TaskExecutionGraph graph) { + if (graph.hasTask(checkTask)) { + spotlessTask.setCheck(); + } + if (graph.hasTask(applyTask)) { + spotlessTask.setApply(); + } + return Closure.DONE; + } + }); + }); + + // 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 + if (spotlessExtension.enforceCheck) { + project.getTasks() + .matching(task -> task.getName().equals(JavaBasePlugin.CHECK_TASK_NAME)) + .all(task -> task.dependsOn(rootCheckTask)); + } + + // clear spotless' cache when the user does a clean, but only after any spotless tasks + Task clean = project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME); + clean.doLast(unused -> SpotlessCache.clear()); + project.getTasks() + .withType(SpotlessTask.class) + .all(task -> task.mustRunAfter(clean)); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java index a3d97c3351..648d092f3a 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java @@ -66,7 +66,7 @@ private SpotlessTask createCheckTask(String name, FormatterStep step) { } private SpotlessTask createApplyTask(String name, FormatterStep step) { - SpotlessTask task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name) + SpotlessPlugin.APPLY, SpotlessTask.class); + SpotlessTask task = project.getTasks().create("spotless" + SpotlessPlugin.capitalize(name) + SpotlessTaskConstants.APPLY, SpotlessTask.class); task.setApply(); task.addStep(step); task.setLineEndingsPolicy(LineEnding.UNIX.createPolicy()); From 11e0ddd805b394b54cb3d3ad205844cd26a9d494 Mon Sep 17 00:00:00 2001 From: carbotaniuman Date: Thu, 1 Aug 2019 12:24:54 -0500 Subject: [PATCH 2/3] Add tests for Gradle 4.9 and 5.4, tests checking lazy task loading and testing integration --- .../spotless/Gradle4_9IntegrationTest.java | 52 +++++++++++++++++++ .../gradle/spotless/Gradle4_9LazyTest.java | 50 ++++++++++++++++++ .../spotless/Gradle5_4IntegrationTest.java | 49 +++++++++++++++++ .../gradle/spotless/Gradle5_4LazyTest.java | 50 ++++++++++++++++++ .../spotless/GradleIntegrationTest.java | 2 +- 5 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9IntegrationTest.java create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9LazyTest.java create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4IntegrationTest.java create mode 100644 plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4LazyTest.java diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9IntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9IntegrationTest.java new file mode 100644 index 0000000000..4fa222eade --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9IntegrationTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import java.io.IOException; + +import org.gradle.testkit.runner.GradleRunner; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.diffplug.spotless.category.NpmTest; + +public class Gradle4_9IntegrationTest extends GradleIntegrationTest { + @Test + public void integration() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "apply plugin: 'scala'", + "spotless {", + " scala {", + " scalafmt().configFile('scalafmt.conf')", + " }", + "}"); + setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); + setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + } + + protected GradleRunner gradleRunner() throws IOException { + return GradleRunner.create() + .withGradleVersion("4.9") + .withProjectDir(rootFolder()) + .withPluginClasspath(); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9LazyTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9LazyTest.java new file mode 100644 index 0000000000..6bcf23d437 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle4_9LazyTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import org.gradle.testkit.runner.GradleRunner; +import org.junit.Test; + +import java.io.IOException; + +public class Gradle4_9LazyTest extends GradleIntegrationTest { + @Test + public void noConfigOnHelp() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "apply plugin: 'scala'", + "spotless {", + " scala {", + " scalafmt().configFile('scalafmt.conf')", + " }", + "}", + "project.tasks.matching { it.getName().startsWith(\"" + SpotlessTaskConstants.EXTENSION + "\") }", + ".configureEach { task -> throw new AssertionError(\"Spotless task configured\") }" + + ); + gradleRunner().withArguments("help").build(); + } + + protected GradleRunner gradleRunner() throws IOException { + return GradleRunner.create() + .withGradleVersion("4.9") + .withProjectDir(rootFolder()) + .withPluginClasspath(); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4IntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4IntegrationTest.java new file mode 100644 index 0000000000..c276a5dec0 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4IntegrationTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import org.gradle.testkit.runner.GradleRunner; +import org.junit.Test; + +import java.io.IOException; + +public class Gradle5_4IntegrationTest extends GradleIntegrationTest { + @Test + public void integration() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "apply plugin: 'scala'", + "spotless {", + " scala {", + " scalafmt().configFile('scalafmt.conf')", + " }", + "}"); + setFile("scalafmt.conf").toResource("scala/scalafmt/scalafmt.conf"); + setFile("src/main/scala/basic.scala").toResource("scala/scalafmt/basic.dirty"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/scala/basic.scala").sameAsResource("scala/scalafmt/basic.cleanWithCustomConf"); + } + + protected GradleRunner gradleRunner() throws IOException { + return GradleRunner.create() + .withGradleVersion("5.4") + .withProjectDir(rootFolder()) + .withPluginClasspath(); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4LazyTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4LazyTest.java new file mode 100644 index 0000000000..215583d467 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/Gradle5_4LazyTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2016 DiffPlug + * + * 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 + * + * http://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 com.diffplug.gradle.spotless; + +import org.gradle.testkit.runner.GradleRunner; +import org.junit.Test; + +import java.io.IOException; + +public class Gradle5_4LazyTest extends GradleIntegrationTest { + @Test + public void noConfigOnHelp() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'com.diffplug.gradle.spotless'", + "}", + "apply plugin: 'scala'", + "spotless {", + " scala {", + " scalafmt().configFile('scalafmt.conf')", + " }", + "}", + "project.tasks.matching { it.getName().startsWith(\"" + SpotlessTaskConstants.EXTENSION + "\") }", + ".configureEach { task -> throw new AssertionError(\"Spotless task configured\") }" + + ); + gradleRunner().withArguments("help").build(); + } + + protected GradleRunner gradleRunner() throws IOException { + return GradleRunner.create() + .withGradleVersion("5.4") + .withProjectDir(rootFolder()) + .withPluginClasspath(); + } +} diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationTest.java index 9f9a2618e6..e35063f006 100644 --- a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationTest.java +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/GradleIntegrationTest.java @@ -56,7 +56,7 @@ public void gitAttributes() throws IOException { setFile(".gitattributes").toContent("* text eol=lf"); } - protected final GradleRunner gradleRunner() throws IOException { + protected GradleRunner gradleRunner() throws IOException { return GradleRunner.create() // Test against Gradle 2.14.1 in order to maintain backwards compatibility. // https://github.com/diffplug/spotless/issues/161 From 93ae599bcbc71848bbd8d48de2ab9532d3fb420b Mon Sep 17 00:00:00 2001 From: carbotaniuman Date: Thu, 1 Aug 2019 12:30:05 -0500 Subject: [PATCH 3/3] Makes tests pass, should be ready --- .../gradle/spotless/SpotlessPlugin.java | 8 ------ .../SpotlessTaskSetupConfigAvoidance.java | 26 ++++++++++++------- 2 files changed, 16 insertions(+), 18 deletions(-) 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 94c86269b3..3caa5122da 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 @@ -19,15 +19,7 @@ import org.gradle.api.Plugin; import org.gradle.api.Project; -import org.gradle.api.Task; -import org.gradle.api.execution.TaskExecutionGraph; import org.gradle.api.plugins.BasePlugin; -import org.gradle.api.plugins.JavaBasePlugin; - -import com.diffplug.spotless.SpotlessCache; - -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import groovy.lang.Closure; public class SpotlessPlugin implements Plugin { SpotlessExtension spotlessExtension; diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java index b3ec259ec1..93534217ae 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskSetupConfigAvoidance.java @@ -24,7 +24,6 @@ import com.diffplug.spotless.SpotlessCache; import static com.diffplug.gradle.spotless.SpotlessTaskConstants.*; -import static com.diffplug.gradle.spotless.SpotlessTaskConstants.FILES_PROPERTY; final class SpotlessTaskSetupConfigAvoidance implements SpotlessTaskSetup { @Override @@ -65,17 +64,24 @@ public void accept(Project project, SpotlessExtension spotlessExtension) { rootCheckTaskProvider.configure(rootCheckTask -> rootCheckTask.dependsOn(checkTaskProvider)); rootApplyTaskProvider.configure(rootApplyTask -> rootApplyTask.dependsOn(applyTaskProvider)); // and they depend on the work task - checkTaskProvider.configure(checkTask -> checkTask.dependsOn(spotlessTaskProvider)); - applyTaskProvider.configure(applyTask -> applyTask.dependsOn(spotlessTaskProvider)); + checkTaskProvider.configure(checkTask -> { + checkTask.dependsOn(spotlessTaskProvider); + }); + applyTaskProvider.configure(applyTask -> { + applyTask.dependsOn(spotlessTaskProvider); + }); - // when the task graph is ready, we'll configure the spotlessTask appropriately project.getGradle().getTaskGraph().whenReady(graph -> { - if (checkTaskProvider.isPresent() && graph.hasTask(checkTaskProvider.get())) { - spotlessTaskProvider.configure(SpotlessTask::setCheck); - } - if (applyTaskProvider.isPresent() && graph.hasTask(applyTaskProvider.get())) { - spotlessTaskProvider.configure(SpotlessTask::setApply); - } + spotlessTaskProvider.configure(spotlessTask -> { + for(Task t : graph.getAllTasks()) { + if(t.getName().equals(checkTaskProvider.getName())) { + spotlessTask.setCheck(); + } + if(t.getName().equals(applyTaskProvider.getName())) { + spotlessTask.setApply(); + } + } + }); }); });