Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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 "latest.release"
Copy link
Contributor

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.

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,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> {

Expand All @@ -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();
Expand All @@ -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()));
Copy link
Contributor

Choose a reason for hiding this comment

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

so the provider part is right, but if pathToExe doesn't accept a provider we don't get the benefit we're looking for - we want gradle to be resolving the provider JIT so .get() is usually a red flag.

This actually is a closure already (the lambda from 67) it's just one that likely evaluates during config time rather than execution. So moving the resolution inside here appears to be the best we can do. Given that, we have the option of either removing the explicit provider and moving that code inside this lambda or leaving it as is. They should be equivalent, so up to you which you feel is more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, I see what you mean now. I believe the spotless gradle plugin supports lazy configuration, so it shouldn't try to configure the protobuf plugin if the user is not formatting files. I'll revert my last commit that moved the resolution from this lambda to a provider.

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