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");
+ }
+}