forked from diffplug/spotless
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Gradle bindings for JSON formatter
As we've created a JVM JSON formatter as part of diffplug#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.
- Loading branch information
1 parent
40b507c
commit f96f38b
Showing
3 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 javax.inject.Inject; | ||
|
||
import com.diffplug.spotless.json.JsonFormatterStep; | ||
|
||
public class JsonExtension extends FormatExtension { | ||
static final String NAME = "json"; | ||
|
||
@Inject | ||
public JsonExtension(SpotlessExtension spotless) { | ||
super(spotless); | ||
addStep(JsonFormatterStep.create(provisioner())); | ||
} | ||
|
||
@Override | ||
protected void setupTask(SpotlessTask task) { | ||
if (target == null) { | ||
throw noDefaultTargetException(); | ||
} | ||
super.setupTask(task); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JsonExtensionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 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.IOException; | ||
|
||
import org.junit.Test; | ||
|
||
public class JsonExtensionTest extends GradleIntegrationHarness { | ||
@Test | ||
public void formatsWhenTargetsAreSpecified() throws IOException { | ||
setFile("build.gradle").toLines( | ||
"buildscript { repositories { mavenCentral() } }", | ||
"plugins {", | ||
" id 'java'", | ||
" id 'com.diffplug.spotless'", | ||
"}", | ||
"spotless {", | ||
" json {" + | ||
" target 'examples/**/*.json'" + | ||
"}", | ||
"}"); | ||
setFile("src/main/resources/example.json").toResource("json/nestedObjectBefore.json"); | ||
setFile("examples/main/resources/example.json").toResource("json/nestedObjectBefore.json"); | ||
gradleRunner().withArguments("spotlessApply").build(); | ||
assertFile("src/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json"); | ||
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectAfter.json"); | ||
} | ||
} |