Unverified Commit 610d963d authored by Burak's avatar Burak Committed by GitHub
Browse files

Add codegen version to generated package (#1621)

* Add codegen version to generated package metadata

* Update `CHANGELOG.next.toml`

* Remove unnecessary try-catch block from `smithyCodegenVersion`

* Add git commit hash to version

* Fix version filename

* Add tests for `Version`

* Store version in "$smithyRsVersion\n$gitCommitHash" format

* Make version parsing more strict
parent 48a85316
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -10,3 +10,9 @@
# references = ["smithy-rs#920"]
# meta = { "breaking" = false, "tada" = false, "bug" = false, "sdk" = "client | server | all"}
# author = "rcoh"

[[smithy-rs]]
message = "Add codegen version to generated package metadata"
references = ["smithy-rs#1612"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "unexge"
+16 −1
Original line number Diff line number Diff line
@@ -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\n${gitCommitHash()}"
    sourceSets.main.get().output.dir(resourcesDir)
    doLast {
        versionFile.writeText(crateVersion)
        versionFile.writeText(version)
    }
}

+2 −4
Original line number Diff line number Diff line
@@ -46,12 +46,10 @@ fun RuntimeCrateLocation.crateLocation(): DependencyLocation = when (this.path)
}

fun defaultRuntimeCrateVersion(): String {
    // generated as part of the build, see codegen/build.gradle.kts
    try {
        return object {}.javaClass.getResource("runtime-crate-version.txt")?.readText()
            ?: throw CodegenException("sdk-version.txt does not exist")
        return Version.crateVersion()
    } catch (ex: Exception) {
        throw CodegenException("failed to load sdk-version.txt which sets the default client-runtime version", ex)
        throw CodegenException("failed to get crate version which sets the default client-runtime version", ex)
    }
}

+37 −0
Original line number Diff line number Diff line
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.smithy.rust.codegen.smithy

import software.amazon.smithy.codegen.core.CodegenException

// generated as part of the build, see codegen/build.gradle.kts
private const val VERSION_FILENAME = "runtime-crate-version.txt"

data class Version(val fullVersion: String, val crateVersion: String) {
    companion object {
        // Version must be in the "{smithy_rs_version}\n{git_commit_hash}" format
        fun parse(content: String): Version {
            val lines = content.lines()
            if (lines.size != 2) {
                throw IllegalArgumentException("Invalid version format, it should contain `2` lines but contains `${lines.size}` line(s)")
            }
            return Version(lines.joinToString("-"), lines.first())
        }

        // Returns full version in the "{smithy_rs_version}-{git_commit_hash}" format
        fun fullVersion(): String =
            fromDefaultResource().fullVersion

        fun crateVersion(): String =
            fromDefaultResource().crateVersion

        private fun fromDefaultResource(): Version =
            parse(
                object {}.javaClass.getResource(VERSION_FILENAME)?.readText()
                    ?: throw CodegenException("$VERSION_FILENAME does not exist"),
            )
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ import software.amazon.smithy.rust.codegen.rustlang.DependencyScope
import software.amazon.smithy.rust.codegen.rustlang.Feature
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.smithy.CoreRustSettings
import software.amazon.smithy.rust.codegen.smithy.Version
import software.amazon.smithy.rust.codegen.util.deepMergeWith

/**
@@ -63,6 +64,11 @@ class CargoTomlGenerator(
                "edition" to "2021",
                "license" to settings.license,
                "repository" to settings.moduleRepository,
                "metadata" to listOfNotNull(
                    "smithy" to listOfNotNull(
                        "codegen-version" to Version.fullVersion(),
                    ).toMap(),
                ).toMap(),
            ).toMap(),
            "dependencies" to dependencies.filter { it.scope == DependencyScope.Compile }
                .associate { it.name to it.toMap() },
Loading