Skip to content

Commit 28e080b

Browse files
committed
Add Gradle bindings for JSON formatter
As we've created a JVM JSON formatter as part of #850, we should make it possible to use it natively in Gradle, which requires we add it as a new supported type in `SpotlessExtension`. When configured, we'll default to including any JSON files under the `src` directory, while allowing it to be overriden if requested.
1 parent 42c59c8 commit 28e080b

File tree

4 files changed

+147
-1
lines changed

4 files changed

+147
-1
lines changed

plugin-gradle/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ Spotless supports all of Gradle's built-in performance features (incremental bui
6868
- [Antlr4](#antlr4) ([antlr4formatter](#antlr4formatter))
6969
- [SQL](#sql) ([dbeaver](#dbeaver), [prettier](#prettier))
7070
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
71+
- [JSON](#json)
7172
- Multiple languages
7273
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
7374
- javascript, jsx, angular, vue, flow, typescript, css, less, scss, html, json, graphql, markdown, ymaml
@@ -527,6 +528,21 @@ spotless {
527528
528529
For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.
529530
531+
## JSON
532+
533+
- `com.diffplug.gradle.spotless.JsonExtension` [javadoc](https://javadoc.io/static/com.diffplug.spotless/spotless-plugin-gradle/5.13.0/com/diffplug/gradle/spotless/JsonExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java)
534+
535+
```gradle
536+
spotless {
537+
json {
538+
target '**/*.json' // you have to set the target manually
539+
540+
indentWithSpaces(6)
541+
indentWithTabs()
542+
}
543+
}
544+
```
545+
530546
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
531547
532548
## Prettier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.gradle.spotless;
17+
18+
import javax.inject.Inject;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.JsonSimpleStep;
22+
23+
public class JsonExtension extends FormatExtension {
24+
private static final int DEFAULT_INDENTATION = 4;
25+
static final String NAME = "json";
26+
27+
@Inject
28+
public JsonExtension(SpotlessExtension spotless) {
29+
super(spotless);
30+
}
31+
32+
@Override
33+
protected void setupTask(SpotlessTask task) {
34+
if (target == null) {
35+
throw noDefaultTargetException();
36+
}
37+
super.setupTask(task);
38+
}
39+
40+
public SimpleConfig simple() {
41+
return new SimpleConfig(DEFAULT_INDENTATION);
42+
}
43+
44+
public class SimpleConfig {
45+
private int indent;
46+
47+
public SimpleConfig(int indent) {
48+
this.indent = indent;
49+
addStep(createStep());
50+
}
51+
52+
public void indentWithSpaces(int indent) {
53+
this.indent = indent;
54+
replaceStep(createStep());
55+
}
56+
57+
private FormatterStep createStep() {
58+
return JsonSimpleStep.create(indent, provisioner());
59+
}
60+
}
61+
62+
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 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.
@@ -169,6 +169,12 @@ public void python(Action<PythonExtension> closure) {
169169
format(PythonExtension.NAME, PythonExtension.class, closure);
170170
}
171171

172+
/** Configures the special JSON-specific extension. */
173+
public void json(Action<JsonExtension> closure) {
174+
requireNonNull(closure);
175+
format(JsonExtension.NAME, JsonExtension.class, closure);
176+
}
177+
172178
/** Configures a custom extension. */
173179
public void format(String name, Action<FormatExtension> closure) {
174180
requireNonNull(name, "name");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 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.gradle.spotless;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Test;
21+
22+
public class JsonExtensionTest extends GradleIntegrationHarness {
23+
@Test
24+
public void defaultFormatting() throws IOException {
25+
setFile("build.gradle").toLines(
26+
"buildscript { repositories { mavenCentral() } }",
27+
"plugins {",
28+
" id 'java'",
29+
" id 'com.diffplug.spotless'",
30+
"}",
31+
"spotless {",
32+
" json {",
33+
" target 'examples/**/*.json'",
34+
" simple()",
35+
"}",
36+
"}");
37+
setFile("src/main/resources/example.json").toResource("json/nestedObjectBefore.json");
38+
setFile("examples/main/resources/example.json").toResource("json/nestedObjectBefore.json");
39+
gradleRunner().withArguments("spotlessApply").build();
40+
assertFile("src/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json");
41+
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectAfter.json");
42+
}
43+
44+
@Test
45+
public void formattingWithCustomNumberOfSpaces() throws IOException {
46+
setFile("build.gradle").toLines(
47+
"buildscript { repositories { mavenCentral() } }",
48+
"plugins {",
49+
" id 'java'",
50+
" id 'com.diffplug.spotless'",
51+
"}",
52+
"spotless {",
53+
" json {",
54+
" target '**/*.json'",
55+
" simple().indentWithSpaces(6)",
56+
"}",
57+
"}");
58+
setFile("src/main/resources/example.json").toResource("json/singletonArrayBefore.json");
59+
gradleRunner().withArguments("spotlessApply").build();
60+
assertFile("src/main/resources/example.json").sameAsResource("json/singletonArrayAfter6Spaces.json");
61+
}
62+
}

0 commit comments

Comments
 (0)