-
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 2 commits
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,14 +1,18 @@ | ||
| 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; | ||
| import org.gradle.api.plugins.PluginContainer; | ||
| import org.gradle.api.provider.Provider; | ||
|
|
||
| public class CodeStylePlugin implements Plugin<Project> { | ||
|
|
||
|
|
@@ -20,13 +24,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 +41,40 @@ 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); | ||
| Provider<File> bufBinaryProvider = | ||
| project.provider( | ||
| () -> { | ||
| File binary = | ||
| project | ||
| .getConfigurations() | ||
| .getByName(BufSupportKt.BUF_BINARY_CONFIGURATION_NAME) | ||
| .getSingleFile(); | ||
| if (!binary.canExecute()) { | ||
| binary.setExecutable(true); | ||
| } | ||
| return binary; | ||
| }); | ||
| spotlessExtension.protobuf( | ||
| format -> | ||
| format | ||
| .buf(bufExtension.getToolVersion()) | ||
| .pathToExe(bufBinaryProvider.get().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.
We always want versions pinned. By latest version, I meant the actual version that was most recent :) Otherwise a breaking change could break the build.