diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java new file mode 100644 index 0000000000..37e4dbab19 --- /dev/null +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java @@ -0,0 +1,39 @@ +/* + * 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 { + private static final Integer DEFAULT_INDENTATION = 4; + static final String NAME = "json"; + + @Inject + public JsonExtension(SpotlessExtension spotless) { + super(spotless); + addStep(JsonFormatterStep.create(DEFAULT_INDENTATION, provisioner())); + } + + @Override + protected void setupTask(SpotlessTask task) { + if (target == null) { + throw noDefaultTargetException(); + } + super.setupTask(task); + } +} diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java index 1f55efc1ee..3e56dda443 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/SpotlessExtension.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2020 DiffPlug + * 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. @@ -169,6 +169,12 @@ public void python(Action closure) { format(PythonExtension.NAME, PythonExtension.class, closure); } + /** Configures the special JSON-specific extension. */ + public void json(Action closure) { + requireNonNull(closure); + format(JsonExtension.NAME, JsonExtension.class, closure); + } + /** Configures a custom extension. */ public void format(String name, Action closure) { requireNonNull(name, "name"); diff --git a/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JsonExtensionTest.java b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JsonExtensionTest.java new file mode 100644 index 0000000000..74b5f82b18 --- /dev/null +++ b/plugin-gradle/src/test/java/com/diffplug/gradle/spotless/JsonExtensionTest.java @@ -0,0 +1,80 @@ +/* + * 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 defaultFormatting() 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"); + } + + @Test + public void formattingWithCustomNumberOfSpaces() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'java'", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " json {" + + " indentWithSpaces(6)" + + " target '**/*.json'" + + "}", + "}"); + setFile("src/main/resources/example.json").toResource("json/singletonArrayBefore.json"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/resources/example.json").sameAsResource("json/singletonArrayAfter6Spaces.json"); + } + + @Test + public void formattingWithTabs() throws IOException { + setFile("build.gradle").toLines( + "buildscript { repositories { mavenCentral() } }", + "plugins {", + " id 'java'", + " id 'com.diffplug.spotless'", + "}", + "spotless {", + " json {" + + " indentWithTabs()" + + " target '**/*.json'" + + "}", + "}"); + setFile("src/main/resources/example.json").toResource("json/singletonArrayBefore.json"); + gradleRunner().withArguments("spotlessApply").build(); + assertFile("src/main/resources/example.json").sameAsResource("json/singletonArrayAfterTabs.json"); + } +}