Skip to content

Commit

Permalink
Add git commit hash to version
Browse files Browse the repository at this point in the history
  • Loading branch information
unexge committed Aug 9, 2022
1 parent 34fb6c4 commit 030cd19
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
17 changes: 16 additions & 1 deletion codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import java.io.ByteArrayOutputStream

plugins {
kotlin("jvm")
Expand Down Expand Up @@ -39,6 +40,18 @@ tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

fun gitCommitHash() =
try {
val output = ByteArrayOutputStream()
exec {
commandLine = listOf("git", "rev-parse", "HEAD")
standardOutput = output
}
output.toString().trim()
} catch (ex: Exception) {
"unknown"
}

val generateSmithyRuntimeCrateVersion by tasks.registering {
// generate the version of the runtime to use as a resource.
// this keeps us from having to manually change version numbers in multiple places
Expand All @@ -47,9 +60,11 @@ val generateSmithyRuntimeCrateVersion by tasks.registering {
outputs.file(versionFile)
val crateVersion = project.properties["smithy.rs.runtime.crate.version"].toString()
inputs.property("crateVersion", crateVersion)
// version format must be in sync with `software.amazon.smithy.rust.codegen.smithy.Version`
val version = "$crateVersion-${gitCommitHash()}"
sourceSets.main.get().output.dir(resourcesDir)
doLast {
versionFile.writeText(crateVersion)
versionFile.writeText(version)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ import software.amazon.smithy.codegen.core.CodegenException

class Version {
companion object {
// generated as part of the build, see codegen/build.gradle.kts
private const val CRATE_VERSION_FILENAME = "runtime-crate-version.txt"
// generated as part of the build in the "{smithy_rs_version}-{git_commit_hash}" format,
// see codegen/build.gradle.kts
private const val VERSION_FILENAME = "smithy-version.txt"

fun crateVersion(): String {
return object {}.javaClass.getResource(CRATE_VERSION_FILENAME)?.readText()
?: throw CodegenException("$CRATE_VERSION_FILENAME does not exist")
fun version(): String {
return object {}.javaClass.getResource(VERSION_FILENAME)?.readText()
?: throw CodegenException("$VERSION_FILENAME does not exist")
}

fun crateVersion(): String =
version().split("-").first()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ class CargoTomlGenerator(
}
}

fun smithyCodegenVersion(): String = Version.crateVersion()
fun smithyCodegenVersion(): String = Version.version()

0 comments on commit 030cd19

Please sign in to comment.