Skip to content

Commit af664dc

Browse files
committed
Added support for generic NativeCmd step
1 parent 738f8ad commit af664dc

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
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.generic;
17+
18+
import java.io.IOException;
19+
import java.io.Serializable;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
import java.util.Objects;
24+
25+
import com.diffplug.spotless.FormatterFunc;
26+
import com.diffplug.spotless.FormatterStep;
27+
import com.diffplug.spotless.ProcessRunner;
28+
29+
public class NativeCmdStep {
30+
// prevent direct instantiation
31+
private NativeCmdStep() {}
32+
33+
public static FormatterStep create(String name, String pathToExe, List<String> arguments) {
34+
Objects.requireNonNull(name, "name");
35+
Objects.requireNonNull(pathToExe, "pathToExe");
36+
List<String> argumentsWithPathToExe = new ArrayList<>();
37+
argumentsWithPathToExe.add(pathToExe);
38+
if (arguments != null) {
39+
argumentsWithPathToExe.addAll(arguments);
40+
}
41+
return FormatterStep.createLazy(name, () -> new State(pathToExe, argumentsWithPathToExe), State::toFunc);
42+
}
43+
44+
static class State implements Serializable {
45+
private static final long serialVersionUID = 1L;
46+
47+
final String pathToExe;
48+
49+
final List<String> arguments;
50+
51+
State(String pathToExe, List<String> arguments) {
52+
this.pathToExe = pathToExe;
53+
this.arguments = arguments;
54+
}
55+
56+
String format(ProcessRunner runner, String input) throws IOException, InterruptedException {
57+
return runner.exec(input.getBytes(StandardCharsets.UTF_8), arguments).assertExitZero(StandardCharsets.UTF_8);
58+
}
59+
60+
FormatterFunc.Closeable toFunc() {
61+
ProcessRunner runner = new ProcessRunner();
62+
return FormatterFunc.Closeable.of(runner, this::format);
63+
}
64+
}
65+
}

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

+4
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
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.util.List;
19+
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
22+
import com.diffplug.spotless.FormatterStep;
23+
import com.diffplug.spotless.generic.NativeCmdStep;
24+
import com.diffplug.spotless.maven.FormatterStepConfig;
25+
import com.diffplug.spotless.maven.FormatterStepFactory;
26+
27+
public class NativeCmd implements FormatterStepFactory {
28+
29+
@Parameter
30+
private String name;
31+
32+
@Parameter
33+
private String pathToExe;
34+
35+
@Parameter
36+
private List<String> arguments;
37+
38+
@Override
39+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
40+
if (name == null || pathToExe == null) {
41+
throw new IllegalArgumentException("Must specify 'name' and 'pathToExe'.");
42+
}
43+
44+
return NativeCmdStep.create(name, pathToExe, arguments);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 NativeCmdTest extends MavenIntegrationHarness {
23+
24+
@Test
25+
public void fromStdOutToStdOut() throws Exception {
26+
writePomWithFormatSteps(
27+
"<nativeCmd>",
28+
" <name>Greetings to Mars</name>",
29+
" <pathToExe>sed</pathToExe>",
30+
" <arguments>" +
31+
" <argument>s/World/Mars/g</argument>" +
32+
" </arguments>",
33+
"</nativeCmd>");
34+
runTest("Hello World", "Hello Mars");
35+
}
36+
37+
private void runTest(String sourceContent, String targetContent) throws Exception {
38+
String path = "src/main/java/test.java";
39+
setFile(path).toContent(sourceContent);
40+
mavenRunner().withArguments("spotless:apply").runNoError();
41+
assertFile(path).hasContent(targetContent);
42+
}
43+
}

0 commit comments

Comments
 (0)