Skip to content

Commit 8604dd4

Browse files
committed
Added support for custom JSR-223 based formatters
1 parent 635a636 commit 8604dd4

File tree

8 files changed

+200
-0
lines changed

8 files changed

+200
-0
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

1416
## [2.17.0] - 2021-09-27
1517
### Added

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.NativeCmdStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
5152
lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -83,6 +84,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
8384
| Fast format on fresh checkout using buildcache | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
8485
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
8586
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
87+
| [`generic.Jsr223Step`](lib/src/main/java/com/diffplug/spotless/generic/Jsr223Step.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
8688
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
8789
| [`generic.NativeCmdStep`](lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java) | :white_large_square: | :+1: | :white_large_square: | :white_large_square: |
8890
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
import java.util.stream.Collectors;
21+
22+
import javax.script.ScriptEngine;
23+
import javax.script.ScriptEngineManager;
24+
25+
import com.diffplug.spotless.FormatterFunc;
26+
import com.diffplug.spotless.FormatterStep;
27+
import com.diffplug.spotless.JarState;
28+
import com.diffplug.spotless.Provisioner;
29+
30+
public final class Jsr223Step {
31+
// prevent direct instantiation
32+
private Jsr223Step() {}
33+
34+
public static FormatterStep create(String name, String dependency, CharSequence engine, CharSequence script, Provisioner provisioner) {
35+
Objects.requireNonNull(name, "name");
36+
Objects.requireNonNull(engine, "engine");
37+
Objects.requireNonNull(script, "script");
38+
return FormatterStep.createLazy(name,
39+
() -> new State(dependency == null ? null : JarState.from(dependency, provisioner), engine, script),
40+
State::toFormatter);
41+
}
42+
43+
private static final class State implements Serializable {
44+
private static final long serialVersionUID = 1L;
45+
46+
private final JarState jarState;
47+
private final String engine;
48+
private final String script;
49+
50+
State(JarState jarState, CharSequence engine, CharSequence script) {
51+
this.jarState = jarState;
52+
this.engine = engine.toString();
53+
this.script = script.toString();
54+
}
55+
56+
FormatterFunc toFormatter() {
57+
ScriptEngineManager scriptEngineManager;
58+
if (jarState == null) {
59+
scriptEngineManager = new ScriptEngineManager(ClassLoader.getSystemClassLoader());
60+
} else {
61+
scriptEngineManager = new ScriptEngineManager(jarState.getClassLoader());
62+
}
63+
ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(engine);
64+
65+
if (scriptEngine == null) {
66+
throw new IllegalArgumentException("Unknown script engine '" + engine + "'. Available engines: " +
67+
scriptEngineManager.getEngineFactories().stream().flatMap(f -> f.getNames().stream()).collect(Collectors.joining(", ")));
68+
}
69+
70+
// evaluate script code
71+
return raw -> {
72+
scriptEngine.put("source", raw);
73+
return (String) scriptEngine.eval(script);
74+
};
75+
}
76+
}
77+
}

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

79
## [2.14.0] - 2021-09-27
810
### Added

plugin-maven/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,13 @@ to true.
769769
<spacesPerTab>4</spacesPerTab> <!-- optional, default is 4 -->
770770
</indent>
771771

772+
<jsr223> <!-- specify replacements using JSR223 scripting -->
773+
<name>Greetings to Mars</name>
774+
<dependency>org.codehaus.groovy:groovy-jsr223:3.0.9</dependency> <!-- optional, maven dependency, containing the jsr223 compatible scripting engine-->
775+
<engine>groovy</engine> <!-- nashorn is provided by JDK 8-14, other engines can be loaded from the given dependency -->
776+
<script>source.replace('World','Mars');</script> <!-- the source variable contains the unformatted code, the returned value of the script is the formatted code -->
777+
</jsr223>
778+
772779
<nativeCmd> <!-- run a native binary -->
773780
<name>Greetings to Mars from sed</name>
774781
<pathToExe>/usr/bin/sed</pathToExe> <!-- path to the binary, unformatted code is send via StdIn, formatted code is expected on StdOut -->

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

+4
Original file line numberDiff line numberDiff line change
@@ -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)