Skip to content

Commit c326efd

Browse files
authored
Add Protobuf FormatterFactory to maven plugin (#2291)
2 parents c43b545 + 0b95d17 commit c326efd

File tree

10 files changed

+196
-2
lines changed

10 files changed

+196
-2
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Added
1414
* Support for `rdf` ([#2261](https://github.com/diffplug/spotless/pull/2261))
15+
* Support for `buf` on maven plugin ([#2291](https://github.com/diffplug/spotless/pull/2291))
1516
### Changed
1617
* Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
1718
* Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`.
1819
([#2259](https://github.com/diffplug/spotless/pull/2259))
1920
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
21+
* Bump default `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291))
2022
### Fixed
2123
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))
2224

lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static String name() {
3838
}
3939

4040
public static String defaultVersion() {
41-
return "1.24.0";
41+
return "1.44.0";
4242
}
4343

4444
private final String version;

plugin-gradle/CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Added
67
### Changed
78
* Use the Gradle user home directory by default for the download directory for the biome executable. Previously, the
89
plugin tried to use Maven's home directory, which is not always accessible by a Gradle plugin. ([#2187](https://github.com/diffplug/spotless/issues/2187))
910
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
1011
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
1112
([#2259](https://github.com/diffplug/spotless/pull/2259))
13+
### Changed
1214
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
15+
* Bump default `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291))
1316
### Fixed
1417
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))
1518

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* Support for `rdf` ([#2261](https://github.com/diffplug/spotless/pull/2261))
8+
* Support for `buf` ([#2291](https://github.com/diffplug/spotless/pull/2291))
89
### Changed
910
* Leverage local repository for Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
1011
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general

plugin-maven/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ user@machine repo % mvn spotless:check
5656
- [Gherkin](#gherkin)
5757
- [Go](#go)
5858
- [RDF](#RDF)
59+
- [Protobuf](#protobuf) ([buf](#buf))
5960
- Multiple languages
6061
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
6162
- [eclipse web tools platform](#eclipse-web-tools-platform)
@@ -1177,6 +1178,36 @@ Configuring some generic and TTL options:
11771178
RDF parsing is done via [Apache Jena](https://jena.apache.org/) in the version that
11781179
[turtle-formatter](https://github.com/atextor/turtle-formatter) depends on (not necessarily the latest).
11791180

1181+
## Protobuf
1182+
1183+
[code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java). [available steps](https://github.com/diffplug/spotless/tree/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf).
1184+
```xml
1185+
<configuration>
1186+
<includes> <!-- optiona: default is **/*.proto -->
1187+
<include>proto/*.proto<include>
1188+
<includes>
1189+
1190+
<excludes> <!-- optiona: if you want to ignore auto generated protos -->
1191+
<include>target/**/<include>
1192+
<excludes>
1193+
1194+
<protobuf>
1195+
<buf /> <!-- has its own section below -->
1196+
</css>
1197+
</configuration>
1198+
```
1199+
1200+
### buf
1201+
1202+
[homepage](https://buf.build/) [buf repo](https://github.com/bufbuild/buf).
1203+
```xml
1204+
<buf>
1205+
<version>1.44.0</version> <!-- optional -->
1206+
<pathToExe>/path/to/buf</pathToExe> <!-- optional: if buf isn't in your path -->
1207+
</buf>
1208+
```
1209+
1210+
11801211
## CSS
11811212

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

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
import com.diffplug.spotless.maven.kotlin.Kotlin;
7474
import com.diffplug.spotless.maven.markdown.Markdown;
7575
import com.diffplug.spotless.maven.pom.Pom;
76+
import com.diffplug.spotless.maven.protobuf.Protobuf;
7677
import com.diffplug.spotless.maven.python.Python;
7778
import com.diffplug.spotless.maven.rdf.Rdf;
7879
import com.diffplug.spotless.maven.scala.Scala;
@@ -200,6 +201,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
200201
@Parameter
201202
private Rdf rdf;
202203

204+
@Parameter
205+
private Protobuf protobuf;
206+
203207
@Parameter(property = "spotlessFiles")
204208
private String filePatterns;
205209

@@ -385,7 +389,7 @@ private FileLocator getFileLocator() {
385389
}
386390

387391
private List<FormatterFactory> getFormatterFactories() {
388-
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, css, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go, rdf))
392+
return Stream.concat(formats.stream(), Stream.of(groovy, java, scala, kotlin, cpp, css, typescript, javascript, antlr4, pom, sql, python, markdown, json, shell, yaml, gherkin, go, rdf, protobuf))
389393
.filter(Objects::nonNull)
390394
.map(factory -> factory.init(repositorySystemSession))
391395
.collect(toList());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2016-2024 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.protobuf;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.maven.FormatterStepConfig;
22+
import com.diffplug.spotless.maven.FormatterStepFactory;
23+
import com.diffplug.spotless.protobuf.BufStep;
24+
25+
public class Buf implements FormatterStepFactory {
26+
27+
@Parameter
28+
private String version;
29+
30+
@Parameter
31+
private String pathToExe;
32+
33+
@Override
34+
public FormatterStep newFormatterStep(FormatterStepConfig config) {
35+
BufStep buf = BufStep.withVersion(version == null ? BufStep.defaultVersion() : version);
36+
37+
if (pathToExe != null) {
38+
buf = buf.withPathToExe(pathToExe);
39+
}
40+
41+
return buf.create();
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2016-2024 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.protobuf;
17+
18+
import static com.diffplug.spotless.protobuf.ProtobufConstants.LICENSE_HEADER_DELIMITER;
19+
20+
import java.util.Set;
21+
22+
import org.apache.maven.project.MavenProject;
23+
24+
import com.diffplug.common.collect.ImmutableSet;
25+
import com.diffplug.spotless.maven.FormatterFactory;
26+
import com.diffplug.spotless.maven.generic.LicenseHeader;
27+
28+
/**
29+
* A {@link FormatterFactory} implementation that corresponds to {@code <protobuf>...</protobuf>}
30+
* configuration element.
31+
* <p>
32+
* It defines a formatter for protobuf source files that can execute both language agnostic (e.g.
33+
* {@link LicenseHeader}) and protobuf-specific (e.g. {@link Buf}) steps.
34+
*/
35+
public class Protobuf extends FormatterFactory {
36+
37+
private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("**/*.proto");
38+
39+
@Override
40+
public Set<String> defaultIncludes(MavenProject project) {
41+
return DEFAULT_INCLUDES;
42+
}
43+
44+
@Override
45+
public String licenseHeaderDelimiter() {
46+
return LICENSE_HEADER_DELIMITER;
47+
}
48+
49+
public void addBuf(Buf buf) {
50+
addStepFactory(buf);
51+
}
52+
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ protected void writePomWithPomSteps(String... steps) throws IOException {
171171
writePom(groupWithSteps("pom", including("pom_test.xml"), steps));
172172
}
173173

174+
protected void writePomWithProtobufSteps(String... steps) throws IOException {
175+
writePom(groupWithSteps("protobuf", steps));
176+
}
177+
174178
protected void writePomWithMarkdownSteps(String... steps) throws IOException {
175179
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
176180
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2024 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.protobuf;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
import com.diffplug.spotless.tag.BufTest;
22+
23+
@BufTest
24+
class BufMavenIntegrationTest extends MavenIntegrationHarness {
25+
@Test
26+
void buf() throws Exception {
27+
writePomWithProtobufSteps("<buf>", "</buf>");
28+
setFile("buf.proto").toResource("protobuf/buf/buf.proto");
29+
mavenRunner().withArguments("spotless:apply").runNoError();
30+
assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
31+
}
32+
33+
@Test
34+
void bufLarge() throws Exception {
35+
writePomWithProtobufSteps("<buf>", "</buf>");
36+
setFile("buf.proto").toResource("protobuf/buf/buf_large.proto");
37+
mavenRunner().withArguments("spotless:apply").runNoError();
38+
assertFile("buf.proto").sameAsResource("protobuf/buf/buf_large.proto.clean");
39+
}
40+
41+
@Test
42+
void bufWithLicense() throws Exception {
43+
writePomWithProtobufSteps(
44+
"<buf>",
45+
"</buf>",
46+
"<licenseHeader>",
47+
" <content>/* (C) 2022 */</content>",
48+
"</licenseHeader>");
49+
setFile("buf.proto").toResource("protobuf/buf/license.proto");
50+
mavenRunner().withArguments("spotless:apply").runNoError();
51+
assertFile("buf.proto").sameAsResource("protobuf/buf/license.proto.clean");
52+
}
53+
}

0 commit comments

Comments
 (0)