Skip to content

Commit 2dd2157

Browse files
matthiasbalkelutovich
authored andcommitted
add support for missing generic steps (closes #207) (#209)
* add support for missing generic steps (closes #207) add format section for custom formats * reformated code * fixed review findings * use assertFile instead of assertThat.isEqualTo for all tests * fixed review findings * fixed code duplication * updated changelog
1 parent 4d28488 commit 2dd2157

27 files changed

+707
-47
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ You might be looking for:
77

88
### Version 1.11.0-SNAPSHOT - TBD (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/snapshot/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/snapshot/), [snapshot repo](https://oss.sonatype.org/content/repositories/snapshots/com/diffplug/spotless/))
99

10+
* Added generic format support for maven-plugin ([#209](https://github.com/diffplug/spotless/pull/209))
11+
1012
### Version 1.10.0 - February 15th 2018 (javadoc [lib](https://diffplug.github.io/spotless/javadoc/spotless-lib/1.10.0/) [lib-extra](https://diffplug.github.io/spotless/javadoc/spotless-lib-extra/1.10.0/), artifact [lib]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib), [lib-extra]([jcenter](https://bintray.com/diffplug/opensource/spotless-lib-extra)))
1113

1214
* LicenseHeaderStep now supports customizing the year range separator in copyright notices. ([#199](https://github.com/diffplug/spotless/pull/199))

README.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ function extra(className) { return '| [`' + className + '`](lib-extra/src/main/j
3232
output = [
3333
'| Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) |',
3434
'| --------------------------------------------- | ------------- | ------------ | --------|',
35-
lib('generic.EndWithNewlineStep') +'{{yes}} | {{no}} | {{no}} |',
36-
lib('generic.IndentStep') +'{{yes}} | {{no}} | {{no}} |',
35+
lib('generic.EndWithNewlineStep') +'{{yes}} | {{yes}} | {{no}} |',
36+
lib('generic.IndentStep') +'{{yes}} | {{yes}} | {{no}} |',
3737
lib('generic.LicenseHeaderStep') +'{{yes}} | {{yes}} | {{no}} |',
38-
lib('generic.ReplaceRegexStep') +'{{yes}} | {{no}} | {{no}} |',
39-
lib('generic.ReplaceStep') +'{{yes}} | {{no}} | {{no}} |',
40-
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{no}} | {{no}} |',
38+
lib('generic.ReplaceRegexStep') +'{{yes}} | {{yes}} | {{no}} |',
39+
lib('generic.ReplaceStep') +'{{yes}} | {{yes}} | {{no}} |',
40+
lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}} | {{no}} |',
4141
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{no}} |',
4242
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{no}} |',
4343
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{no}} |',
@@ -55,12 +55,12 @@ lib('sql.DBeaverSQLFormatterStep') +'{{yes}} | {{no}}
5555
-->
5656
| Feature / FormatterStep | [plugin-gradle](plugin-gradle/README.md) | [plugin-maven](plugin-maven/README.md) | [(Your build tool here)](CONTRIBUTING.md#how-to-add-a-new-plugin-for-a-build-system) |
5757
| --------------------------------------------- | ------------- | ------------ | --------|
58-
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :white_large_square: | :white_large_square: |
59-
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :white_large_square: | :white_large_square: |
58+
| [`generic.EndWithNewlineStep`](lib/src/main/java/com/diffplug/spotless/generic/EndWithNewlineStep.java) | :+1: | :+1: | :white_large_square: |
59+
| [`generic.IndentStep`](lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java) | :+1: | :+1: | :white_large_square: |
6060
| [`generic.LicenseHeaderStep`](lib/src/main/java/com/diffplug/spotless/generic/LicenseHeaderStep.java) | :+1: | :+1: | :white_large_square: |
61-
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :white_large_square: | :white_large_square: |
62-
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
63-
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :white_large_square: | :white_large_square: |
61+
| [`generic.ReplaceRegexStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java) | :+1: | :+1: | :white_large_square: |
62+
| [`generic.ReplaceStep`](lib/src/main/java/com/diffplug/spotless/generic/ReplaceStep.java) | :+1: | :+1: | :white_large_square: |
63+
| [`generic.TrimTrailingWhitespaceStep`](lib/src/main/java/com/diffplug/spotless/generic/TrimTrailingWhitespaceStep.java) | :+1: | :+1: | :white_large_square: |
6464
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :white_large_square: |
6565
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :white_large_square: |
6666
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :white_large_square: |

lib/src/main/java/com/diffplug/spotless/generic/IndentStep.java

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323

2424
/** Simple step which checks for consistent indentation characters. */
2525
public final class IndentStep {
26+
27+
private static final int DEFAULT_NUM_SPACES_PER_TAB = 4;
28+
2629
// prevent direct instantiation
2730
private IndentStep() {}
2831

@@ -33,6 +36,11 @@ private <T> T tabSpace(T tab, T space) {
3336
return this == TAB ? tab : space;
3437
}
3538

39+
/** Creates a step which will indent with the given type of whitespace, converting between tabs and spaces at the default ratio. */
40+
public FormatterStep create() {
41+
return IndentStep.create(this, defaultNumSpacesPerTab());
42+
}
43+
3644
/** Synonym for {@link IndentStep#create(Type, int)}. */
3745
public FormatterStep create(int numSpacesPerTab) {
3846
return IndentStep.create(this, numSpacesPerTab);
@@ -128,4 +136,8 @@ String format(String raw) {
128136
private static boolean isSpaceOrTab(char c) {
129137
return c == ' ' || c == '\t';
130138
}
139+
140+
public static int defaultNumSpacesPerTab() {
141+
return DEFAULT_NUM_SPACES_PER_TAB;
142+
}
131143
}

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/FormatExtension.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public void indentWithSpaces(int numSpacesPerTab) {
332332

333333
/** Ensures that the files are indented using spaces. */
334334
public void indentWithSpaces() {
335-
indentWithSpaces(4);
335+
addStep(IndentStep.Type.SPACE.create());
336336
}
337337

338338
/** Ensures that the files are indented using tabs. */
@@ -342,7 +342,7 @@ public void indentWithTabs(int tabToSpaces) {
342342

343343
/** Ensures that the files are indented using tabs. */
344344
public void indentWithTabs() {
345-
indentWithTabs(4);
345+
addStep(IndentStep.Type.TAB.create());
346346
}
347347

348348
abstract class LicenseHeaderConfig {

plugin-maven/README.md

+54
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,60 @@ By default, all files matching `src/main/java/**/*.java` and `src/test/java/**/*
115115
</configuration>
116116
```
117117

118+
<a name="format"></a>
119+
120+
## Applying to custom sources
121+
122+
By default, no Ant-Style include patterns are defined. Each element under `<format>` is a step, and they will be applied in the order specified. Every step is optional, and they will be applied in the order specified.
123+
124+
```xml
125+
<configuration>
126+
<format>
127+
<includes>
128+
<!-- include all property files in "resource" folders under "src" -->
129+
<include>src/**/resources/**/*.properties</include>
130+
</includes>
131+
132+
<licenseHeader>
133+
<!-- Specify either content or file, but not both -->
134+
<content>/* Licensed under Apache-2.0 */</content>
135+
<file>${basedir}/license-header</file>
136+
<!-- conent until first occurrence of the delimiter regex will be interpreted as header section -->
137+
<delimiter>#</delimiter>
138+
</licenseHeader>
139+
140+
<!-- Files must end with a newline -->
141+
<endWithNewline />
142+
143+
<!-- Specify whether to use tabs or spaces for indentation -->
144+
<indent>
145+
<!-- Specify either spaces or tabs -->
146+
<spaces>true</spaces>
147+
<tabs>true</tabs>
148+
<!-- Specify how many spaces are used to convert one tab and vice versa. Defaults to 4 -->
149+
<spacesPerTab>4</spacesPerTab>
150+
</indent>
151+
152+
<!-- Trim trailing whitespaces -->
153+
<trimTrailingWhitespace />
154+
155+
<!-- Specify replacements using search and replace -->
156+
<replace>
157+
<name>Say Hello to Mars</name>
158+
<search>World</search>
159+
<replacement>Mars</replacement>
160+
</replace>
161+
162+
<!-- Specify replacements using regex match and replace -->
163+
<replaceRegex>
164+
<name>Say Hello to Mars from Regex</name>
165+
<searchRegex>(Hello) W[a-z]{3}d</searchRegex>
166+
<replacement>$1 Mars</replacement>
167+
</replaceRegex>
168+
</format>
169+
</configuration>
170+
```
171+
118172
<a name="invisible"></a>
119173

120174
## Line endings and encodings (invisible stuff)

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import org.apache.maven.plugin.AbstractMojo;
2929
import org.apache.maven.plugin.MojoExecutionException;
30-
import org.apache.maven.plugin.MojoFailureException;
3130
import org.apache.maven.plugins.annotations.Component;
3231
import org.apache.maven.plugins.annotations.Parameter;
3332
import org.codehaus.plexus.util.FileUtils;
@@ -38,6 +37,7 @@
3837
import com.diffplug.spotless.Formatter;
3938
import com.diffplug.spotless.LineEnding;
4039
import com.diffplug.spotless.Provisioner;
40+
import com.diffplug.spotless.maven.generic.Format;
4141
import com.diffplug.spotless.maven.generic.LicenseHeader;
4242
import com.diffplug.spotless.maven.java.Java;
4343
import com.diffplug.spotless.maven.scala.Scala;
@@ -71,6 +71,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
7171
@Parameter
7272
private LicenseHeader licenseHeader;
7373

74+
@Parameter
75+
private Format format;
76+
7477
@Parameter
7578
private Java java;
7679

@@ -80,7 +83,7 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
8083
protected abstract void process(List<File> files, Formatter formatter) throws MojoExecutionException;
8184

8285
@Override
83-
public final void execute() throws MojoExecutionException, MojoFailureException {
86+
public final void execute() throws MojoExecutionException {
8487
List<FormatterFactory> formatterFactories = getFormatterFactories();
8588

8689
for (FormatterFactory formatterFactory : formatterFactories) {
@@ -127,7 +130,7 @@ private FormatterConfig getFormatterConfig() {
127130
}
128131

129132
private List<FormatterFactory> getFormatterFactories() {
130-
return Stream.of(java, scala)
133+
return Stream.of(format, java, scala)
131134
.filter(Objects::nonNull)
132135
.collect(toList());
133136
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016 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 com.diffplug.spotless.FormatterStep;
19+
import com.diffplug.spotless.generic.EndWithNewlineStep;
20+
import com.diffplug.spotless.maven.FormatterStepConfig;
21+
import com.diffplug.spotless.maven.FormatterStepFactory;
22+
23+
public class EndWithNewline implements FormatterStepFactory {
24+
25+
@Override
26+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
27+
return EndWithNewlineStep.create();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016 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.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
public class Format extends FormatterFactory {
24+
25+
@Override
26+
public Set<String> defaultIncludes() {
27+
return Collections.emptySet();
28+
}
29+
30+
@Override
31+
public String licenseHeaderDelimiter() {
32+
// do not specify a default delimiter
33+
return null;
34+
}
35+
36+
public void addEndWithNewline(EndWithNewline endWithNewline) {
37+
addStepFactory(endWithNewline);
38+
}
39+
40+
public void addIndent(Indent indent) {
41+
addStepFactory(indent);
42+
}
43+
44+
public void addTrimTrailingWhitespace(TrimTrailingWhitespace trimTrailingWhitespace) {
45+
addStepFactory(trimTrailingWhitespace);
46+
}
47+
48+
public void addReplace(Replace replace) {
49+
addStepFactory(replace);
50+
}
51+
52+
public void addReplaceRegex(ReplaceRegex replaceRegex) {
53+
addStepFactory(replaceRegex);
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2016 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.IndentStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Indent implements FormatterStepFactory {
26+
27+
@Parameter
28+
private boolean tabs;
29+
30+
@Parameter
31+
private boolean spaces;
32+
33+
@Parameter
34+
private Integer spacesPerTab;
35+
36+
@Override
37+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38+
39+
if (spacesPerTab == null) {
40+
spacesPerTab = IndentStep.defaultNumSpacesPerTab();
41+
}
42+
43+
if (spaces ^ tabs) {
44+
if (spaces) {
45+
return IndentStep.create(IndentStep.Type.SPACE, spacesPerTab);
46+
} else {
47+
return IndentStep.create(IndentStep.Type.TAB, spacesPerTab);
48+
}
49+
} else {
50+
throw new IllegalArgumentException("Must specify exactly one of 'spaces' or 'tabs'.");
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2016 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.ReplaceStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Replace implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String name;
29+
30+
@Parameter
31+
private String search;
32+
33+
@Parameter
34+
private String replacement;
35+
36+
@Override
37+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
38+
if (name == null || search == null || replacement == null) {
39+
throw new IllegalArgumentException("Must specify 'name', 'search' and 'replacement'.");
40+
}
41+
42+
return ReplaceStep.create(name, search, replacement);
43+
}
44+
}

0 commit comments

Comments
 (0)