Skip to content

Commit e32fcbd

Browse files
committed
Added support for local binary formatters
1 parent 4e30602 commit e32fcbd

File tree

8 files changed

+181
-0
lines changed

8 files changed

+181
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
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 calling local binary formatters ([#949](https://github.com/diffplug/spotless/pull/949))
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
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ output = [
4747
lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
4848
lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
4949
lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
50+
lib('generic.NativeCmdStep') +'{{no}} | {{yes}} | {{no}} | {{no}} |',
5051
lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
5152
lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
5253
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
@@ -83,6 +84,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
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: |
8586
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
87+
| [`generic.NativeCmdStep`](lib/src/main/java/com/diffplug/spotless/generic/NativeCmdStep.java) | :white_large_square: | :+1: | :white_large_square: | :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: |
8890
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.File;
19+
import java.io.IOException;
20+
import java.io.Serializable;
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
import java.util.Objects;
25+
26+
import com.diffplug.spotless.FileSignature;
27+
import com.diffplug.spotless.FormatterFunc;
28+
import com.diffplug.spotless.FormatterStep;
29+
import com.diffplug.spotless.ProcessRunner;
30+
31+
public class NativeCmdStep {
32+
// prevent direct instantiation
33+
private NativeCmdStep() {}
34+
35+
public static FormatterStep create(String name, File pathToExe, List<String> arguments) {
36+
Objects.requireNonNull(name, "name");
37+
Objects.requireNonNull(pathToExe, "pathToExe");
38+
List<String> argumentsWithPathToExe = new ArrayList<>();
39+
argumentsWithPathToExe.add(pathToExe.getAbsolutePath());
40+
if (arguments != null) {
41+
argumentsWithPathToExe.addAll(arguments);
42+
}
43+
return FormatterStep.createLazy(name, () -> new State(FileSignature.signAsList(pathToExe), argumentsWithPathToExe), State::toFunc);
44+
}
45+
46+
static class State implements Serializable {
47+
private static final long serialVersionUID = 1L;
48+
49+
final FileSignature pathToExe;
50+
51+
final List<String> arguments;
52+
53+
State(FileSignature pathToExe, List<String> arguments) {
54+
this.pathToExe = pathToExe;
55+
this.arguments = arguments;
56+
}
57+
58+
String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
59+
return runner.exec(input.getBytes(StandardCharsets.UTF_8), arguments).assertExitZero(StandardCharsets.UTF_8);
60+
}
61+
62+
FormatterFunc.Closeable toFunc() {
63+
ProcessRunner runner = new ProcessRunner();
64+
return FormatterFunc.Closeable.of(runner, this::format);
65+
}
66+
}
67+
}

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
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 calling local binary formatters ([#949](https://github.com/diffplug/spotless/pull/949))
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
* Added `groupArtifact` option for `google-java-format` ([#944](https://github.com/diffplug/spotless/pull/944))

plugin-maven/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,14 @@ to true.
769769
<spacesPerTab>4</spacesPerTab> <!-- optional, default is 4 -->
770770
</indent>
771771

772+
<nativeCmd> <!-- run a native binary -->
773+
<name>Greetings to Mars from sed</name>
774+
<pathToExe>/usr/bin/sed</pathToExe> <!-- path to the binary, unformatted code is send via StdIn, formatted code is expected on StdOut -->
775+
<arguments> <!-- optional, list with arguments for the binary call-->
776+
<argument>s/World/Mars/g</argument>
777+
</arguments>
778+
</nativeCmd>
779+
772780
<replace> <!-- specify replacements using search and replace -->
773781
<name>Say Hello to Mars</name>
774782
<search>World</search>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ public final void addReplace(Replace replace) {
118118
addStepFactory(replace);
119119
}
120120

121+
public final void addNativeCmd(NativeCmd nativeCmd) {
122+
addStepFactory(nativeCmd);
123+
}
124+
121125
public final void addReplaceRegex(ReplaceRegex replaceRegex) {
122126
addStepFactory(replaceRegex);
123127
}
Lines changed: 47 additions & 0 deletions
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 java.io.File;
19+
import java.util.List;
20+
21+
import org.apache.maven.plugins.annotations.Parameter;
22+
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.generic.NativeCmdStep;
25+
import com.diffplug.spotless.maven.FormatterStepConfig;
26+
import com.diffplug.spotless.maven.FormatterStepFactory;
27+
28+
public class NativeCmd implements FormatterStepFactory {
29+
30+
@Parameter
31+
private String name;
32+
33+
@Parameter
34+
private File pathToExe;
35+
36+
@Parameter
37+
private List<String> arguments;
38+
39+
@Override
40+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
41+
if (name == null || pathToExe == null) {
42+
throw new IllegalArgumentException("Must specify 'name' and 'pathToExe'.");
43+
}
44+
45+
return NativeCmdStep.create(name, pathToExe, arguments);
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 java.io.File;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
25+
26+
public class NativeCmdTest extends MavenIntegrationHarness {
27+
28+
@Test
29+
public void fromStdInToStdOut() throws Exception {
30+
// This will only work if /usr/bin/sed is available
31+
assumeThat(new File("/usr/bin/sed")).exists();
32+
writePomWithFormatSteps(
33+
"<nativeCmd>",
34+
" <name>Greetings to Mars</name>",
35+
" <pathToExe>/usr/bin/sed</pathToExe>",
36+
" <arguments>",
37+
" <argument>s/World/Mars/g</argument>",
38+
" </arguments>",
39+
"</nativeCmd>");
40+
runTest("Hello World", "Hello Mars");
41+
}
42+
43+
private void runTest(String sourceContent, String targetContent) throws Exception {
44+
String path = "src/main/java/test.java";
45+
setFile(path).toContent(sourceContent);
46+
mavenRunner().withArguments("spotless:apply").runNoError();
47+
assertFile(path).hasContent(targetContent);
48+
}
49+
}

0 commit comments

Comments
 (0)