diff --git a/codegen/build.gradle.kts b/codegen/build.gradle.kts index e73271af856..ab4a337d015 100644 --- a/codegen/build.gradle.kts +++ b/codegen/build.gradle.kts @@ -4,6 +4,7 @@ */ import org.gradle.api.tasks.testing.logging.TestExceptionFormat +import java.io.ByteArrayOutputStream plugins { kotlin("jvm") @@ -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 @@ -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) } } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt index a40e04e115e..46343b8e00b 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/Version.kt @@ -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() } } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt index c2fd941a059..f9c466a2b20 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/generators/CargoTomlGenerator.kt @@ -81,4 +81,4 @@ class CargoTomlGenerator( } } -fun smithyCodegenVersion(): String = Version.crateVersion() +fun smithyCodegenVersion(): String = Version.version()