Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 14 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
id("org.hypertrace.ci-utils-plugin") version "0.3.0"
id("org.hypertrace.publish-plugin") version "1.0.4"
id("org.owasp.dependencycheck") version "8.4.0"
id("com.diffplug.spotless") version "7.0.0"
Copy link
Contributor

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.

Copy link
Contributor Author

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.

}

group = "org.hypertrace.gradle.code.style"
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 All @@ -43,3 +40,14 @@ dependencyCheck {
scanConfigurations.add("runtimeClasspath")
failBuildOnCVSS = 3.0F
}

spotless {
java {
importOrder()
removeUnusedImports()
googleJavaFormat("1.17.0")
}
kotlin {
ktlint()
}
}
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