Skip to content

Commit

Permalink
Merge branch 'main' into maven-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Sep 17, 2020
2 parents 2e75385 + 6a1f09d commit e9a5448
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ lib('generic.TrimTrailingWhitespaceStep') +'{{yes}} | {{yes}}
lib('antlr4.Antlr4FormatterStep') +'{{yes}} | {{yes}} | {{no}} | {{no}} |',
lib('cpp.ClangFormatStep') +'{{yes}} | {{no}} | {{no}} | {{no}} |',
extra('cpp.EclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{no}} | {{yes}} | {{no}} |',
extra('groovy.GrEclipseFormatterStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
lib('java.GoogleJavaFormatStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
lib('java.ImportOrderStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
lib('java.RemoveUnusedImportsStep') +'{{yes}} | {{yes}} | {{yes}} | {{no}} |',
Expand Down Expand Up @@ -88,7 +88,7 @@ extra('wtp.EclipseWtpFormatterStep') +'{{yes}} | {{yes}}
| [`antlr4.Antlr4FormatterStep`](lib/src/main/java/com/diffplug/spotless/antlr4/Antlr4FormatterStep.java) | :+1: | :+1: | :white_large_square: | :white_large_square: |
| [`cpp.ClangFormatStep`](lib/src/main/java/com/diffplug/spotless/cpp/ClangFormatStep.java) | :+1: | :white_large_square: | :white_large_square: | :white_large_square: |
| [`cpp.EclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/cpp/EclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :white_large_square: | :+1: | :white_large_square: |
| [`groovy.GrEclipseFormatterStep`](lib-extra/src/main/java/com/diffplug/spotless/extra/groovy/GrEclipseFormatterStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`java.GoogleJavaFormatStep`](lib/src/main/java/com/diffplug/spotless/java/GoogleJavaFormatStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`java.ImportOrderStep`](lib/src/main/java/com/diffplug/spotless/java/ImportOrderStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
| [`java.RemoveUnusedImportsStep`](lib/src/main/java/com/diffplug/spotless/java/RemoveUnusedImportsStep.java) | :+1: | :+1: | :+1: | :white_large_square: |
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [2.3.1] - 2020-09-12
### Fixed
* Improved JRE parsing to handle strings like `16-loom` (fixes [#693](https://github.com/diffplug/spotless/issues/693)).
* Added support for groovy formatting ([#697](https://github.com/diffplug/spotless/pull/697))

## [2.3.0] - 2020-09-11
### Added
Expand Down
45 changes: 45 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ user@machine repo % mvn spotless:check
- [Requirements](#requirements)
- **Languages**
- [Java](#java) ([google-java-format](#google-java-format), [eclipse jdt](#eclipse-jdt), [prettier](#prettier))
- [Groovy](#groovy) ([eclipse groovy](#eclipse-groovy))
- [Kotlin](#kotlin) ([ktlint](#ktlint), [ktfmt](#ktfmt), [prettier](#prettier))
- [Scala](#scala) ([scalafmt](#scalafmt))
- [C/C++](#cc) ([eclipse cdt](#eclipse-cdt))
Expand Down Expand Up @@ -185,6 +186,50 @@ Spotless requires Maven to be running on JRE 8+.
</eclipse>
```

## Groovy

[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy/Groovy.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/groovy).

```xml
<configuration>
<groovy>
<!-- These are the defaults, you can override if you want -->
<includes>
<include>src/main/groovy/**/*.groovy</include>
<include>src/test/groovy/**/*.groovy</include>
<include>src/main/java/**/*.java</include>
<include>src/test/java/**/*.java</include>
</includes>

<importOrder /> <!-- standard import order -->
<importOrder> <!-- or a custom ordering -->
<order>java,javax,org,com,com.diffplug,</order> <!-- or use <file>${basedir}/eclipse.importorder</file> -->
<!-- You probably want an empty string at the end - all of the
imports you didn't specify explicitly will go there. -->
</importOrder>

<greclipse /> <!-- has its own section below -->

<licenseHeader>
<content>/* (C)$YEAR */</content> <!-- or <file>${basedir}/license-header</file> -->
</licenseHeader>
</java>
</configuration>
```

### eclipse groovy

[homepage](https://github.com/groovy/groovy-eclipse/wiki). [changelog](https://github.com/groovy/groovy-eclipse/releases). [compatible versions](https://github.com/diffplug/spotless/tree/main/lib-extra/src/main/resources/com/diffplug/spotless/extra/groovy_eclipse_formatter). The Groovy formatter uses some of the [eclipse jdt](#eclipse-jdt) configuration parameters in addition to groovy-specific ones. All parameters can be configured within a single file, like the Java properties file [greclipse.properties](../testlib/src/main/resources/groovy/greclipse/format/greclipse.properties) in the previous example. The formatter step can also load the [exported Eclipse properties](../ECLIPSE_SCREENSHOTS.md) and augment it with the `.metadata/.plugins/org.eclipse.core.runtime/.settings/org.codehaus.groovy.eclipse.ui.prefs` from your Eclipse workspace as shown below.

```xml
<greclipse>
<version>4.13.0</version> <!-- optional -->
<file>${basedir}/greclipse.properties</file> <!-- optional -->
</greclipse>
```

Groovy-Eclipse formatting errors/warnings lead per default to a build failure. This behavior can be changed by adding the property/key value `ignoreFormatterProblems=true` to a configuration file. In this scenario, files causing problems, will not be modified by this formatter step.

<a name="applying-to-kotlin-source"></a>

## Kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.diffplug.spotless.maven.cpp.Cpp;
import com.diffplug.spotless.maven.generic.Format;
import com.diffplug.spotless.maven.generic.LicenseHeader;
import com.diffplug.spotless.maven.groovy.Groovy;
import com.diffplug.spotless.maven.java.Java;
import com.diffplug.spotless.maven.kotlin.Kotlin;
import com.diffplug.spotless.maven.scala.Scala;
Expand Down Expand Up @@ -95,6 +96,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private List<Format> formats = Collections.emptyList();

@Parameter
private Groovy groovy;

@Parameter
private Java java;

Expand Down Expand Up @@ -205,7 +209,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
return Stream.concat(formats.stream(), Stream.of(java, scala, kotlin, cpp, typescript, antlr4, sql))
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, typescript, antlr4, sql))
.filter(Objects::nonNull)
.collect(toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.groovy;

import java.io.File;
import java.util.Arrays;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.extra.EclipseBasedStepBuilder;
import com.diffplug.spotless.extra.groovy.GrEclipseFormatterStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class GrEclipse implements FormatterStepFactory {

@Parameter
private String file;

@Parameter
private String version;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
EclipseBasedStepBuilder grEclipseConfig = GrEclipseFormatterStep.createBuilder(stepConfig.getProvisioner());
grEclipseConfig.setVersion(version == null ? GrEclipseFormatterStep.defaultVersion() : version);
if (null != file) {
File settingsFile = stepConfig.getFileLocator().locateFile(file);
grEclipseConfig.setPreferences(Arrays.asList(settingsFile));
}
return grEclipseConfig.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.groovy;

import java.util.Set;

import com.diffplug.common.collect.ImmutableSet;
import com.diffplug.spotless.maven.FormatterFactory;
import com.diffplug.spotless.maven.generic.LicenseHeader;

/**
* A {@link FormatterFactory} implementation that corresponds to {@code <groovy>...</groovy>} configuration element.
* <p>
* It defines a formatter for groovy source files that can execute both language agnostic (e.g. {@link LicenseHeader})
* and groovy-specific (e.g. {@link GrEclipse}) steps.
*/
public class Groovy extends FormatterFactory {

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");
private static final String LICENSE_HEADER_DELIMITER = "package ";

@Override
public Set<String> defaultIncludes() {
return DEFAULT_INCLUDES;
}

@Override
public String licenseHeaderDelimiter() {
return LICENSE_HEADER_DELIMITER;
}

public void addGreclipse(GrEclipse greclipse) {
addStepFactory(greclipse);
}

public void addImportOrder(ImportOrder importOrder) {
addStepFactory(importOrder);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.groovy;

import java.io.File;

import org.apache.maven.plugins.annotations.Parameter;

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.java.ImportOrderStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;

public class ImportOrder implements FormatterStepFactory {
@Parameter
private String file;

@Parameter
private String order;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
if (file != null ^ order != null) {
if (file != null) {
File importsFile = config.getFileLocator().locateFile(file);
return ImportOrderStep.forGroovy().createFrom(importsFile);
} else {
return ImportOrderStep.forGroovy().createFrom(order.split(","));
}
} else if (file == null && order == null) {
return ImportOrderStep.forGroovy().createFrom();
} else {
throw new IllegalArgumentException("Must specify exactly one of 'file' or 'order'.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ protected void writePomWithAntlr4Steps(String... steps) throws IOException {
writePom(groupWithSteps("antlr4", steps));
}

protected void writePomWithGroovySteps(String... steps) throws IOException {
writePom(groupWithSteps("groovy", steps));
}

protected void writePomWithJavaSteps(String... steps) throws IOException {
writePom(groupWithSteps("java", steps));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public void fromContentCpp() throws Exception {
assertFile(path).hasContent(cppLicense + '\n' + cppContent);
}

@Test
public void fromContentGroovy() throws Exception {
writePomWithGroovySteps(
"<licenseHeader>",
" <content>",
"// If you can't trust a man's word",
"// Does it help to have it in writing?",
" </content>",
"</licenseHeader>");
runTest();
}

@Test
public void fromContentJava() throws Exception {
writePomWithJavaSteps(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.groovy;

import org.junit.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

public class GrEclipseTest extends MavenIntegrationHarness {

@Test
public void testEclipse() throws Exception {
writePomWithGroovySteps(
"<greclipse>",
" <file>${basedir}/greclipse.properties</file>",
" <version>4.12.0</version>",
"</greclipse>");
setFile("greclipse.properties").toResource("groovy/greclipse/format/greclipse.properties");

String path = "src/main/groovy/test.groovy";
setFile(path).toResource("groovy/greclipse/format/unformatted.test");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).sameAsResource("groovy/greclipse/format/formatted.test");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.diffplug.spotless.maven.groovy;

import org.junit.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;

public class ImportOrderTest extends MavenIntegrationHarness {
@Test
public void file() throws Exception {
setFile("import.properties").toResource("java/importsorter/import.properties");
writePomWithGroovySteps(
"<importOrder>",
" <file>${basedir}/import.properties</file>",
"</importOrder>");
runTest();
}

@Test
public void order() throws Exception {
writePomWithGroovySteps(
"<importOrder>",
" <order>java,javax,org,\\#com</order>",
"</importOrder>");
runTest();
}

@Test
public void standard() throws Exception {
writePomWithGroovySteps("<importOrder />");
runTest("java/importsorter/GroovyCodeSortedMisplacedImportsDefault.test");
}

private void runTest() throws Exception {
runTest("java/importsorter/GroovyCodeSortedMisplacedImports.test");
}

private void runTest(String expectedResource) throws Exception {
String path = "src/main/groovy/test.groovy";
setFile(path).toResource("java/importsorter/GroovyCodeUnsortedMisplacedImports.test");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile(path).sameAsResource(expectedResource);
}
}
Loading

0 comments on commit e9a5448

Please sign in to comment.