diff --git a/CHANGES.md b/CHANGES.md index 02e14cc79a..1ab0bdfca1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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)) 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; diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index e3550693f7..b13b5cd5d2 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -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)) diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 452e1e79a8..21f2113238 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 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). 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/BufMavenIntegrationTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java new file mode 100644 index 0000000000..4f07c06772 --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java @@ -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("", ""); + 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"); + } +}