Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid eager creation of tasks for Spotless plugin #617

Merged
merged 3 commits into from
Jun 18, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}