Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable json in maven plugin #1446

Merged
merged 6 commits into from Jan 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Expand Up @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [2.29.0] - 2023-01-02
### Added
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446))
### Fixed
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))
Expand Down
50 changes: 49 additions & 1 deletion plugin-maven/README.md
Expand Up @@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
- [Markdown](#markdown) ([flexmark](#flexmark))
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
- [JSON](#json)
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
- [eclipse web tools platform](#eclipse-web-tools-platform)
Expand Down Expand Up @@ -431,7 +432,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T
<configuration>
<cpp>
<includes> <!-- You have to set the target manually -->
<include>src/native/**</inclue>
<include>src/native/**</include>
</includes>

<eclipseCdt /> <!-- has its own section below -->
Expand Down Expand Up @@ -700,6 +701,53 @@ The auto-discovery of config files (up the file tree) will not work when using t

For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.

## JSON

- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/json.java)

```xml
<configuration>
<json>
<includes> <!-- You have to set the target manually -->
<include>src/**/*.json</include>
</includes>

<simple /> <!-- has its own section below -->
<gson /> <!-- has its own section below -->
</json>
</configuration>
```

### simple

Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:

```xml
<simple>
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
</simple>
```

### Gson

Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files.

```xml
<gson>
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
<sortByKeys>false</sortByKeys> <!-- optional: sort JSON by its keys -->
<escapeHtml>false</indentSpaces> <!-- optional: escape HTML in values -->
<version>2.8.1</version> <!-- optional: specify version -->
</gson>
```

Notes:
* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either
all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former.
* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the
[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String))
for details.

<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>

## Prettier
Expand Down
@@ -0,0 +1,45 @@
/*
* Copyright 2023 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.maven.json;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.json.gson.GsonStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Gson implements FormatterStepFactory {
private static final String DEFAULT_GSON_VERSION = "2.8.9";

@Parameter
int indentSpaces = Json.DEFAULT_INDENTATION;

@Parameter
boolean sortByKeys = false;

@Parameter
boolean escapeHtml = false;

@Parameter
String version = DEFAULT_GSON_VERSION;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
int indentSpaces = this.indentSpaces;
return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner());
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2023 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.maven.json;

import java.util.Collections;
import java.util.Set;

import com.diffplug.spotless.maven.FormatterFactory;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <json>...</json>} configuration element.
*/
public class Json extends FormatterFactory {
public static final int DEFAULT_INDENTATION = 4;

@Override
public Set<String> defaultIncludes() {
return Collections.emptySet();
}

@Override
public String licenseHeaderDelimiter() {
return null;
}

public void addSimple(Simple simple) {
addStepFactory(simple);
}

public void addGson(Gson gson) {
addStepFactory(gson);
}

}
@@ -0,0 +1,35 @@
/*
* Copyright 2023 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.maven.json;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.json.JsonSimpleStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class Simple implements FormatterStepFactory {

@Parameter
int indentSpaces = Json.DEFAULT_INDENTATION;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
int indentSpaces = this.indentSpaces;
return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner());
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 DiffPlug
* Copyright 2016-2023 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 @@ -156,6 +156,10 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException {
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
}

protected void writePomWithJsonSteps(String... steps) throws IOException {
writePom(groupWithSteps("json", including("**/*.json"), steps));
}

protected void writePom(String... configuration) throws IOException {
writePom(null, configuration, null);
}
Expand Down
@@ -0,0 +1,40 @@
/*
* Copyright 2023 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.maven.json;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

public class JsonTest extends MavenIntegrationHarness {
@Test
public void testFormatJson_WithSimple_defaultConfig() throws Exception {
writePomWithJsonSteps("<json><simple/></json>");

setFile("json_test.json").toResource("json/sortByKeysBefore.json");
mavenRunner().withArguments("spotless:apply").runNoError().error();
assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json");
}

@Test
public void testFormatJson_WithGson_defaultConfig() throws Exception {
writePomWithJsonSteps("<json><gson/></json>");

setFile("json_test.json").toResource("json/sortByKeysBefore.json");
mavenRunner().withArguments("spotless:apply").runNoError().error();
assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json");
}
}