Skip to content

Commit 1d6b717

Browse files
author
Dimitrii Lipiridi
committed
feat: Prepare Maven's IDE hook
1 parent cb5cb6a commit 1d6b717

File tree

5 files changed

+132
-25
lines changed

5 files changed

+132
-25
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void setEncoding(String name) {
124124

125125
/** @see #setRatchetFrom(String) */
126126
public String getRatchetFrom() {
127-
return ratchetFrom == RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL ? spotless.getRatchetFrom() : ratchetFrom;
127+
return ratchetFrom.equals(RATCHETFROM_NOT_SET_AT_FORMAT_LEVEL) ? spotless.getRatchetFrom() : ratchetFrom;
128128
}
129129

130130
/**

plugin-maven/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
compileOnly "org.eclipse.aether:aether-api:${VER_ECLIPSE_AETHER}"
4040

4141
implementation "com.diffplug.durian:durian-core:${VER_DURIAN}"
42+
implementation "com.diffplug.durian:durian-io:${VER_DURIAN}"
4243
implementation "com.diffplug.durian:durian-collect:${VER_DURIAN}"
4344
implementation("org.codehaus.plexus:plexus-resources:${VER_PLEXUS_RESOURCES}")
4445
implementation "org.eclipse.jgit:org.eclipse.jgit:${VER_JGIT}"

plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,6 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
187187
@Parameter(property = "spotlessFiles")
188188
private String filePatterns;
189189

190-
@Parameter(property = "spotlessIdeHook")
191-
private String spotlessIdeHook;
192-
193190
@Parameter(property = LicenseHeaderStep.spotlessSetLicenseHeaderYearsFromGitHistory)
194191
private String setLicenseHeaderYearsFromGitHistory;
195192

@@ -254,10 +251,6 @@ private boolean shouldSkip() {
254251
}
255252

256253
private List<File> collectFiles(FormatterFactory formatterFactory, FormatterConfig config) {
257-
if (!(spotlessIdeHook == null || spotlessIdeHook.isEmpty())) {
258-
return fetchFileFromIdeHook();
259-
}
260-
261254
Optional<String> ratchetFrom = formatterFactory.ratchetFrom(config);
262255
try {
263256
final List<File> files;
@@ -271,30 +264,21 @@ private List<File> collectFiles(FormatterFactory formatterFactory, FormatterConf
271264
}
272265
final String[] includePatterns = this.filePatterns.split(",");
273266
final List<Pattern> compiledIncludePatterns = Arrays.stream(includePatterns)
274-
.map(Pattern::compile)
275-
.collect(Collectors.toList());
267+
.map(Pattern::compile)
268+
.collect(Collectors.toList());
276269
final Predicate<File> shouldInclude = file -> compiledIncludePatterns
277-
.stream()
278-
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
279-
.matches());
270+
.stream()
271+
.anyMatch(filePattern -> filePattern.matcher(file.getAbsolutePath())
272+
.matches());
280273
return files
281-
.stream()
282-
.filter(shouldInclude)
283-
.collect(toList());
274+
.stream()
275+
.filter(shouldInclude)
276+
.collect(toList());
284277
} catch (IOException e) {
285278
throw new PluginException("Unable to scan file tree rooted at " + baseDir, e);
286279
}
287280
}
288281

289-
private List<File> fetchFileFromIdeHook() {
290-
File file = new File(spotlessIdeHook);
291-
if (!file.isAbsolute()) {
292-
throw new PluginException("Argument passed to spotlessIdeHook must be an absolute path");
293-
}
294-
295-
return List.of(file);
296-
}
297-
298282
private List<File> collectFilesFromGit(FormatterFactory formatterFactory, String ratchetFrom) {
299283
MatchPatterns includePatterns = MatchPatterns.from(
300284
withNormalizedFileSeparators(getIncludes(formatterFactory)));
@@ -400,4 +384,8 @@ private UpToDateChecker createUpToDateChecker(Iterable<Formatter> formatters) {
400384
}
401385
return UpToDateChecker.wrapWithBuildContext(checker, buildContext);
402386
}
387+
388+
protected File getBaseDir() {
389+
return baseDir;
390+
}
403391
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2016-2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven;
17+
18+
import com.diffplug.common.base.Errors;
19+
import com.diffplug.common.io.ByteStreams;
20+
import com.diffplug.spotless.Formatter;
21+
import com.diffplug.spotless.PaddedCell;
22+
23+
import java.io.File;
24+
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.util.List;
27+
28+
class IdeHook {
29+
30+
private static void dumpIsClean() {
31+
System.err.println("IS CLEAN");
32+
}
33+
34+
static void performHook(Iterable<File> projectFiles, String path, Formatter formatter, File projectDir, String ratchetFrom, boolean spotlessIdeHookUseStdIn, boolean spotlessIdeHookUseStdOut) {
35+
File file = new File(path);
36+
if (!file.isAbsolute()) {
37+
System.err.println("Argument passed to spotlessIdeHook must be an absolute path");
38+
return;
39+
}
40+
if (projectContainsFile(projectFiles, file)) {
41+
try {
42+
if (ratchetFrom != null) {
43+
GitRatchetMaven ratchet = GitRatchetMaven.instance();
44+
if (ratchet.isClean(projectDir, ratchet.rootTreeShaOf(projectDir, ratchetFrom), file)) {
45+
dumpIsClean();
46+
return;
47+
}
48+
}
49+
byte[] bytes;
50+
if (spotlessIdeHookUseStdIn) {
51+
bytes = ByteStreams.toByteArray(System.in);
52+
} else {
53+
bytes = Files.readAllBytes(file.toPath());
54+
}
55+
PaddedCell.DirtyState dirty = PaddedCell.calculateDirtyState(formatter, file, bytes);
56+
if (dirty.isClean()) {
57+
dumpIsClean();
58+
} else if (dirty.didNotConverge()) {
59+
System.err.println("DID NOT CONVERGE");
60+
System.err.println("Run 'spotlessDiagnose' for details https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md");
61+
} else {
62+
System.err.println("IS DIRTY");
63+
if (spotlessIdeHookUseStdOut) {
64+
dirty.writeCanonicalTo(System.out);
65+
} else {
66+
dirty.writeCanonicalTo(file);
67+
}
68+
}
69+
} catch (IOException e) {
70+
e.printStackTrace(System.err);
71+
throw Errors.asRuntime(e);
72+
} finally {
73+
System.err.close();
74+
System.out.close();
75+
}
76+
}
77+
}
78+
79+
private static boolean projectContainsFile(Iterable<File> projectFiles, File file) {
80+
for (File projectFile : projectFiles) {
81+
if (projectFile.equals(file)) {
82+
return true;
83+
}
84+
}
85+
return false;
86+
}
87+
}

plugin-maven/src/main/java/com/diffplug/spotless/maven/SpotlessApplyMojo.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,39 @@
1818
import java.io.File;
1919
import java.io.IOException;
2020

21+
import java.util.List;
22+
2123
import org.apache.maven.plugin.MojoExecutionException;
2224
import org.apache.maven.plugins.annotations.Mojo;
2325

2426
import com.diffplug.spotless.Formatter;
2527
import com.diffplug.spotless.PaddedCell;
2628
import com.diffplug.spotless.maven.incremental.UpToDateChecker;
2729

30+
import org.apache.maven.plugins.annotations.Parameter;
31+
2832
/**
2933
* Performs formatting of all source files according to configured formatters.
3034
*/
3135
@Mojo(name = AbstractSpotlessMojo.GOAL_APPLY, threadSafe = true)
3236
public class SpotlessApplyMojo extends AbstractSpotlessMojo {
3337

38+
@Parameter(property = "spotlessIdeHook")
39+
private String spotlessIdeHook;
40+
41+
@Parameter(property = "spotlessIdeHookUseStdIn")
42+
private boolean spotlessIdeHookUseStdIn;
43+
44+
@Parameter(property = "spotlessIdeHookUseStdOut")
45+
private boolean spotlessIdeHookUseStdOut;
46+
3447
@Override
3548
protected void process(Iterable<File> files, Formatter formatter, UpToDateChecker upToDateChecker) throws MojoExecutionException {
49+
if(isIdeHook()) {
50+
IdeHook.performHook(files, spotlessIdeHook, formatter, getBaseDir(), null, spotlessIdeHookUseStdIn, spotlessIdeHookUseStdOut);
51+
return;
52+
}
53+
3654
ImpactedFilesTracker counter = new ImpactedFilesTracker();
3755

3856
for (File file : files) {
@@ -69,4 +87,17 @@ protected void process(Iterable<File> files, Formatter formatter, UpToDateChecke
6987
getLog().debug(String.format("Spotless.%s has no target files. Examine your `<includes>`: https://github.com/diffplug/spotless/tree/main/plugin-maven#quickstart", formatter.getName()));
7088
}
7189
}
90+
91+
private boolean isIdeHook() {
92+
return !(spotlessIdeHook == null || spotlessIdeHook.isEmpty());
93+
}
94+
95+
private List<File> fetchFileFromIdeHook() {
96+
File file = new File(spotlessIdeHook);
97+
if (!file.isAbsolute()) {
98+
throw new PluginException("Argument passed to spotlessIdeHook must be an absolute path");
99+
}
100+
101+
return List.of(file);
102+
}
72103
}

0 commit comments

Comments
 (0)