Skip to content

Commit

Permalink
Each FormatExtension now maintains a list of actions, which it exec…
Browse files Browse the repository at this point in the history
…utes lazily only if the task is configured.
  • Loading branch information
nedtwigg committed Jun 19, 2020
1 parent 9a02419 commit 1e2801d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import javax.annotation.Nullable;

import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
Expand Down Expand Up @@ -56,6 +57,7 @@
/** Adds a `spotless{Name}Check` and `spotless{Name}Apply` task. */
public class FormatExtension {
final SpotlessExtensionBase spotless;
final List<Action<FormatExtension>> modernLazyActions = new ArrayList<>();

public FormatExtension(SpotlessExtensionBase spotless) {
this.spotless = Objects.requireNonNull(spotless);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.diffplug.gradle.spotless;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.plugins.BasePlugin;
import org.gradle.api.plugins.JavaBasePlugin;
Expand Down Expand Up @@ -46,10 +47,14 @@ public SpotlessExtensionModern(Project project) {

final TaskProvider<?> rootCheckTask, rootApplyTask, rootDiagnoseTask;

@SuppressWarnings("unchecked")
@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
// TODO override configure(String name, Class<T> clazz, Action<T> configure) so that it is lazy
public <T extends FormatExtension> void format(String name, Class<T> clazz, Action<T> configure) {
maybeCreate(name, clazz).modernLazyActions.add((Action<FormatExtension>) configure);
}

@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
boolean isIdeHook = project.hasProperty(IdeHook.PROPERTY);
TaskContainer tasks = project.getTasks();
TaskProvider<?> cleanTask = tasks.named(BasePlugin.CLEAN_TASK_NAME);
Expand All @@ -62,7 +67,16 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
task.mustRunAfter(cleanTask);
});

project.afterEvaluate(unused -> spotlessTask.configure(formatExtension::setupTask));
project.afterEvaluate(unused -> {
spotlessTask.configure(task -> {
// now that the task is being configured, we execute our actions
for (Action<FormatExtension> lazyAction : formatExtension.modernLazyActions) {
lazyAction.execute(formatExtension);
}
// and now we'll setup the task
formatExtension.setupTask(task);
});
});

// create the check and apply control tasks
TaskProvider<SpotlessApply> applyTask = tasks.register(taskName + APPLY, SpotlessApply.class, task -> {
Expand Down

0 comments on commit 1e2801d

Please sign in to comment.