Skip to content

Commit f91067e

Browse files
committed
Add groovy formatter to maven
1 parent d033a87 commit f91067e

File tree

9 files changed

+282
-1
lines changed

9 files changed

+282
-1
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import java.util.stream.Collectors;
3232
import java.util.stream.Stream;
3333

34+
import com.diffplug.spotless.maven.groovy.Groovy;
35+
3436
import org.apache.maven.plugin.AbstractMojo;
3537
import org.apache.maven.plugin.MojoExecutionException;
3638
import org.apache.maven.plugins.annotations.Component;
@@ -94,6 +96,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
9496
@Parameter
9597
private List<Format> formats = Collections.emptyList();
9698

99+
@Parameter
100+
private Groovy groovy;
101+
97102
@Parameter
98103
private Java java;
99104

@@ -201,7 +206,7 @@ private FileLocator getFileLocator() {
201206
}
202207

203208
private List<FormatterFactory> getFormatterFactories() {
204-
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, antlr4))
209+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4))
205210
.filter(Objects::nonNull)
206211
.collect(toList());
207212
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2020 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.groovy;
17+
18+
import java.io.File;
19+
import java.util.Arrays;
20+
21+
import org.apache.maven.plugins.annotations.Parameter;
22+
23+
import com.diffplug.spotless.FormatterStep;
24+
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
25+
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
26+
import com.diffplug.spotless.maven.FormatterStepConfig;
27+
import com.diffplug.spotless.maven.FormatterStepFactory;
28+
29+
public class Eclipse implements FormatterStepFactory {
30+
31+
@Parameter
32+
private String file;
33+
34+
@Parameter
35+
private String version;
36+
37+
@Override
38+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
39+
EclipseBasedStepBuilder eclipseConfig = GrEclipseFormatterStep.createBuilder(stepConfig.getProvisioner());
40+
eclipseConfig.setVersion(version == null ? GrEclipseFormatterStep.defaultVersion() : version);
41+
if (null != file) {
42+
File settingsFile = stepConfig.getFileLocator().locateFile(file);
43+
eclipseConfig.setPreferences(Arrays.asList(settingsFile));
44+
}
45+
return eclipseConfig.build();
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 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.groovy;
17+
18+
import java.util.Set;
19+
20+
import com.diffplug.common.collect.ImmutableSet;
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
import com.diffplug.spotless.maven.generic.LicenseHeader;
23+
24+
/**
25+
* A {@link FormatterFactory} implementation that corresponds to {@code <groovy>...</groovy>} configuration element.
26+
* <p>
27+
* It defines a formatter for groovy source files that can execute both language agnostic (e.g. {@link LicenseHeader})
28+
* and groovy-specific (e.g. {@link Eclipse}) steps.
29+
*/
30+
public class Groovy extends FormatterFactory {
31+
32+
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("src/main/groovy/**/*.groovy", "src/test/groovy/**/*.groovy", "src/main/java/**/*.java", "src/test/java/**/*.java");
33+
private static final String LICENSE_HEADER_DELIMITER = "package ";
34+
35+
@Override
36+
public Set<String> defaultIncludes() {
37+
return DEFAULT_INCLUDES;
38+
}
39+
40+
@Override
41+
public String licenseHeaderDelimiter() {
42+
return LICENSE_HEADER_DELIMITER;
43+
}
44+
45+
public void addEclipse(Eclipse eclipse) {
46+
addStepFactory(eclipse);
47+
}
48+
49+
public void addImportOrder(ImportOrder importOrder) {
50+
addStepFactory(importOrder);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2020 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.groovy;
17+
18+
import java.io.File;
19+
20+
import org.apache.maven.plugins.annotations.Parameter;
21+
22+
import com.diffplug.spotless.FormatterStep;
23+
import com.diffplug.spotless.java.ImportOrderStep;
24+
import com.diffplug.spotless.maven.FormatterStepConfig;
25+
import com.diffplug.spotless.maven.FormatterStepFactory;
26+
27+
public class ImportOrder implements FormatterStepFactory {
28+
@Parameter
29+
private String file;
30+
31+
@Parameter
32+
private String order;
33+
34+
@Override
35+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
36+
if (file != null ^ order != null) {
37+
if (file != null) {
38+
File importsFile = config.getFileLocator().locateFile(file);
39+
return ImportOrderStep.forGroovy().createFrom(importsFile);
40+
} else {
41+
return ImportOrderStep.forGroovy().createFrom(order.split(","));
42+
}
43+
} else if (file == null && order == null) {
44+
return ImportOrderStep.forGroovy().createFrom();
45+
} else {
46+
throw new IllegalArgumentException("Must specify exactly one of 'file' or 'order'.");
47+
}
48+
}
49+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

+4
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ protected void writePomWithAntlr4Steps(String... steps) throws IOException {
102102
writePom(groupWithSteps("antlr4", steps));
103103
}
104104

105+
protected void writePomWithGroovySteps(String... steps) throws IOException {
106+
writePom(groupWithSteps("groovy", steps));
107+
}
108+
105109
protected void writePomWithJavaSteps(String... steps) throws IOException {
106110
writePom(groupWithSteps("java", steps));
107111
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/generic/LicenseHeaderTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public void fromContentCpp() throws Exception {
5151
assertFile(path).hasContent(cppLicense + '\n' + cppContent);
5252
}
5353

54+
@Test
55+
public void fromContentGroovy() throws Exception {
56+
writePomWithGroovySteps(
57+
"<licenseHeader>",
58+
" <content>",
59+
"// If you can't trust a man's word",
60+
"// Does it help to have it in writing?",
61+
" </content>",
62+
"</licenseHeader>");
63+
runTest();
64+
}
65+
5466
@Test
5567
public void fromContentJava() throws Exception {
5668
writePomWithJavaSteps(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2020 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.groovy;
17+
18+
import com.diffplug.spotless.maven.MavenRunner;
19+
20+
import org.junit.Ignore;
21+
import org.junit.Test;
22+
23+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
24+
25+
public class EclipseFormatStepTest extends MavenIntegrationHarness {
26+
27+
@Test
28+
public void testEclipse() throws Exception {
29+
writePomWithGroovySteps(
30+
"<eclipse>",
31+
" <file>${basedir}/greclipse.properties</file>",
32+
" <version>4.12.0</version>",
33+
"</eclipse>");
34+
setFile("greclipse.properties").toResource("groovy/greclipse/format/greclipse.properties");
35+
36+
String path = "src/main/groovy/test.groovy";
37+
setFile(path).toResource("groovy/greclipse/format/unformatted.test");
38+
mavenRunner().withArguments("spotless:apply").runNoError();
39+
assertFile(path).sameAsResource("groovy/greclipse/format/formatted.test");
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020 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.groovy;
17+
18+
import com.diffplug.spotless.maven.MavenRunner;
19+
20+
import org.junit.Test;
21+
22+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
23+
24+
public class ImportOrderTest extends MavenIntegrationHarness {
25+
@Test
26+
public void file() throws Exception {
27+
setFile("import.properties").toResource("java/importsorter/import.properties");
28+
writePomWithGroovySteps(
29+
"<importOrder>",
30+
" <file>${basedir}/import.properties</file>",
31+
"</importOrder>");
32+
runTest();
33+
}
34+
35+
@Test
36+
public void order() throws Exception {
37+
writePomWithGroovySteps(
38+
"<importOrder>",
39+
" <order>java,javax,org,\\#com</order>",
40+
"</importOrder>");
41+
runTest();
42+
}
43+
44+
@Test
45+
public void standard() throws Exception {
46+
writePomWithGroovySteps("<importOrder />");
47+
runTest("java/importsorter/GroovyCodeSortedMisplacedImportsDefault.test");
48+
}
49+
50+
private void runTest() throws Exception {
51+
runTest("java/importsorter/GroovyCodeSortedMisplacedImports.test");
52+
}
53+
54+
private void runTest(String expectedResource) throws Exception {
55+
String path = "src/main/groovy/test.groovy";
56+
setFile(path).toResource("java/importsorter/GroovyCodeUnsortedMisplacedImports.test");
57+
mavenRunner().withArguments("spotless:apply").runNoError();
58+
assertFile(path).sameAsResource(expectedResource);
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import static com.foo.Bar
2+
import static java.lang.Exception.*
3+
import static java.lang.Runnable.*
4+
5+
import java.lang.Runnable
6+
import java.lang.Thread
7+
import org.dooda.Didoo
8+
public class NotDeletedByFormatter {
9+
}
10+
import will.not;
11+
import be.modified;

0 commit comments

Comments
 (0)