Skip to content

Commit

Permalink
Merge pull request #617 from bigdaz/lazy-task-creation
Browse files Browse the repository at this point in the history
Avoid eager creation of tasks for Spotless plugin
  • Loading branch information
bigdaz authored Jun 18, 2020
2 parents 183c0b4 + 78cfd65 commit 1cc5f54
Showing 1 changed file with 52 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@
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.TaskContainer;
import org.gradle.api.tasks.TaskProvider;

public class SpotlessExtensionModern extends SpotlessExtensionBase {
public SpotlessExtensionModern(Project project) {
super(project);
rootCheckTask = project.task(EXTENSION + CHECK);
rootCheckTask.setGroup(TASK_GROUP);
rootCheckTask.setDescription(CHECK_DESCRIPTION);
rootApplyTask = project.task(EXTENSION + APPLY);
rootApplyTask.setGroup(TASK_GROUP);
rootApplyTask.setDescription(APPLY_DESCRIPTION);
rootDiagnoseTask = project.task(EXTENSION + DIAGNOSE);
rootDiagnoseTask.setGroup(TASK_GROUP); // no description on purpose
rootCheckTask = project.getTasks().register(EXTENSION + CHECK, task -> {
task.setGroup(TASK_GROUP);
task.setDescription(CHECK_DESCRIPTION);
});
rootApplyTask = project.getTasks().register(EXTENSION + APPLY, task -> {
task.setGroup(TASK_GROUP);
task.setDescription(APPLY_DESCRIPTION);
});
rootDiagnoseTask = project.getTasks().register(EXTENSION + DIAGNOSE, task -> {
task.setGroup(TASK_GROUP); // no description on purpose
});

project.afterEvaluate(unused -> {
if (enforceCheck) {
Expand All @@ -40,53 +44,58 @@ public SpotlessExtensionModern(Project project) {
});
}

final Task rootCheckTask, rootApplyTask, rootDiagnoseTask;
final TaskProvider<?> rootCheckTask, rootApplyTask, rootDiagnoseTask;

@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
// TODO level 1: implement SpotlessExtension::createFormatTasks, but using config avoidance
// TODO level 2: override configure(String name, Class<T> clazz, Action<T> configure) so that it is lazy
// TODO override configure(String name, Class<T> clazz, Action<T> configure) so that it is lazy

boolean isIdeHook = project.hasProperty(IdeHook.PROPERTY);
TaskContainer tasks = project.getTasks();
TaskProvider<?> cleanTask = tasks.named(BasePlugin.CLEAN_TASK_NAME);

// create the SpotlessTask
String taskName = EXTENSION + SpotlessPlugin.capitalize(name);
SpotlessTaskModern spotlessTask = project.getTasks().create(taskName, SpotlessTaskModern.class);
project.afterEvaluate(unused -> formatExtension.setupTask(spotlessTask));
TaskProvider<SpotlessTaskModern> spotlessTask = tasks.register(taskName, SpotlessTaskModern.class, task -> {
task.setEnabled(!isIdeHook);
// clean removes the SpotlessCache, so we have to run after clean
task.mustRunAfter(cleanTask);
});

// clean removes the SpotlessCache, so we have to run after clean
Task clean = project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);
spotlessTask.mustRunAfter(clean);
project.afterEvaluate(unused -> spotlessTask.configure(formatExtension::setupTask));

// create the check and apply control tasks
SpotlessCheck checkTask = project.getTasks().create(taskName + CHECK, SpotlessCheck.class);
checkTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
checkTask.source = spotlessTask;
checkTask.dependsOn(spotlessTask);
TaskProvider<SpotlessApply> applyTask = tasks.register(taskName + APPLY, SpotlessApply.class, task -> {
task.setEnabled(!isIdeHook);
task.dependsOn(spotlessTask);
task.setSpotlessOutDirectory(spotlessTask.get().getOutputDirectory());
task.linkSource(spotlessTask.get());
});
rootApplyTask.configure(task -> {
task.dependsOn(applyTask);

SpotlessApply applyTask = project.getTasks().create(taskName + APPLY, SpotlessApply.class);
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
applyTask.linkSource(spotlessTask);
applyTask.dependsOn(spotlessTask);
if (isIdeHook) {
// the rootApplyTask is no longer just a marker task, now it does a bit of work itself
task.doLast(unused -> IdeHook.performHook(spotlessTask.get()));
}
});

// if the user runs both, make sure that apply happens first,
checkTask.mustRunAfter(applyTask);
TaskProvider<SpotlessCheck> checkTask = tasks.register(taskName + CHECK, SpotlessCheck.class, task -> {
task.setEnabled(!isIdeHook);
task.dependsOn(spotlessTask);
task.setSpotlessOutDirectory(spotlessTask.get().getOutputDirectory());
task.source = spotlessTask.get();

// the root tasks depend on the control tasks
rootCheckTask.dependsOn(checkTask);
rootApplyTask.dependsOn(applyTask);
// if the user runs both, make sure that apply happens first,
task.mustRunAfter(applyTask);
});
rootCheckTask.configure(task -> task.dependsOn(checkTask));

// create the diagnose task
SpotlessDiagnoseTask diagnoseTask = project.getTasks().create(taskName + DIAGNOSE, SpotlessDiagnoseTask.class);
diagnoseTask.source = spotlessTask;
rootDiagnoseTask.dependsOn(diagnoseTask);
diagnoseTask.mustRunAfter(clean);

if (project.hasProperty(IdeHook.PROPERTY)) {
// disable the normal tasks, to disable their up-to-date checking
spotlessTask.setEnabled(false);
checkTask.setEnabled(false);
applyTask.setEnabled(false);
// the rootApplyTask is no longer just a marker task, now it does a bit of work itself
rootApplyTask.doLast(unused -> IdeHook.performHook(spotlessTask));
}
TaskProvider<SpotlessDiagnoseTask> diagnoseTask = tasks.register(taskName + DIAGNOSE, SpotlessDiagnoseTask.class, task -> {
task.source = spotlessTask.get();
task.mustRunAfter(cleanTask);
});
rootDiagnoseTask.configure(task -> task.dependsOn(diagnoseTask));
}
}

0 comments on commit 1cc5f54

Please sign in to comment.