Skip to content

Commit a25eded

Browse files
committed
SpotlessTaskImpl now generates both a formatted result and a lint result.
1 parent aac3972 commit a25eded

File tree

7 files changed

+72
-26
lines changed

7 files changed

+72
-26
lines changed

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessApply.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
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.
@@ -29,7 +29,7 @@ public abstract class SpotlessApply extends SpotlessTaskService.ClientTask {
2929
@TaskAction
3030
public void performAction() {
3131
getTaskService().get().registerApplyAlreadyRan(this);
32-
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(getSpotlessOutDirectory().get());
32+
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(contentDir());
3333
if (files.isEmpty()) {
3434
getState().setDidWork(sourceDidWork());
3535
} else {

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessCheck.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
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.
@@ -50,7 +50,7 @@ public void performAction() throws IOException {
5050
}
5151

5252
private void performAction(boolean isTest) throws IOException {
53-
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(getSpotlessOutDirectory().get());
53+
ConfigurableFileTree files = getConfigCacheWorkaround().fileTree().from(contentDir());
5454
if (files.isEmpty()) {
5555
getState().setDidWork(sourceDidWork());
5656
} else if (!isTest && applyHasRun()) {
@@ -101,7 +101,7 @@ public void visitFile(FileVisitDetails fileVisitDetails) {
101101
.runToFix("Run '" + calculateGradleCommand() + " " + getTaskPathPrefix() + "spotlessApply' to fix these violations.")
102102
.formatterFolder(
103103
getProjectDir().get().getAsFile().toPath(),
104-
getSpotlessOutDirectory().get().toPath(),
104+
contentDir().toPath(),
105105
getEncoding().get())
106106
.problemFiles(problemFiles)
107107
.getMessage());

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTask.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 DiffPlug
2+
* Copyright 2020-2022 DiffPlug
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.
@@ -154,6 +154,17 @@ public File getOutputDirectory() {
154154
return outputDirectory;
155155
}
156156

157+
static final String CONTENT = "content";
158+
static final String LINT = "lint";
159+
160+
File contentDir() {
161+
return new File(outputDirectory, CONTENT);
162+
}
163+
164+
File lintDir() {
165+
return new File(outputDirectory, LINT);
166+
}
167+
157168
protected final LiveCache<List<FormatterStep>> steps = createLive("steps");
158169
{
159170
steps.set(new ArrayList<FormatterStep>());

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskImpl.java

+41-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
2222
import java.nio.file.StandardCopyOption;
23+
import java.util.Collections;
24+
import java.util.List;
2325

2426
import javax.annotation.Nullable;
2527
import javax.inject.Inject;
@@ -38,6 +40,7 @@
3840
import com.diffplug.common.base.StringPrinter;
3941
import com.diffplug.spotless.DirtyState;
4042
import com.diffplug.spotless.Formatter;
43+
import com.diffplug.spotless.Lint;
4144
import com.diffplug.spotless.extra.GitRatchet;
4245

4346
@CacheableTask
@@ -64,7 +67,8 @@ public void performAction(InputChanges inputs) throws Exception {
6467
if (!inputs.isIncremental()) {
6568
getLogger().info("Not incremental: removing prior outputs");
6669
getFs().delete(d -> d.delete(outputDirectory));
67-
Files.createDirectories(outputDirectory.toPath());
70+
Files.createDirectories(contentDir().toPath());
71+
Files.createDirectories(lintDir().toPath());
6872
}
6973

7074
try (Formatter formatter = buildFormatter()) {
@@ -86,10 +90,14 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
8690
File output = getOutputFile(input);
8791
getLogger().debug("Applying format to " + input + " and writing to " + output);
8892
DirtyState dirtyState;
93+
List<Lint> lints;
8994
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
9095
dirtyState = DirtyState.clean();
96+
lints = Collections.emptyList();
9197
} else {
92-
dirtyState = DirtyState.of(formatter, input).calculateDirtyState();
98+
DirtyState.Calculation calculation = DirtyState.of(formatter, input);
99+
dirtyState = calculation.calculateDirtyState();
100+
lints = calculation.calculateLintAgainstRaw();
93101
}
94102
if (dirtyState.isClean()) {
95103
// Remove previous output if it exists
@@ -106,27 +114,46 @@ private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter,
106114
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
107115
dirtyState.writeCanonicalTo(output);
108116
}
117+
118+
File lint = getLintFile(input);
119+
if (lints.isEmpty()) {
120+
Files.deleteIfExists(lint.toPath());
121+
} else {
122+
Lint.toFile(lints, lint);
123+
}
109124
}
110125

111126
private void deletePreviousResult(File input) throws IOException {
112-
File output = getOutputFile(input);
113-
if (output.isDirectory()) {
114-
getFs().delete(d -> d.delete(output));
127+
delete(getOutputFile(input));
128+
delete(getLintFile(input));
129+
}
130+
131+
private File getOutputFile(File input) {
132+
return new File(contentDir(), relativize(input));
133+
}
134+
135+
private File getLintFile(File input) {
136+
return new File(lintDir(), relativize(input));
137+
}
138+
139+
private void delete(File file) throws IOException {
140+
if (file.isDirectory()) {
141+
getFs().delete(d -> d.delete(file));
115142
} else {
116-
Files.deleteIfExists(output.toPath());
143+
Files.deleteIfExists(file.toPath());
117144
}
118145
}
119146

120-
private File getOutputFile(File input) {
147+
private String relativize(File input) {
121148
File projectDir = getProjectDir().get().getAsFile();
122149
String outputFileName = FormatExtension.relativize(projectDir, input);
123-
if (outputFileName == null) {
124-
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
125-
printer.println("Spotless error! All target files must be within the project dir.");
126-
printer.println(" project dir: " + projectDir.getAbsolutePath());
127-
printer.println(" target: " + input.getAbsolutePath());
128-
}));
150+
if (outputFileName != null) {
151+
return outputFileName;
129152
}
130-
return new File(outputDirectory, outputFileName);
153+
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
154+
printer.println("Spotless error! All target files must be within the project dir.");
155+
printer.println(" project dir: " + projectDir.getAbsolutePath());
156+
printer.println(" target: " + input.getAbsolutePath());
157+
}));
131158
}
132159
}

Diff for: plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessTaskService.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2022 DiffPlug
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.
@@ -95,6 +95,14 @@ static abstract class ClientTask extends DefaultTask {
9595
@Internal
9696
abstract Property<File> getSpotlessOutDirectory();
9797

98+
File contentDir() {
99+
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.CONTENT);
100+
}
101+
102+
File lintDir() {
103+
return new File(getSpotlessOutDirectory().get(), SpotlessTaskImpl.LINT);
104+
}
105+
98106
@Internal
99107
abstract Property<SpotlessTaskService> getTaskService();
100108

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/FormatTaskTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
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.
@@ -47,7 +47,7 @@ public BuildServiceParameters.None getParameters() {
4747
@Test
4848
void testLineEndings() throws Exception {
4949
File testFile = setFile("testFile").toContent("\r\n");
50-
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
50+
File outputFile = new File(spotlessTask.contentDir(), "testFile");
5151

5252
spotlessTask.setTarget(Collections.singleton(testFile));
5353
Tasks.execute(spotlessTask);
@@ -58,7 +58,7 @@ void testLineEndings() throws Exception {
5858
@Test
5959
void testStep() throws Exception {
6060
File testFile = setFile("testFile").toContent("apple");
61-
File outputFile = new File(spotlessTask.getOutputDirectory(), "testFile");
61+
File outputFile = new File(spotlessTask.contentDir(), "testFile");
6262
spotlessTask.setTarget(Collections.singleton(testFile));
6363

6464
spotlessTask.addStep(FormatterStep.createNeverUpToDate("double-p", content -> content.replace("pp", "p")));

Diff for: plugin-gradle/src/test/java/com/diffplug/gradle/spotless/PaddedCellTaskTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2022 DiffPlug
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.
@@ -59,7 +59,7 @@ public BuildServiceParameters.None getParameters() {
5959
source = createFormatTask(name, step);
6060
check = createCheckTask(name, source);
6161
apply = createApplyTask(name, source);
62-
outputFile = new File(source.getOutputDirectory() + "/src", file.getName());
62+
outputFile = new File(source.contentDir(), "src/" + file.getName());
6363
}
6464

6565
private SpotlessTaskImpl createFormatTask(String name, FormatterStep step) {

0 commit comments

Comments
 (0)