Skip to content

Format .proto files with spotlessApply #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
`java-gradle-plugin`
id("org.hypertrace.repository-plugin") version "0.4.0"
id("org.hypertrace.ci-utils-plugin") version "0.3.0"
id("org.hypertrace.code-style-plugin") version "2.0.1"
id("org.hypertrace.publish-plugin") version "1.0.4"
id("org.owasp.dependencycheck") version "8.4.0"
}
Expand All @@ -16,12 +17,8 @@ java {
}

dependencies {
api("com.diffplug.spotless:spotless-plugin-gradle:6.25.0")
constraints {
implementation("com.squareup.okio:okio:3.4.0")
implementation("org.eclipse.jgit:org.eclipse.jgit:6.8.0.202311291450-r")
implementation("org.eclipse.platform:org.eclipse.osgi:3.18.500")
}
api("com.diffplug.spotless:spotless-plugin-gradle:7.0.0")
implementation("build.buf:buf-gradle-plugin:0.10.0")
}

gradlePlugin {
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ plugins {
id("org.hypertrace.version-settings") version "0.2.0"
}

rootProject.name = "hypertrace-gradle-code-style-plugin"
rootProject.name = "hypertrace-gradle-code-style-plugin"
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package org.hypertrace.gradle.code.style;

import build.buf.gradle.BufExtension;
import build.buf.gradle.BufPlugin;
import build.buf.gradle.BufSupportKt;
import com.diffplug.gradle.spotless.SpotlessExtension;
import com.diffplug.gradle.spotless.SpotlessPlugin;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
Expand All @@ -20,13 +23,14 @@ public void apply(@Nonnull Project target) {
private void configureCodeStyle(Project project) {
PluginContainer pluginContainer = project.getPlugins();
pluginContainer.apply(SpotlessPlugin.class);
pluginContainer.apply(BufPlugin.class);
configureFormatting(project);
}

private void configureFormatting(Project project) {
SpotlessExtension spotlessExtension =
project.getExtensions().getByType(SpotlessExtension.class);
configureFormatting(spotlessExtension);
}

private void configureFormatting(SpotlessExtension spotlessExtension) {
spotlessExtension.java(
format -> {
format.importOrder();
Expand All @@ -36,26 +40,34 @@ private void configureFormatting(SpotlessExtension spotlessExtension) {
});

spotlessExtension.kotlinGradle(
format ->
{
try {
format
.ktlint("0.50.0")
.editorConfigOverride(
new HashMap<String, Object>() {
{
put("indent_size", "2");
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
format -> {
try {
format.ktlint("0.50.0").editorConfigOverride(Map.of("indent_size", "2"));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

BufExtension bufExtension = project.getExtensions().getByType(BufExtension.class);
spotlessExtension.protobuf(
format -> {
File bufBinary =
project
.getConfigurations()
.getByName(BufSupportKt.BUF_BINARY_CONFIGURATION_NAME)
.getSingleFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't typically want to do something like this since it forces the buf binary to be resolved whether the user is formatting or not. If the buf plugin is responsible for downloading it, are they not already making it executable? Can you use a provider instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're not making it executable. They do the same in their exec task: https://github.com/bufbuild/buf-gradle-plugin/blob/f03bbc1e5cf902eaf9c505d0b43d603aee6f5a85/src/main/kotlin/build/buf/gradle/BufSupport.kt#L74-L76
I can switch to a provider.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use a provider, let me know if that was what you were expecting.

if (!bufBinary.canExecute()) {
bufBinary.setExecutable(true);
}
format.buf(bufExtension.getToolVersion()).pathToExe(bufBinary.getAbsolutePath());
});
bufExtension.setEnforceFormat(false);

spotlessExtension.format(
"misc",
format -> {
format.target("*.md", "src/**/*.proto", ".gitignore", "*.yaml");
format.indentWithSpaces(2);
format.target("*.md", ".gitignore", "*.yaml");
format.leadingTabsToSpaces(2);
format.trimTrailingWhitespace();
format.endWithNewline();
});
Expand Down
Loading