From d00816f42aa5c6d1a1d1990b9dab77195f102b2d Mon Sep 17 00:00:00 2001 From: zashroof Date: Thu, 3 Oct 2024 19:20:50 -0400 Subject: [PATCH 1/6] Create protobuf formatterFactory for mvn --- .../spotless/maven/AbstractSpotlessMojo.java | 6 ++- .../diffplug/spotless/maven/protobuf/Buf.java | 44 ++++++++++++++++ .../spotless/maven/protobuf/Protobuf.java | 52 +++++++++++++++++++ .../maven/MavenIntegrationHarness.java | 4 ++ .../spotless/maven/protobuf/BufTest.java | 52 +++++++++++++++++++ 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Buf.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java index 9ac91e8db1..ac5d095a16 100644 --- a/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/AbstractSpotlessMojo.java @@ -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; @@ -200,6 +201,9 @@ public abstract class AbstractSpotlessMojo extends AbstractMojo { @Parameter private Rdf rdf; + @Parameter + private Protobuf protobuf; + @Parameter(property = "spotlessFiles") private String filePatterns; @@ -385,7 +389,7 @@ private FileLocator getFileLocator() { } private List 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()); diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Buf.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Buf.java new file mode 100644 index 0000000000..1d336d7639 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Buf.java @@ -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(); + } + +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java new file mode 100644 index 0000000000..3362ea2ad3 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java @@ -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 ...} + * configuration element. + *

+ * 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 DEFAULT_INCLUDES = ImmutableSet.of("**/*.proto"); + + @Override + public Set defaultIncludes(MavenProject project) { + return DEFAULT_INCLUDES; + } + + @Override + public String licenseHeaderDelimiter() { + return LICENSE_HEADER_DELIMITER; + } + + public void addBuf(Buf buf) { + addStepFactory(buf); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index 9dbdf52339..0d0582e180 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -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)); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java new file mode 100644 index 0000000000..86c6f519fb --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java @@ -0,0 +1,52 @@ +/* + * 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; + +class BufTest extends MavenIntegrationHarness { + + @Test + void buf() throws Exception { + writePomWithProtobufSteps("", ""); + 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("", ""); + 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( + "", + "", + "", + " /* (C) 2022 */", + ""); + setFile("buf.proto").toResource("protobuf/buf/license.proto"); + mavenRunner().withArguments("spotless:apply").runNoError(); + assertFile("buf.proto").sameAsResource("protobuf/buf/license.proto.clean"); + } +} From 2a17eab430b1f617482bff13f9f8ab6f0cd3729a Mon Sep 17 00:00:00 2001 From: zashroof Date: Thu, 3 Oct 2024 19:21:25 -0400 Subject: [PATCH 2/6] Update buf default version to 1.44.0 --- lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java index 4fc087ad7a..e623bd0b51 100644 --- a/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java +++ b/lib/src/main/java/com/diffplug/spotless/protobuf/BufStep.java @@ -38,7 +38,7 @@ public static String name() { } public static String defaultVersion() { - return "1.24.0"; + return "1.44.0"; } private final String version; From adc8475884a07d99dc0f44b3e94c41b30df42c1d Mon Sep 17 00:00:00 2001 From: zashroof Date: Thu, 3 Oct 2024 19:34:35 -0400 Subject: [PATCH 3/6] List changes in the PR to changes.md --- CHANGES.md | 2 ++ plugin-maven/CHANGES.md | 1 + 2 files changed, 3 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index d0cf643323..6b7ff82093 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,10 +12,12 @@ 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 `buff` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291)) ## [3.0.0.BETA2] - 2024-08-25 ### Changed diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index b4151a5e9f..e6b681c7bd 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -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 From b128617e57c5b1dcdf62370e6d77bd1dee3c1df2 Mon Sep 17 00:00:00 2001 From: zashroof Date: Thu, 3 Oct 2024 19:57:27 -0400 Subject: [PATCH 4/6] Update readme --- plugin-maven/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index e9cb264279..17dc8651e9 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -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) @@ -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 + + + proto/*.proto + + + + target/**/ + + + + + + +``` + +### buf + +[homepage](https://buf.build/) [buf repo](https://github.com/bufbuild/buf). +```xml + + 1.44.0 + /path/to/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). From 06c954f3e1a27c26f766800c5c0bfec7ab936d07 Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 10 Oct 2024 15:50:22 -0700 Subject: [PATCH 5/6] Rename `BufTest` to `BufMavenIntegrationTest` and mark its weird dependency requirements for CI. --- .../protobuf/{BufTest.java => BufMavenIntegrationTest.java} | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) rename plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/{BufTest.java => BufMavenIntegrationTest.java} (93%) diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java similarity index 93% rename from plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java rename to plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java index 86c6f519fb..4f07c06772 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufTest.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java @@ -18,9 +18,10 @@ import org.junit.jupiter.api.Test; import com.diffplug.spotless.maven.MavenIntegrationHarness; +import com.diffplug.spotless.tag.BufTest; -class BufTest extends MavenIntegrationHarness { - +@BufTest +class BufMavenIntegrationTest extends MavenIntegrationHarness { @Test void buf() throws Exception { writePomWithProtobufSteps("", ""); From 0c6523b7c5b687e7b359b5ad10ea7994e34b9f8e Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Thu, 10 Oct 2024 15:52:07 -0700 Subject: [PATCH 6/6] Some minor changelog bumps. --- CHANGES.md | 2 +- plugin-gradle/CHANGES.md | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 6b7ff82093..06083011c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,7 +17,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * 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 `buff` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291)) +* Bump default `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291)) ## [3.0.0.BETA2] - 2024-08-25 ### Changed diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 8c1c357c32..baeecc64df 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,12 +3,14 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] - +### Added * 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 `buf` version to latest `1.24.0` -> `1.44.0`. ([#2291](https://github.com/diffplug/spotless/pull/2291)) ## [7.0.0.BETA2] - 2024-08-25 ### Changed