Skip to content

Commit 271477f

Browse files
committed
Add Gradle config
1 parent 01c3c71 commit 271477f

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.spotless.json;
17+
18+
public class JsonDefaults {
19+
private static final String INCLUDES = "src/**/*.json";
20+
21+
private JsonDefaults() {
22+
// utility class, should not be instantiated
23+
}
24+
25+
public static String includes() {
26+
return INCLUDES;
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 java.util.*;
19+
20+
import javax.inject.Inject;
21+
22+
import com.diffplug.spotless.json.JsonDefaults;
23+
import com.diffplug.spotless.json.JsonFormatterStep;
24+
25+
public class JsonExtension extends FormatExtension {
26+
static final String NAME = "json";
27+
28+
@Inject
29+
public JsonExtension(SpotlessExtension spotless) {
30+
super(spotless);
31+
addStep(JsonFormatterStep.create(provisioner()));
32+
}
33+
34+
@Override
35+
protected void setupTask(SpotlessTask task) {
36+
if (target == null) {
37+
target = parseTarget(JsonDefaults.includes());
38+
}
39+
super.setupTask(task);
40+
}
41+
}

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,60 @@
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 defaultsToSrcJsonFiles() 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+
"}");
34+
setFile("src/main/resources/example.json").toResource("json/nestedObjectBefore.json");
35+
setFile("examples/main/resources/example.json").toResource("json/nestedObjectBefore.json");
36+
gradleRunner().withArguments("spotlessApply").build();
37+
assertFile("src/main/resources/example.json").sameAsResource("json/nestedObjectAfter.json");
38+
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json");
39+
}
40+
41+
@Test
42+
public void canOverrideTarget() throws IOException {
43+
setFile("build.gradle").toLines(
44+
"buildscript { repositories { mavenCentral() } }",
45+
"plugins {",
46+
" id 'java'",
47+
" id 'com.diffplug.spotless'",
48+
"}",
49+
"spotless {",
50+
" json {" +
51+
" target 'examples/**/*.json'" +
52+
"}",
53+
"}");
54+
setFile("src/main/resources/example.json").toResource("json/nestedObjectBefore.json");
55+
setFile("examples/main/resources/example.json").toResource("json/nestedObjectBefore.json");
56+
gradleRunner().withArguments("spotlessApply").build();
57+
assertFile("src/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json");
58+
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectAfter.json");
59+
}
60+
}

0 commit comments

Comments
 (0)