Skip to content

Commit

Permalink
Add Protobuf FormatterFactory to maven plugin (#2291)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Oct 11, 2024
2 parents c43b545 + 0b95d17 commit c326efd
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support for `rdf` ([#2261](https://github.com/diffplug/spotless/pull/2261))
* Support for `buf` on maven plugin ([#2291](https://github.com/diffplug/spotless/pull/2291))
### Changed
* Support configuring the Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
* Add explicit support for JSONC / CSS via biome, via the file extensions `.css` and `.jsonc`.
([#2259](https://github.com/diffplug/spotless/pull/2259))
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
* Bump default `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291))
### Fixed
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static String name() {
}

public static String defaultVersion() {
return "1.24.0";
return "1.44.0";
}

private final String version;
Expand Down
3 changes: 3 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).

## [Unreleased]
### Added
### Changed
* Use the Gradle user home directory by default for the download directory for the biome executable. Previously, the
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))
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
formatting step. Biome supports formatting CSS as of 1.8.0 (experimental, opt-in) and 1.9.0 (stable).
([#2259](https://github.com/diffplug/spotless/pull/2259))
### Changed
* Bump default `jackson` version to latest `2.17.2` -> `2.18.0`. ([#2279](https://github.com/diffplug/spotless/pull/2279))
* Bump default `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291))
### Fixed
* Java import order, ignore duplicate group entries. ([#2293](https://github.com/diffplug/spotless/pull/2293))

Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
## [Unreleased]
### Added
* Support for `rdf` ([#2261](https://github.com/diffplug/spotless/pull/2261))
* Support for `buf` ([#2291](https://github.com/diffplug/spotless/pull/2291))
### Changed
* Leverage local repository for Equo P2 cache. ([#2238](https://github.com/diffplug/spotless/pull/2238))
* Add explicit support for CSS via biome. Formatting CSS via biome was already supported as a general
Expand Down
31 changes: 31 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ user@machine repo % mvn spotless:check
- [Gherkin](#gherkin)
- [Go](#go)
- [RDF](#RDF)
- [Protobuf](#protobuf) ([buf](#buf))
- Multiple languages
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection), [caching `npm install` results](#caching-results-of-npm-install))
- [eclipse web tools platform](#eclipse-web-tools-platform)
Expand Down Expand Up @@ -1177,6 +1178,36 @@ Configuring some generic and TTL options:
RDF parsing is done via [Apache Jena](https://jena.apache.org/) in the version that
[turtle-formatter](https://github.com/atextor/turtle-formatter) depends on (not necessarily the latest).

## Protobuf

[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).
```xml
<configuration>
<includes> <!-- optiona: default is **/*.proto -->
<include>proto/*.proto<include>
<includes>

<excludes> <!-- optiona: if you want to ignore auto generated protos -->
<include>target/**/<include>
<excludes>

<protobuf>
<buf /> <!-- has its own section below -->
</css>
</configuration>
```

### buf

[homepage](https://buf.build/) [buf repo](https://github.com/bufbuild/buf).
```xml
<buf>
<version>1.44.0</version> <!-- optional -->
<pathToExe>/path/to/buf</pathToExe> <!-- optional: if buf isn't in your path -->
</buf>
```


## CSS

[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).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import com.diffplug.spotless.maven.kotlin.Kotlin;
import com.diffplug.spotless.maven.markdown.Markdown;
import com.diffplug.spotless.maven.pom.Pom;
import com.diffplug.spotless.maven.protobuf.Protobuf;
import com.diffplug.spotless.maven.python.Python;
import com.diffplug.spotless.maven.rdf.Rdf;
import com.diffplug.spotless.maven.scala.Scala;
Expand Down Expand Up @@ -200,6 +201,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo {
@Parameter
private Rdf rdf;

@Parameter
private Protobuf protobuf;

@Parameter(property = "spotlessFiles")
private String filePatterns;

Expand Down Expand Up @@ -385,7 +389,7 @@ private FileLocator getFileLocator() {
}

private List<FormatterFactory> getFormatterFactories() {
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))
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))
.filter(Objects::nonNull)
.map(factory -> factory.init(repositorySystemSession))
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016-2024 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.protobuf;

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

import com.diffplug.spotless.FormatterStep;
import com.diffplug.spotless.maven.FormatterStepConfig;
import com.diffplug.spotless.maven.FormatterStepFactory;
import com.diffplug.spotless.protobuf.BufStep;

public class Buf implements FormatterStepFactory {

@Parameter
private String version;

@Parameter
private String pathToExe;

@Override
public FormatterStep newFormatterStep(FormatterStepConfig config) {
BufStep buf = BufStep.withVersion(version == null ? BufStep.defaultVersion() : version);

if (pathToExe != null) {
buf = buf.withPathToExe(pathToExe);
}

return buf.create();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2016-2024 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.protobuf;

import static com.diffplug.spotless.protobuf.ProtobufConstants.LICENSE_HEADER_DELIMITER;

import java.util.Set;

import org.apache.maven.project.MavenProject;

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 <protobuf>...</protobuf>}
* configuration element.
* <p>
* It defines a formatter for protobuf source files that can execute both language agnostic (e.g.
* {@link LicenseHeader}) and protobuf-specific (e.g. {@link Buf}) steps.
*/
public class Protobuf extends FormatterFactory {

private static final Set<String> DEFAULT_INCLUDES = ImmutableSet.of("**/*.proto");

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

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

public void addBuf(Buf buf) {
addStepFactory(buf);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ protected void writePomWithPomSteps(String... steps) throws IOException {
writePom(groupWithSteps("pom", including("pom_test.xml"), steps));
}

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

protected void writePomWithMarkdownSteps(String... steps) throws IOException {
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2024 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.protobuf;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.maven.MavenIntegrationHarness;
import com.diffplug.spotless.tag.BufTest;

@BufTest
class BufMavenIntegrationTest extends MavenIntegrationHarness {
@Test
void buf() throws Exception {
writePomWithProtobufSteps("<buf>", "</buf>");
setFile("buf.proto").toResource("protobuf/buf/buf.proto");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("buf.proto").sameAsResource("protobuf/buf/buf.proto.clean");
}

@Test
void bufLarge() throws Exception {
writePomWithProtobufSteps("<buf>", "</buf>");
setFile("buf.proto").toResource("protobuf/buf/buf_large.proto");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("buf.proto").sameAsResource("protobuf/buf/buf_large.proto.clean");
}

@Test
void bufWithLicense() throws Exception {
writePomWithProtobufSteps(
"<buf>",
"</buf>",
"<licenseHeader>",
" <content>/* (C) 2022 */</content>",
"</licenseHeader>");
setFile("buf.proto").toResource("protobuf/buf/license.proto");
mavenRunner().withArguments("spotless:apply").runNoError();
assertFile("buf.proto").sameAsResource("protobuf/buf/license.proto.clean");
}
}

0 comments on commit c326efd

Please sign in to comment.