-
Notifications
You must be signed in to change notification settings - Fork 435
/
SpotlessTaskImpl.java
132 lines (119 loc) · 4.61 KB
/
SpotlessTaskImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2016-2021 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.gradle.spotless;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import javax.annotation.Nullable;
import javax.inject.Inject;
import org.gradle.api.GradleException;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.provider.Provider;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;
import org.gradle.work.ChangeType;
import org.gradle.work.FileChange;
import org.gradle.work.InputChanges;
import com.diffplug.common.base.StringPrinter;
import com.diffplug.spotless.Formatter;
import com.diffplug.spotless.PaddedCell;
import com.diffplug.spotless.extra.GitRatchet;
@CacheableTask
public abstract class SpotlessTaskImpl extends SpotlessTask {
@Internal
abstract DirectoryProperty getProjectDir();
void init(Provider<SpotlessTaskService> service) {
getTaskService().set(service);
getProjectDir().set(getProject().getProjectDir());
}
@Inject
protected abstract FileSystemOperations getFs();
@TaskAction
public void performAction(InputChanges inputs) throws Exception {
SpotlessTaskService taskService = getTaskService().get();
taskService.registerSourceAlreadyRan(this);
if (target == null) {
throw new GradleException("You must specify 'Iterable<File> target'");
}
if (!inputs.isIncremental()) {
getLogger().info("Not incremental: removing prior outputs");
getFs().delete(d -> d.delete(outputDirectory));
Files.createDirectories(outputDirectory.toPath());
}
try (Formatter formatter = buildFormatter()) {
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);
}
}
}
}
}
private void processInputFile(@Nullable GitRatchet ratchet, Formatter formatter, File input) throws IOException {
File output = getOutputFile(input);
getLogger().debug("Applying format to " + input + " and writing to " + output);
PaddedCell.DirtyState dirtyState;
if (ratchet != null && ratchet.isClean(getProjectDir().get().getAsFile(), getRootTreeSha(), input)) {
dirtyState = PaddedCell.isClean();
} else {
dirtyState = PaddedCell.calculateDirtyState(formatter, input);
}
if (dirtyState.isClean()) {
// Remove previous output if it exists
Files.deleteIfExists(output.toPath());
} else if (dirtyState.didNotConverge()) {
getLogger().warn("Skipping '" + input + "' because it does not converge. Run {@code spotlessDiagnose} to understand why");
} else {
Path parentDir = output.toPath().getParent();
if (parentDir == null) {
throw new IllegalStateException("Every file has a parent folder.");
}
Files.createDirectories(parentDir);
// Need to copy the original file to the tmp location just to remember the file attributes
Files.copy(input.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES);
dirtyState.writeCanonicalTo(output);
}
}
private void deletePreviousResult(File input) throws IOException {
File output = getOutputFile(input);
if (output.isDirectory()) {
getFs().delete(d -> d.delete(output));
} else {
Files.deleteIfExists(output.toPath());
}
}
private File getOutputFile(File input) {
File projectDir = getProjectDir().get().getAsFile();
String outputFileName = FormatExtension.relativize(projectDir, input);
if (outputFileName == null) {
throw new IllegalArgumentException(StringPrinter.buildString(printer -> {
printer.println("Spotless error! All target files must be within the project dir.");
printer.println(" project dir: " + projectDir.getAbsolutePath());
printer.println(" target: " + input.getAbsolutePath());
}));
}
return new File(outputDirectory, outputFileName);
}
}