Skip to content

Commit

Permalink
Modify IDE Hook to work with configuration cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Julius Lehmann authored and Julius Lehmann committed Sep 26, 2024
1 parent 7c3bead commit 475d85c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 96 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
public class SpotlessExtensionImpl extends SpotlessExtension {
final TaskProvider<?> rootCheckTask, rootApplyTask, rootDiagnoseTask;

final static String PROPERTY = "spotlessIdeHook";

public SpotlessExtensionImpl(Project project) {
super(project);
rootCheckTask = project.getTasks().register(EXTENSION + CHECK, task -> {
Expand All @@ -48,17 +50,17 @@ public SpotlessExtensionImpl(Project project) {

@Override
protected void createFormatTasks(String name, FormatExtension formatExtension) {
boolean isIdeHook = project.hasProperty(IdeHook.PROPERTY);
TaskContainer tasks = project.getTasks();

// create the SpotlessTask
String taskName = EXTENSION + SpotlessPlugin.capitalize(name);
TaskProvider<SpotlessTaskImpl> spotlessTask = tasks.register(taskName, SpotlessTaskImpl.class, task -> {
task.init(getRegisterDependenciesTask().getTaskService());
task.setGroup(TASK_GROUP);
task.setEnabled(!isIdeHook);
// clean removes the SpotlessCache, so we have to run after clean
task.mustRunAfter(BasePlugin.CLEAN_TASK_NAME);
task.getSpotlessIdeHook().set((String) project.findProperty(PROPERTY));
task.getProjectDir().set(project.getProjectDir());
});
project.afterEvaluate(unused -> {
spotlessTask.configure(task -> {
Expand All @@ -75,23 +77,16 @@ protected void createFormatTasks(String name, FormatExtension formatExtension) {
TaskProvider<SpotlessApply> applyTask = tasks.register(taskName + APPLY, SpotlessApply.class, task -> {
task.init(spotlessTask.get());
task.setGroup(TASK_GROUP);
task.setEnabled(!isIdeHook);
task.dependsOn(spotlessTask);
});
rootApplyTask.configure(task -> {
task.dependsOn(applyTask);

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()));
}
});

TaskProvider<SpotlessCheck> checkTask = tasks.register(taskName + CHECK, SpotlessCheck.class, task -> {
SpotlessTaskImpl source = spotlessTask.get();
task.setGroup(TASK_GROUP);
task.init(source);
task.setEnabled(!isIdeHook);
task.dependsOn(source);

// if the user runs both, make sure that apply happens first,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import org.gradle.api.GradleException;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
Expand All @@ -46,11 +49,14 @@ public abstract class SpotlessTaskImpl extends SpotlessTask {
@Internal
abstract DirectoryProperty getProjectDir();

@Optional
@Input
public abstract Property<String> getSpotlessIdeHook();

void init(Provider<SpotlessTaskService> service) {
taskServiceProvider = service;
SpotlessTaskService.usesServiceTolerateTestFailure(this, service);
getTaskService().set(service);
getProjectDir().set(getProject().getProjectDir());
}

// this field is stupid, but we need it, see https://github.com/diffplug/spotless/issues/1260
Expand Down Expand Up @@ -82,11 +88,13 @@ public void performAction(InputChanges inputs) throws Exception {
GitRatchetGradle ratchet = getRatchet();
for (FileChange fileChange : inputs.getFileChanges(target)) {
File input = fileChange.getFile();
if (fileChange.getChangeType() == ChangeType.REMOVED) {
deletePreviousResult(input);
} else {
if (input.isFile()) {
processInputFile(ratchet, formatter, input);
if (!getSpotlessIdeHook().isPresent() || input.equals(new File(getSpotlessIdeHook().get()))) {
if (fileChange.getChangeType() == ChangeType.REMOVED) {
deletePreviousResult(input);
} else {
if (input.isFile()) {
processInputFile(ratchet, formatter, input);
}
}
}
}
Expand Down

0 comments on commit 475d85c

Please sign in to comment.