Skip to content

Commit

Permalink
Add Gradle bindings for JSON formatter
Browse files Browse the repository at this point in the history
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
jamietanna committed Apr 25, 2021
1 parent f787f9e commit a99b7f1
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 1 deletion.
28 changes: 28 additions & 0 deletions lib/src/main/java/com/diffplug/spotless/json/JsonDefaults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.spotless.json;

public class JsonDefaults {
private static final String INCLUDES = "src/**/*.json";

private JsonDefaults() {
// utility class, should not be instantiated
}

public static String includes() {
return INCLUDES;
}
}
20 changes: 20 additions & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,26 @@ See the [javadoc](https://javadoc.io/static/com.diffplug.spotless/spotless-plugi
<a name="retroactively-populating-year-range-from-git-history"></a>
## JSON Files
JSON files can be formatted for you, using the `json` formatter:
```gradle
spotless {
json {} // defaults to `src/**/*.json`
}
```
Or if you'd like custom paths:
```gradle
spotless {
json {
target 'src/test/**/*.json
}
}
```
### Retroactively slurp years from git history
If your project has not been rigorous with copyright headers, and you'd like to use git history to repair this retroactively, you can do so with `-PspotlessSetLicenseHeaderYearsFromGitHistory=true`. When run in this mode, Spotless will do an expensive search through git history for each file, and set the copyright header based on the oldest and youngest commits for that file. This is intended to be a one-off sort of thing.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 java.util.*;

import javax.inject.Inject;

import com.diffplug.spotless.json.JsonDefaults;
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) {
target = parseTarget(JsonDefaults.includes());
}
super.setupTask(task);
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -169,6 +169,12 @@ public void python(Action<PythonExtension> closure) {
format(PythonExtension.NAME, PythonExtension.class, closure);
}

/** Configures the special JSON-specific extension. */
public void json(Action<JsonExtension> closure) {
requireNonNull(closure);
format(JsonExtension.NAME, JsonExtension.class, closure);
}

/** Configures a custom extension. */
public void format(String name, Action<FormatExtension> closure) {
requireNonNull(name, "name");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 defaultsToSrcJsonFiles() throws IOException {
setFile("build.gradle").toLines(
"buildscript { repositories { mavenCentral() } }",
"plugins {",
" id 'java'",
" id 'com.diffplug.spotless'",
"}",
"spotless {",
" 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/nestedObjectAfter.json");
assertFile("examples/main/resources/example.json").sameAsResource("json/nestedObjectBefore.json");
}

@Test
public void canOverrideTarget() 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");
}
}

0 comments on commit a99b7f1

Please sign in to comment.