Skip to content

Commit 339f7c4

Browse files
committed
Added support for custom JSR-223 based formatters
1 parent 738f8ad commit 339f7c4

File tree

7 files changed

+161
-1
lines changed

7 files changed

+161
-1
lines changed

README.md

+1
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}} |',
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

79
## [2.13.0] - 2021-09-04
810
### Added

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>
773+
<script>source.replace('World','Mars');</script>
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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.junit.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
public class Jsr223Test extends MavenIntegrationHarness {
23+
24+
@Test
25+
public void fromContent() throws Exception {
26+
writePomWithFormatSteps(
27+
"<jsr223>",
28+
" <name>Greetings to Mars</name>",
29+
" <engine>nashorn</engine>",
30+
" <script>source.replace('World','Mars');</script>",
31+
"</jsr223>");
32+
runTest("Hello World", "Hello Mars");
33+
}
34+
35+
private void runTest(String sourceContent, String targetContent) throws Exception {
36+
String path = "src/main/java/test.java";
37+
setFile(path).toContent(sourceContent);
38+
mavenRunner().withArguments("spotless:apply").runNoError();
39+
assertFile(path).hasContent(targetContent);
40+
}
41+
}

0 commit comments

Comments
 (0)