-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Protobuf FormatterFactory to maven plugin (#2291)
- Loading branch information
Showing
10 changed files
with
196 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Buf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
plugin-maven/src/main/java/com/diffplug/spotless/maven/protobuf/Protobuf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
plugin-maven/src/test/java/com/diffplug/spotless/maven/protobuf/BufMavenIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |