-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
@@ -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(); | ||
|
|
@@ -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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than using spotless directly you could just use the last version of this plugin to have been published to dogfood it a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use the current version and settled on this, but that's a good idea to use the last version.