Skip to content

Commit 386cf58

Browse files
committed
Copy-paste the legacy implementations into the modern implementations, so that we don't have to reimplement all at once.
1 parent 69f7d26 commit 386cf58

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtensionModern.java

+62
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,77 @@
1616
package com.diffplug.gradle.spotless;
1717

1818
import org.gradle.api.Project;
19+
import org.gradle.api.Task;
20+
import org.gradle.api.plugins.BasePlugin;
21+
import org.gradle.api.plugins.JavaBasePlugin;
1922

2023
public class SpotlessExtensionModern extends SpotlessExtensionBase {
2124
public SpotlessExtensionModern(Project project) {
2225
super(project);
26+
rootCheckTask = project.task(EXTENSION + CHECK);
27+
rootCheckTask.setGroup(TASK_GROUP);
28+
rootCheckTask.setDescription(CHECK_DESCRIPTION);
29+
rootApplyTask = project.task(EXTENSION + APPLY);
30+
rootApplyTask.setGroup(TASK_GROUP);
31+
rootApplyTask.setDescription(APPLY_DESCRIPTION);
32+
rootDiagnoseTask = project.task(EXTENSION + DIAGNOSE);
33+
rootDiagnoseTask.setGroup(TASK_GROUP); // no description on purpose
34+
35+
project.afterEvaluate(unused -> {
36+
if (enforceCheck) {
37+
project.getTasks().named(JavaBasePlugin.CHECK_TASK_NAME)
38+
.configure(task -> task.dependsOn(rootCheckTask));
39+
}
40+
});
2341
}
2442

43+
final Task rootCheckTask, rootApplyTask, rootDiagnoseTask;
44+
2545
@Override
2646
protected void createFormatTasks(String name, FormatExtension formatExtension) {
2747
// TODO level 1: implement SpotlessExtension::createFormatTasks, but using config avoidance
2848
// TODO level 2: override configure(String name, Class<T> clazz, Action<T> configure) so that it is lazy
49+
50+
// create the SpotlessTask
51+
String taskName = EXTENSION + SpotlessPlugin.capitalize(name);
52+
SpotlessTaskModern spotlessTask = project.getTasks().create(taskName, SpotlessTaskModern.class);
53+
project.afterEvaluate(unused -> formatExtension.setupTask(spotlessTask));
54+
55+
// clean removes the SpotlessCache, so we have to run after clean
56+
Task clean = project.getTasks().getByName(BasePlugin.CLEAN_TASK_NAME);
57+
spotlessTask.mustRunAfter(clean);
58+
59+
// create the check and apply control tasks
60+
SpotlessCheck checkTask = project.getTasks().create(taskName + CHECK, SpotlessCheck.class);
61+
checkTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
62+
checkTask.source = spotlessTask;
63+
checkTask.dependsOn(spotlessTask);
64+
65+
SpotlessApply applyTask = project.getTasks().create(taskName + APPLY, SpotlessApply.class);
66+
applyTask.setSpotlessOutDirectory(spotlessTask.getOutputDirectory());
67+
applyTask.linkSource(spotlessTask);
68+
applyTask.dependsOn(spotlessTask);
69+
70+
// if the user runs both, make sure that apply happens first,
71+
checkTask.mustRunAfter(applyTask);
72+
73+
// the root tasks depend on the control tasks
74+
rootCheckTask.dependsOn(checkTask);
75+
rootApplyTask.dependsOn(applyTask);
76+
77+
// create the diagnose task
78+
SpotlessDiagnoseTask diagnoseTask = project.getTasks().create(taskName + DIAGNOSE, SpotlessDiagnoseTask.class);
79+
diagnoseTask.source = spotlessTask;
80+
rootDiagnoseTask.dependsOn(diagnoseTask);
81+
diagnoseTask.mustRunAfter(clean);
82+
83+
if (project.hasProperty(IdeHook.PROPERTY)) {
84+
// disable the normal tasks, to disable their up-to-date checking
85+
spotlessTask.setEnabled(false);
86+
checkTask.setEnabled(false);
87+
applyTask.setEnabled(false);
88+
// the rootApplyTask is no longer just a marker task, now it does a bit of work itself
89+
rootApplyTask.doLast(unused -> IdeHook.performHook(spotlessTask));
90+
}
2991
}
3092
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskModern.java

+68-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,80 @@
1515
*/
1616
package com.diffplug.gradle.spotless;
1717

18+
import java.io.File;
19+
import java.io.IOException;
20+
import java.nio.file.Files;
21+
import java.util.Arrays;
22+
import java.util.List;
23+
import java.util.regex.Pattern;
24+
import java.util.stream.Collectors;
25+
26+
import org.gradle.api.GradleException;
1827
import org.gradle.api.tasks.CacheableTask;
1928
import org.gradle.api.tasks.TaskAction;
29+
import org.gradle.api.tasks.incremental.IncrementalTaskInputs;
30+
31+
import com.diffplug.common.base.Errors;
32+
import com.diffplug.common.base.Preconditions;
33+
import com.diffplug.common.base.Throwing;
34+
import com.diffplug.spotless.Formatter;
2035

2136
@CacheableTask
2237
public class SpotlessTaskModern extends SpotlessTask {
2338
@TaskAction
24-
public void performAction() throws Exception {
39+
public void performAction(IncrementalTaskInputs inputs) throws Exception {
2540
// TODO: implement using the InputChanges api
41+
42+
if (target == null) {
43+
throw new GradleException("You must specify 'Iterable<File> target'");
44+
}
45+
46+
if (!inputs.isIncremental()) {
47+
getLogger().info("Not incremental: removing prior outputs");
48+
getProject().delete(outputDirectory);
49+
Files.createDirectories(outputDirectory.toPath());
50+
}
51+
52+
Throwing.Specific.Predicate<File, IOException> shouldInclude;
53+
if (this.filePatterns.isEmpty()) {
54+
shouldInclude = file -> true;
55+
} else {
56+
Preconditions.checkArgument(ratchet == null,
57+
"Cannot use 'ratchetFrom' and '-PspotlessFiles' at the same time");
58+
59+
// a list of files has been passed in via project property
60+
final String[] includePatterns = this.filePatterns.split(",");
61+
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
62+
.map(Pattern::compile)
63+
.collect(Collectors.toList());
64+
shouldInclude = file -> compiledIncludePatterns
65+
.stream()
66+
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
67+
.matches());
68+
}
69+
70+
try (Formatter formatter = buildFormatter()) {
71+
inputs.outOfDate(inputDetails -> {
72+
File input = inputDetails.getFile();
73+
try {
74+
if (shouldInclude.test(input) && input.isFile()) {
75+
processInputFile(formatter, input);
76+
}
77+
} catch (IOException e) {
78+
throw Errors.asRuntime(e);
79+
}
80+
});
81+
}
82+
83+
inputs.removed(removedDetails -> {
84+
File input = removedDetails.getFile();
85+
try {
86+
if (shouldInclude.test(input)) {
87+
deletePreviousResult(input);
88+
}
89+
} catch (IOException e) {
90+
throw Errors.asRuntime(e);
91+
}
92+
});
2693
}
2794
}

0 commit comments

Comments
 (0)