From 3fbb7639ea83b7b86d78ab58e77c5b3f41c09214 Mon Sep 17 00:00:00 2001 From: Appu Goundan Date: Tue, 4 Jun 2024 15:09:54 -0400 Subject: [PATCH] Add buildinfo at compile time Signed-off-by: Appu Goundan --- .../kotlin/build-logic.build-info.gradle.kts | 14 +++++++ .../main/kotlin/buildlogic/BuildInfoTask.kt | 37 +++++++++++++++++++ sigstore-java/build.gradle.kts | 5 +++ .../java/dev/sigstore/http/HttpParams.java | 3 +- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts create mode 100644 build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt diff --git a/build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts b/build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts new file mode 100644 index 00000000..0b02efcc --- /dev/null +++ b/build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts @@ -0,0 +1,14 @@ +import buildlogic.BuildInfoTask + +plugins { + java +} + +val generateBuildInfo by tasks.registering(BuildInfoTask::class) { + version.set(project.version.toString()) + genDir.set(project.layout.buildDirectory.dir("generated/buildinfo")) +} + +sourceSets.main { + java.srcDir(generateBuildInfo) +} diff --git a/build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt b/build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt new file mode 100644 index 00000000..3e3bf6d1 --- /dev/null +++ b/build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt @@ -0,0 +1,37 @@ +package buildlogic + +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFile +import org.gradle.api.provider.Property +import org.gradle.api.provider.Provider +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction + +abstract class BuildInfoTask : DefaultTask() { + @get:Input + abstract val packageName: Property + + @get:Input + abstract val version: Property + + @get:OutputDirectory + abstract val genDir: DirectoryProperty + + @TaskAction + fun run() { + val output = """ + |package ${packageName.get()}; + | + |public class BuildInfo { + | public static final String VERSION = "${version.get()}"; + |} + """.trimMargin() + val outputPath = genDir.file(packageName.get().replace(".", "/").plus("/BuildInfo.java")).get().asFile + outputPath.parentFile.mkdirs() + outputPath.writeText(output) + } +} diff --git a/sigstore-java/build.gradle.kts b/sigstore-java/build.gradle.kts index cb442353..fb0bac42 100644 --- a/sigstore-java/build.gradle.kts +++ b/sigstore-java/build.gradle.kts @@ -3,6 +3,7 @@ import com.google.protobuf.gradle.id plugins { id("build-logic.java-published-library") id("build-logic.test-junit5") + id("build-logic.build-info") id("org.jsonschema2dataclass") version "5.0.0" id("com.google.protobuf") version "0.9.4" } @@ -120,3 +121,7 @@ jsonSchema2Pojo { tasks.named("sourcesJar") { dependsOn("generateJsonSchema2DataClassConfigRekor") } + +tasks.generateBuildInfo { + packageName.set("dev.sigstore.buildinfo") +} diff --git a/sigstore-java/src/main/java/dev/sigstore/http/HttpParams.java b/sigstore-java/src/main/java/dev/sigstore/http/HttpParams.java index c8463b49..d2924c34 100644 --- a/sigstore-java/src/main/java/dev/sigstore/http/HttpParams.java +++ b/sigstore-java/src/main/java/dev/sigstore/http/HttpParams.java @@ -16,6 +16,7 @@ package dev.sigstore.http; import com.google.api.client.util.Preconditions; +import dev.sigstore.buildinfo.BuildInfo; import org.immutables.value.Value; /** @@ -24,7 +25,7 @@ */ @Value.Immutable public abstract class HttpParams { - static final String DEFAULT_USER_AGENT = "sigstoreJavaClient/0.0.1"; + static final String DEFAULT_USER_AGENT = "sigstoreJavaClient/" + BuildInfo.VERSION; static final int DEFAULT_TIMEOUT = 60; static final boolean DEFAULT_ALLOW_INSECURE_CONNECTIONS = false;