Skip to content

Commit afde073

Browse files
committed
Make format checking cacheable
Closes gh-164
1 parent 23720b2 commit afde073

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/CheckTask.java

+33-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,10 +18,18 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.nio.file.Files;
22+
import java.nio.file.StandardOpenOption;
23+
import java.util.Collections;
2124
import java.util.List;
2225
import java.util.stream.Collectors;
2326

2427
import org.gradle.api.GradleException;
28+
import org.gradle.api.file.FileTree;
29+
import org.gradle.api.tasks.CacheableTask;
30+
import org.gradle.api.tasks.OutputFile;
31+
import org.gradle.api.tasks.PathSensitive;
32+
import org.gradle.api.tasks.PathSensitivity;
2533
import org.gradle.api.tasks.TaskAction;
2634

2735
import io.spring.javaformat.formatter.FileEdit;
@@ -31,6 +39,7 @@
3139
*
3240
* @author Phillip Webb
3341
*/
42+
@CacheableTask
3443
public class CheckTask extends FormatterTask {
3544

3645
/**
@@ -43,20 +52,39 @@ public class CheckTask extends FormatterTask {
4352
*/
4453
public static final String DESCRIPTION = "Run Spring Java formatting checks";
4554

46-
public CheckTask() {
47-
getOutputs().upToDateWhen((task) -> true);
48-
}
55+
private File reportLocation;
4956

5057
@TaskAction
5158
public void checkFormatting() throws IOException, InterruptedException {
5259
List<File> problems = formatFiles().filter(FileEdit::hasEdits).map(FileEdit::getFile)
5360
.collect(Collectors.toList());
61+
this.reportLocation.getParentFile().mkdirs();
5462
if (!problems.isEmpty()) {
5563
StringBuilder message = new StringBuilder("Formatting violations found in the following files:\n");
56-
problems.stream().forEach((f) -> message.append(" * " + f + "\n"));
64+
problems.stream().forEach((f) -> message.append(" * " + getProject().relativePath(f) + "\n"));
5765
message.append("\nRun `format` to fix.");
66+
Files.write(this.reportLocation.toPath(), Collections.singletonList(message.toString()),
67+
StandardOpenOption.CREATE);
5868
throw new GradleException(message.toString());
5969
}
70+
else {
71+
this.reportLocation.createNewFile();
72+
}
73+
}
74+
75+
@Override
76+
@PathSensitive(PathSensitivity.RELATIVE)
77+
public FileTree getSource() {
78+
return super.getSource();
79+
}
80+
81+
@OutputFile
82+
public File getReportLocation() {
83+
return this.reportLocation;
84+
}
85+
86+
public void setReportLocation(File reportLocation) {
87+
this.reportLocation = reportLocation;
6088
}
6189

6290
}

spring-javaformat-gradle/spring-javaformat-gradle-plugin/src/main/java/io/spring/javaformat/gradle/SpringJavaFormatPlugin.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package io.spring.javaformat.gradle;
1818

19+
import java.io.File;
20+
1921
import org.gradle.api.Plugin;
2022
import org.gradle.api.Project;
2123
import org.gradle.api.Task;
@@ -52,6 +54,8 @@ private void addSourceTasks() {
5254

5355
private void addSourceTasks(SourceSet sourceSet, Task checkAll, Task formatAll) {
5456
CheckTask checkTask = addSourceTask(sourceSet, CheckTask.class, CheckTask.NAME, CheckTask.DESCRIPTION);
57+
checkTask.setReportLocation(
58+
new File(this.project.getBuildDir(), "reports/format/" + sourceSet.getName() + "/check-format.txt"));
5559
checkAll.dependsOn(checkTask);
5660
FormatTask formatSourceSet = addSourceTask(sourceSet, FormatTask.class, FormatTask.NAME,
5761
FormatTask.DESCRIPTION);

spring-javaformat/spring-javaformat-formatter-shader/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<version>0.0.19-SNAPSHOT</version>
1010
</parent>
1111
<artifactId>spring-javaformat-formatter-shader</artifactId>
12-
<name>Spring JavaFormat Formatter Shaded</name>
12+
<name>Spring JavaFormat Formatter Shader</name>
1313
<properties>
1414
<main.basedir>${basedir}/../..</main.basedir>
1515
</properties>

0 commit comments

Comments
 (0)