Skip to content

Commit 59a764e

Browse files
committed
Format .proto files with spotlessApply
- Use the buf-gradle-plugin to download the correct buf binary automatically and configure spotless to use it - Update spotless plugin to `7.0.0` - Use spotless to format files in this repo Signed-off-by: Akshath Kothari <[email protected]>
1 parent 53095b8 commit 59a764e

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

build.gradle.kts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ plugins {
66
id("org.hypertrace.ci-utils-plugin") version "0.3.0"
77
id("org.hypertrace.publish-plugin") version "1.0.4"
88
id("org.owasp.dependencycheck") version "8.4.0"
9+
id("com.diffplug.spotless") version "7.0.0"
910
}
1011

1112
group = "org.hypertrace.gradle.code.style"
@@ -16,12 +17,8 @@ java {
1617
}
1718

1819
dependencies {
19-
api("com.diffplug.spotless:spotless-plugin-gradle:6.25.0")
20-
constraints {
21-
implementation("com.squareup.okio:okio:3.4.0")
22-
implementation("org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r")
23-
implementation("org.eclipse.platform:org.eclipse.osgi:3.18.500")
24-
}
20+
api("com.diffplug.spotless:spotless-plugin-gradle:7.0.0")
21+
implementation("build.buf:buf-gradle-plugin:0.10.0")
2522
}
2623

2724
gradlePlugin {
@@ -43,3 +40,14 @@ dependencyCheck {
4340
scanConfigurations.add("runtimeClasspath")
4441
failBuildOnCVSS = 3.0F
4542
}
43+
44+
spotless {
45+
java {
46+
importOrder()
47+
removeUnusedImports()
48+
googleJavaFormat("1.17.0")
49+
}
50+
kotlin {
51+
ktlint()
52+
}
53+
}

src/main/java/org/hypertrace/gradle/code/style/CodeStylePlugin.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package org.hypertrace.gradle.code.style;
22

3+
import build.buf.gradle.BufExtension;
4+
import build.buf.gradle.BufPlugin;
5+
import build.buf.gradle.BufSupportKt;
36
import com.diffplug.gradle.spotless.SpotlessExtension;
47
import com.diffplug.gradle.spotless.SpotlessPlugin;
5-
8+
import java.io.File;
69
import java.io.IOException;
7-
import java.util.HashMap;
10+
import java.util.Map;
811
import javax.annotation.Nonnull;
912
import org.gradle.api.Plugin;
1013
import org.gradle.api.Project;
@@ -20,13 +23,14 @@ public void apply(@Nonnull Project target) {
2023
private void configureCodeStyle(Project project) {
2124
PluginContainer pluginContainer = project.getPlugins();
2225
pluginContainer.apply(SpotlessPlugin.class);
26+
pluginContainer.apply(BufPlugin.class);
27+
configureFormatting(project);
28+
}
2329

30+
private void configureFormatting(Project project) {
2431
SpotlessExtension spotlessExtension =
2532
project.getExtensions().getByType(SpotlessExtension.class);
26-
configureFormatting(spotlessExtension);
27-
}
2833

29-
private void configureFormatting(SpotlessExtension spotlessExtension) {
3034
spotlessExtension.java(
3135
format -> {
3236
format.importOrder();
@@ -36,26 +40,34 @@ private void configureFormatting(SpotlessExtension spotlessExtension) {
3640
});
3741

3842
spotlessExtension.kotlinGradle(
39-
format ->
40-
{
41-
try {
42-
format
43-
.ktlint("0.50.0")
44-
.editorConfigOverride(
45-
new HashMap<String, Object>() {
46-
{
47-
put("indent_size", "2");
48-
}
49-
});
50-
} catch (IOException e) {
51-
throw new RuntimeException(e);
52-
}
43+
format -> {
44+
try {
45+
format.ktlint("0.50.0").editorConfigOverride(Map.of("indent_size", "2"));
46+
} catch (IOException e) {
47+
throw new RuntimeException(e);
48+
}
5349
});
50+
51+
BufExtension bufExtension = project.getExtensions().getByType(BufExtension.class);
52+
spotlessExtension.protobuf(
53+
format -> {
54+
File bufBinary =
55+
project
56+
.getConfigurations()
57+
.getByName(BufSupportKt.BUF_BINARY_CONFIGURATION_NAME)
58+
.getSingleFile();
59+
if (!bufBinary.canExecute()) {
60+
bufBinary.setExecutable(true);
61+
}
62+
format.buf(bufExtension.getToolVersion()).pathToExe(bufBinary.getAbsolutePath());
63+
});
64+
bufExtension.setEnforceFormat(false);
65+
5466
spotlessExtension.format(
5567
"misc",
5668
format -> {
57-
format.target("*.md", "src/**/*.proto", ".gitignore", "*.yaml");
58-
format.indentWithSpaces(2);
69+
format.target("*.md", ".gitignore", "*.yaml");
70+
format.leadingTabsToSpaces(2);
5971
format.trimTrailingWhitespace();
6072
format.endWithNewline();
6173
});

0 commit comments

Comments
 (0)