Skip to content

Commit 2eebed3

Browse files
authored
Merge branch 'main' into fix-ktlint-48
2 parents 3409754 + c50503e commit 2eebed3

File tree

12 files changed

+265
-147
lines changed

12 files changed

+265
-147
lines changed

gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ VER_SLF4J=[1.6,2.0[
2727
VER_DURIAN=1.2.0
2828
VER_JGIT=5.13.1.202206130422-r
2929
VER_JUNIT=5.9.1
30-
VER_ASSERTJ=3.23.1
30+
VER_ASSERTJ=3.24.1
3131
VER_MOCKITO=4.11.0
3232

3333
# Used for Maven Plugin

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
* Support `ktlint` 0.48+ new rule disabling syntax ([#1456](https://github.com/diffplug/spotless/pull/1456)) fixes ([#1444](https://github.com/diffplug/spotless/issues/1444))
88
### Changes
99
* Bump default `ktlint` version to latest `0.47.1` -> `0.48.1` ([#1456](https://github.com/diffplug/spotless/pull/1456))
10+
* Reduce spurious invalidations of the up-to-date index file ([#1461](https://github.com/diffplug/spotless/pull/1461))
1011

1112
## [2.29.0] - 2023-01-02
1213
### Added
1314
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
15+
* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446))
1416
### Fixed
1517
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1618
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))

plugin-maven/README.md

+63-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check
5858
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
5959
- [Markdown](#markdown) ([flexmark](#flexmark))
6060
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
61+
- [JSON](#json)
6162
- Multiple languages
6263
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
6364
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -128,7 +129,16 @@ To use it in your pom, just [add the Spotless dependency](https://search.maven.o
128129
Spotless consists of a list of formats (in the example above, `misc` and `java`), and each format has:
129130
- a `target` (the files to format), which you set with [`includes` and `excludes`](https://github.com/diffplug/spotless/blob/989abbecff4d8373c6111c1a98f359eadc532429/plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java#L51-L55)
130131
- a list of `FormatterStep`, which are just `String -> String` functions, such as [`replace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Replace.java), [`replaceRegex`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/ReplaceRegex.java), [`trimTrailingWhitespace`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/TrimTrailingWhitespace.java), [`indent`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Indent.java), [`prettier`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/Prettier.java), [`eclipseWtp`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/EclipseWtp.java), and [`licenseHeader`](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/generic/LicenseHeader.java).
131-
132+
- **order matters**, and this is good! (More info [here](https://github.com/diffplug/spotless/blob/main/PADDEDCELL.md) and [here](https://github.com/diffplug/spotless/blob/main/CONTRIBUTING.md#how-spotless-works))
133+
- For example, `googleJavaFormat` always indents with spaces, but some wish it had a tab mode
134+
- ```xml
135+
<googleJavaFormat/> // this works
136+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
137+
```
138+
- ```xml
139+
<indent><tabs>true</tabs><spacesPerTab>2</spacesPerTab></indent>
140+
<googleJavaFormat/> // the tab indentation gets overwritten
141+
```
132142

133143
### Requirements
134144

@@ -178,6 +188,10 @@ any other maven phase (i.e. compile) then it can be configured as below;
178188
<include>src/test/java/**/*.java</include>
179189
</includes>
180190

191+
<googleJavaFormat /> <!-- has its own section below -->
192+
<eclipse /> <!-- has its own section below -->
193+
<prettier /> <!-- has its own section below -->
194+
181195
<importOrder /> <!-- standard import order -->
182196
<importOrder> <!-- or a custom ordering -->
183197
<wildcardsLast>false</wildcardsLast> <!-- Optional, default false. Sort wildcard import after specific imports -->
@@ -187,10 +201,6 @@ any other maven phase (i.e. compile) then it can be configured as below;
187201

188202
<removeUnusedImports /> <!-- self-explanatory -->
189203

190-
<googleJavaFormat /> <!-- has its own section below -->
191-
<eclipse /> <!-- has its own section below -->
192-
<prettier /> <!-- has its own section below -->
193-
194204
<formatAnnotations /> <!-- fixes formatting of type annotations, see below -->
195205

196206
<licenseHeader>
@@ -431,7 +441,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
431441
<configuration>
432442
<cpp>
433443
<includes> <!-- You have to set the target manually -->
434-
<include>src/native/**</inclue>
444+
<include>src/native/**</include>
435445
</includes>
436446

437447
<eclipseCdt /> <!-- has its own section below -->
@@ -700,6 +710,53 @@ The auto-discovery of config files (up the file tree) will not work when using t
700710

701711
For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.
702712

713+
## JSON
714+
715+
- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/json.java)
716+
717+
```xml
718+
<configuration>
719+
<json>
720+
<includes> <!-- You have to set the target manually -->
721+
<include>src/**/*.json</include>
722+
</includes>
723+
724+
<simple /> <!-- has its own section below -->
725+
<gson /> <!-- has its own section below -->
726+
</json>
727+
</configuration>
728+
```
729+
730+
### simple
731+
732+
Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
733+
734+
```xml
735+
<simple>
736+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
737+
</simple>
738+
```
739+
740+
### Gson
741+
742+
Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files.
743+
744+
```xml
745+
<gson>
746+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
747+
<sortByKeys>false</sortByKeys> <!-- optional: sort JSON by its keys -->
748+
<escapeHtml>false</indentSpaces> <!-- optional: escape HTML in values -->
749+
<version>2.8.1</version> <!-- optional: specify version -->
750+
</gson>
751+
```
752+
753+
Notes:
754+
* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either
755+
all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former.
756+
* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the
757+
[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String))
758+
for details.
759+
703760
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
704761

705762
## Prettier

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/FileIndex.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -76,7 +76,7 @@ static FileIndex read(FileIndexConfig config, Log log) {
7676
PluginFingerprint computedFingerprint = config.getPluginFingerprint();
7777
PluginFingerprint storedFingerprint = PluginFingerprint.from(firstLine);
7878
if (!computedFingerprint.equals(storedFingerprint)) {
79-
log.info("Fingerprint mismatch in the index file. Fallback to an empty index");
79+
log.info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
8080
return emptyIndexFallback(config);
8181
} else {
8282
Content content = readIndexContent(reader, config.getProjectDir(), log);

plugin-maven/src/main/java/com/diffplug/spotless/maven/incremental/PluginFingerprint.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2022 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -17,17 +17,21 @@
1717

1818
import java.io.IOException;
1919
import java.io.UncheckedIOException;
20-
import java.util.ArrayList;
2120
import java.util.Base64;
22-
import java.util.List;
2321
import java.util.Objects;
2422

25-
import org.apache.maven.model.Dependency;
2623
import org.apache.maven.model.Plugin;
2724
import org.apache.maven.project.MavenProject;
2825

2926
import com.diffplug.spotless.Formatter;
3027

28+
/**
29+
* Represents a particular Spotless Maven plugin setup using a Base64-encoded serialized form of:
30+
* <ol>
31+
* <li>Plugin version as configured in the POM</li>
32+
* <li>Formatter instances created according to the POM configuration</li>
33+
* </ol>
34+
*/
3135
class PluginFingerprint {
3236

3337
private static final String SPOTLESS_PLUGIN_KEY = "com.diffplug.spotless:spotless-maven-plugin";
@@ -83,22 +87,15 @@ public String toString() {
8387
}
8488

8589
private static byte[] digest(Plugin plugin, Iterable<Formatter> formatters) {
86-
// dependencies can be an unserializable org.apache.maven.model.merge.ModelMerger$MergingList
87-
// replace it with a serializable ArrayList
88-
List<Dependency> dependencies = plugin.getDependencies();
89-
plugin.setDependencies(new ArrayList<>(dependencies));
9090
try (ObjectDigestOutputStream out = ObjectDigestOutputStream.create()) {
91-
out.writeObject(plugin);
91+
out.writeObject(plugin.getVersion());
9292
for (Formatter formatter : formatters) {
9393
out.writeObject(formatter);
9494
}
9595
out.flush();
9696
return out.digest();
9797
} catch (IOException e) {
9898
throw new UncheckedIOException("Unable to serialize plugin " + plugin, e);
99-
} finally {
100-
// reset the original list
101-
plugin.setDependencies(dependencies);
10299
}
103100
}
104101
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 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.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.gson.GsonStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Gson implements FormatterStepFactory {
26+
private static final String DEFAULT_GSON_VERSION = "2.8.9";
27+
28+
@Parameter
29+
int indentSpaces = Json.DEFAULT_INDENTATION;
30+
31+
@Parameter
32+
boolean sortByKeys = false;
33+
34+
@Parameter
35+
boolean escapeHtml = false;
36+
37+
@Parameter
38+
String version = DEFAULT_GSON_VERSION;
39+
40+
@Override
41+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
42+
int indentSpaces = this.indentSpaces;
43+
return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner());
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 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.json;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <json>...</json>} configuration element.
25+
*/
26+
public class Json extends FormatterFactory {
27+
public static final int DEFAULT_INDENTATION = 4;
28+
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addSimple(Simple simple) {
40+
addStepFactory(simple);
41+
}
42+
43+
public void addGson(Gson gson) {
44+
addStepFactory(gson);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 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.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.JsonSimpleStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Simple implements FormatterStepFactory {
26+
27+
@Parameter
28+
int indentSpaces = Json.DEFAULT_INDENTATION;
29+
30+
@Override
31+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
32+
int indentSpaces = this.indentSpaces;
33+
return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner());
34+
}
35+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -156,6 +156,10 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException {
156156
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
157157
}
158158

159+
protected void writePomWithJsonSteps(String... steps) throws IOException {
160+
writePom(groupWithSteps("json", including("**/*.json"), steps));
161+
}
162+
159163
protected void writePom(String... configuration) throws IOException {
160164
writePom(null, configuration, null);
161165
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/incremental/FileIndexTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 DiffPlug
2+
* Copyright 2021-2023 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.
@@ -60,7 +60,7 @@ void readFallsBackToEmptyIndexOnFingerprintMismatch() throws Exception {
6060
FileIndex index = FileIndex.read(config, log);
6161

6262
assertThat(index.size()).isZero();
63-
verify(log).info("Fingerprint mismatch in the index file. Fallback to an empty index");
63+
verify(log).info("Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index");
6464
}
6565

6666
@Test

0 commit comments

Comments
 (0)