Skip to content

Commit

Permalink
Add buildinfo at compile time
Browse files Browse the repository at this point in the history
Signed-off-by: Appu Goundan <[email protected]>
  • Loading branch information
loosebazooka committed Jun 18, 2024
1 parent 0e154bf commit 3fbb763
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
14 changes: 14 additions & 0 deletions build-logic/jvm/src/main/kotlin/build-logic.build-info.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions build-logic/jvm/src/main/kotlin/buildlogic/BuildInfoTask.kt
Original file line number Diff line number Diff line change
@@ -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<String>

@get:Input
abstract val version: Property<String>

@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)
}
}
5 changes: 5 additions & 0 deletions sigstore-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -120,3 +121,7 @@ jsonSchema2Pojo {
tasks.named("sourcesJar") {
dependsOn("generateJsonSchema2DataClassConfigRekor")
}

tasks.generateBuildInfo {
packageName.set("dev.sigstore.buildinfo")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

Expand Down

0 comments on commit 3fbb763

Please sign in to comment.