-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
222 additions
and
2 deletions.
There are no files selected for viewing
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
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
45 changes: 45 additions & 0 deletions
45
plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.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,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()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.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,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); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.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,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()); | ||
} | ||
} |
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
40 changes: 40 additions & 0 deletions
40
plugin-maven/src/test/java/com/diffplug/spotless/maven/json/JsonTest.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,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"); | ||
} | ||
} |