Skip to content

Commit aa5019e

Browse files
committed
Added support for custom JSR-223 based formatters
1 parent 85344bd commit aa5019e

File tree

10 files changed

+209
-6
lines changed

10 files changed

+209
-6
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Added
14+
* Added support for custom JSR223 formatters ([#945](https://github.com/diffplug/spotless/pull/945))
1315
### Changed
1416
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
1517

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ output = [
4646
'| Fast format on fresh checkout using buildcache | {{yes}} | {{no}} | {{no}} | {{no}} |',
4747
lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
4848
lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
49+
lib('generic.Jsr223Step') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
4950
lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
5051
lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
5152
lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -82,6 +83,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
8283
| Fast format on fresh checkout using buildcache | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
8384
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
8485
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
86+
| [`generic.Jsr223Step`](lib/src/main/java/com/diffplug/spotless/generic/Jsr223Step.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
8587
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
8688
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
8789
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.generic;
17+
18+
import java.io.Serializable;
19+
import java.util.Objects;
20+
21+
import javax.script.ScriptEngine;
22+
import javax.script.ScriptEngineManager;
23+
24+
import com.diffplug.spotless.FormatterFunc;
25+
import com.diffplug.spotless.FormatterStep;
26+
27+
public final class Jsr223Step {
28+
// prevent direct instantiation
29+
private Jsr223Step() {}
30+
31+
public static FormatterStep create(String name, CharSequence engine, CharSequence script) {
32+
Objects.requireNonNull(name, "name");
33+
Objects.requireNonNull(engine, "engine");
34+
Objects.requireNonNull(script, "script");
35+
return FormatterStep.createLazy(name,
36+
() -> new State(engine, script),
37+
State::toFormatter);
38+
}
39+
40+
private static final class State implements Serializable {
41+
private static final long serialVersionUID = 1L;
42+
43+
private final String engine;
44+
private final String script;
45+
46+
State(CharSequence engine, CharSequence script) {
47+
this.engine = engine.toString();
48+
this.script = script.toString();
49+
}
50+
51+
FormatterFunc toFormatter() {
52+
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
53+
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(engine);
54+
55+
// evaluate JavaScript code
56+
return raw -> {
57+
scriptEngine.put("source", raw);
58+
return (String) scriptEngine.eval(script);
59+
};
60+
}
61+
}
62+
}

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Added
7+
* Added support for custom JSR223 formatters ([#945](https://github.com/diffplug/spotless/pull/945))
68
### Changed
79
* Added support and bump Eclipse formatter default versions to `4.21` for `eclipse-cdt`, `eclipse-jdt`, `eclipse-wtp`. Change is only applied for JVM 11+.
810

plugin-maven/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,12 @@ to true.
767767
<spacesPerTab>4</spacesPerTab> <!-- optional, default is 4 -->
768768
</indent>
769769

770+
<jsr223> <!-- specify replacements using JSR223 scripting -->
771+
<name>Greetings to Mars</name>
772+
<engine>nashorn</engine> <!-- nashorn is provided by JDK 8-14, other engines can be used if they are on the classpath -->
773+
<script>source.replace('World','Mars');</script> <!-- the source variable contains the unformatted code, the returned value of the script is the formatted code -->
774+
</jsr223>
775+
770776
<replace> <!-- specify replacements using search and replace -->
771777
<name>Say Hello to Mars</name>
772778
<search>World</search>

plugin-maven/src/main/java/com/diffplug/spotless/maven/FormatterFactory.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -110,6 +110,10 @@ public final void addIndent(Indent indent) {
110110
addStepFactory(indent);
111111
}
112112

113+
public final void addJsr223(Jsr223 jsr223) {
114+
addStepFactory(jsr223);
115+
}
116+
113117
public final void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
114118
addStepFactory(trimTrailingWhitespace);
115119
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.generic;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.generic.Jsr223Step;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Jsr223 implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String name;
29+
30+
@Parameter
31+
private String engine;
32+
33+
@Parameter
34+
private String script;
35+
36+
@Override
37+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38+
if (name == null || engine == null || script == null) {
39+
throw new IllegalArgumentException("Must specify 'name', 'engine' and 'script'.");
40+
}
41+
42+
return Jsr223Step.create(name, engine, script);
43+
}
44+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

+18-5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class MavenIntegrationHarness extends ResourceHarness {
5757
private static final String CONFIGURATION = "configuration";
5858
private static final String EXECUTIONS = "executions";
5959
private static final String MODULES = "modules";
60+
private static final String ADDITIONAL_PLUGIN_DEPENDENCIES = "additionalPluginDependencies";
6061
private static final String MODULE_NAME = "name";
6162
private static final String CHILD_ID = "childId";
6263

@@ -98,6 +99,10 @@ protected void writePomWithFormatSteps(String... steps) throws IOException {
9899
writePom(formats(groupWithSteps("format", including("<include>src/**/java/**/*.java</include>"), steps)));
99100
}
100101

102+
protected void writePomWithAdditionalPluginDependenciesAndFormatSteps(String[] additionalPluginDependencies, String... steps) throws IOException {
103+
writePom(null, formats(groupWithSteps("format", including("<include>src/**/java/**/*.java</include>"), steps)), additionalPluginDependencies);
104+
}
105+
101106
protected void writePomWithAntlr4Steps(String... steps) throws IOException {
102107
writePom(groupWithSteps("antlr4", steps));
103108
}
@@ -139,7 +144,11 @@ protected void writePom(String... configuration) throws IOException {
139144
}
140145

141146
protected void writePom(String[] executions, String[] configuration) throws IOException {
142-
String pomXmlContent = createPomXmlContent(executions, configuration);
147+
writePom(executions, configuration, null);
148+
}
149+
150+
protected void writePom(String[] executions, String[] configuration, String[] additionalPluginDependencies) throws IOException {
151+
String pomXmlContent = createPomXmlContent(executions, configuration, additionalPluginDependencies);
143152
setFile("pom.xml").toContent(pomXmlContent);
144153
}
145154

@@ -153,8 +162,8 @@ protected MultiModuleProjectCreator multiModuleProject() {
153162
return new MultiModuleProjectCreator();
154163
}
155164

156-
private String createPomXmlContent(String[] executions, String[] configuration) throws IOException {
157-
Map<String, Object> params = buildPomXmlParams(executions, configuration, null);
165+
private String createPomXmlContent(String[] executions, String[] configuration, String[] additionalPluginDependencies) throws IOException {
166+
Map<String, Object> params = buildPomXmlParams(executions, configuration, null, additionalPluginDependencies);
158167
return createPomXmlContent("/pom-test.xml.mustache", params);
159168
}
160169

@@ -168,7 +177,7 @@ private String createPomXmlContent(String pomTemplate, Map<String, Object> param
168177
}
169178
}
170179

171-
private static Map<String, Object> buildPomXmlParams(String[] executions, String[] configuration, String[] modules) {
180+
private static Map<String, Object> buildPomXmlParams(String[] executions, String[] configuration, String[] modules, String[] additionalPluginDependencies) {
172181
Map<String, Object> params = new HashMap<>();
173182
params.put(SPOTLESS_MAVEN_PLUGIN_VERSION, getSystemProperty(SPOTLESS_MAVEN_PLUGIN_VERSION));
174183

@@ -185,6 +194,10 @@ private static Map<String, Object> buildPomXmlParams(String[] executions, String
185194
params.put(MODULES, moduleNames);
186195
}
187196

197+
if (additionalPluginDependencies != null) {
198+
params.put(ADDITIONAL_PLUGIN_DEPENDENCIES, String.join("\n", additionalPluginDependencies));
199+
}
200+
188201
return params;
189202
}
190203

@@ -261,7 +274,7 @@ private void createRootPom() throws IOException {
261274
modulesList.addAll(subProjects.keySet());
262275
String[] modules = modulesList.toArray(new String[0]);
263276

264-
Map<String, Object> rootPomParams = buildPomXmlParams(null, configuration, modules);
277+
Map<String, Object> rootPomParams = buildPomXmlParams(null, configuration, modules, null);
265278
setFile("pom.xml").toContent(createPomXmlContent("/multi-module/pom-parent.xml.mustache", rootPomParams));
266279
}
267280

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.generic;
17+
18+
import static org.assertj.core.api.Assumptions.assumeThat;
19+
20+
import javax.script.ScriptEngineManager;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
25+
26+
public class Jsr223Test extends MavenIntegrationHarness {
27+
28+
@Test
29+
public void buildInNashorn() throws Exception {
30+
// This will only work for JDKs that bundle nashorn (8-14)
31+
assumeThat(new ScriptEngineManager().getEngineByName("nashorn")).isNotNull();
32+
writePomWithFormatSteps(
33+
"<jsr223>",
34+
" <name>Greetings to Mars</name>",
35+
" <engine>nashorn</engine>",
36+
" <script>source.replace('World','Mars');</script>",
37+
"</jsr223>");
38+
runTest("Hello World", "Hello Mars");
39+
}
40+
41+
@Test
42+
public void groovyFromPluginDependency() throws Exception {
43+
writePomWithAdditionalPluginDependenciesAndFormatSteps(
44+
new String[]{
45+
"<dependency>",
46+
" <groupId>org.codehaus.groovy</groupId>",
47+
" <artifactId>groovy-jsr223</artifactId>",
48+
" <version>3.0.9</version>",
49+
"</dependency>",
50+
},
51+
"<jsr223>",
52+
" <name>Greetings to Mars</name>",
53+
" <engine>groovy</engine>",
54+
" <script>source.replace('World','Mars')</script>",
55+
"</jsr223>");
56+
runTest("Hello World", "Hello Mars");
57+
}
58+
59+
private void runTest(String sourceContent, String targetContent) throws Exception {
60+
String path = "src/main/java/test.java";
61+
setFile(path).toContent(sourceContent);
62+
mavenRunner().withArguments("spotless:apply").runNoError();
63+
assertFile(path).hasContent(targetContent);
64+
}
65+
}

plugin-maven/src/test/resources/pom-test.xml.mustache

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
<executions>
3232
{{{executions}}}
3333
</executions>
34+
<dependencies>
35+
{{{additionalPluginDependencies}}}
36+
</dependencies>
3437
</plugin>
3538
</plugins>
3639
</build>

0 commit comments

Comments
 (0)