Skip to content

Commit b6ea481

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

File tree

8 files changed

+195
-1
lines changed

8 files changed

+195
-1
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,71 @@
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+
import com.diffplug.spotless.JarState;
27+
import com.diffplug.spotless.Provisioner;
28+
29+
public final class Jsr223Step {
30+
// prevent direct instantiation
31+
private Jsr223Step() {}
32+
33+
public static FormatterStep create(String name, String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) {
34+
Objects.requireNonNull(name, "name");
35+
Objects.requireNonNull(engine, "engine");
36+
Objects.requireNonNull(script, "script");
37+
return FormatterStep.createLazy(name,
38+
() -> new State(dependency == null ? null : JarState.from(dependency, provisioner), engine, script),
39+
State::toFormatter);
40+
}
41+
42+
private static final class State implements Serializable {
43+
private static final long serialVersionUID = 1L;
44+
45+
private final JarState jarState;
46+
private final String engine;
47+
private final String script;
48+
49+
State(JarState jarState, CharSequence engine, CharSequence script) {
50+
this.jarState = jarState;
51+
this.engine = engine.toString();
52+
this.script = script.toString();
53+
}
54+
55+
FormatterFunc toFormatter() {
56+
ScriptEngineManager scriptEngineManager;
57+
if (jarState == null) {
58+
scriptEngineManager = new ScriptEngineManager();
59+
} else {
60+
scriptEngineManager = new ScriptEngineManager(jarState.getClassLoader());
61+
}
62+
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(engine);
63+
64+
// evaluate JavaScript code
65+
return raw -> {
66+
scriptEngine.put("source", raw);
67+
return (String) scriptEngine.eval(script);
68+
};
69+
}
70+
}
71+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,13 @@ 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+
<dependency>org.codehaus.groovy:groovy-jsr223:3.0.9</dependency> <!-- optional, maven dependency, containing the jsr223 compatible scripting engine-->
773+
<engine>groovy</engine> <!-- nashorn is provided by JDK 8-14, other engines can be loaded from the given dependency -->
774+
<script>source.replace('World','Mars');</script> <!-- the source variable contains the unformatted code, the returned value of the script is the formatted code -->
775+
</jsr223>
776+
770777
<replace> <!-- specify replacements using search and replace -->
771778
<name>Say Hello to Mars</name>
772779
<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,47 @@
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 dependency;
32+
33+
@Parameter
34+
private String engine;
35+
36+
@Parameter
37+
private String script;
38+
39+
@Override
40+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
41+
if (name == null || engine == null || script == null) {
42+
throw new IllegalArgumentException("Must specify 'name', 'engine' and 'script'.");
43+
}
44+
45+
return Jsr223Step.create(name, dependency, engine, script, config.getProvisioner());
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 groovyFromJarState() throws Exception {
43+
writePomWithFormatSteps(
44+
"<jsr223>",
45+
" <name>Greetings to Mars</name>",
46+
" <dependency>org.codehaus.groovy:groovy-jsr223:3.0.9</dependency>",
47+
" <engine>groovy</engine>",
48+
" <script>source.replace('World','Mars')</script>",
49+
"</jsr223>");
50+
runTest("Hello World", "Hello Mars");
51+
}
52+
53+
private void runTest(String sourceContent, String targetContent) throws Exception {
54+
String path = "src/main/java/test.java";
55+
setFile(path).toContent(sourceContent);
56+
mavenRunner().withArguments("spotless:apply").runNoError();
57+
assertFile(path).hasContent(targetContent);
58+
}
59+
}

0 commit comments

Comments
 (0)