Unverified Commit edab8cfb authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Fix runtime crate versions (#3260)

## Motivation and Context
When the stable/unstable crate versions were split, this caused a
regression in determining the crate version during codegen. This
enhances our crate-version forwarding code to publish all the crate
versions. For impact, see codegen-client-test.

## Description
- Change the build artifact to be a JSON blob will all required
versions.

## Testing
- [ ] Audit diff

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [ ] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent eb61ae35
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.crateLocation

fun RuntimeConfig.awsRuntimeCrate(name: String, features: Set<String> = setOf()): CargoDependency =
    CargoDependency(name, awsRoot().crateLocation(null), features = features)
    CargoDependency(name, awsRoot().crateLocation(name), features = features)

object AwsCargoDependency {
    fun awsConfig(runtimeConfig: RuntimeConfig) = runtimeConfig.awsRuntimeCrate("aws-config")
+2 −0
Original line number Diff line number Diff line
@@ -79,4 +79,6 @@ object CrateSet {
    )

    val ENTIRE_SMITHY_RUNTIME = (AWS_SDK_SMITHY_RUNTIME + SERVER_SMITHY_RUNTIME).toSortedSet(compareBy { it.name })

    val ALL_CRATES = AWS_SDK_RUNTIME + ENTIRE_SMITHY_RUNTIME
}
+1 −0
Original line number Diff line number Diff line
@@ -476,6 +476,7 @@ class ServiceConfigGenerator(
            docs("Apply test defaults to the builder")
            rustBlock("pub fn apply_test_defaults(&mut self) -> &mut Self") {
                customizations.forEach { it.section(ServiceConfig.DefaultForTests("self"))(this) }
                rustTemplate("self.behavior_version = #{Some}(crate::config::BehaviorVersion::latest());", *preludeScope)
                rust("self")
            }

+33 −4
Original line number Diff line number Diff line
@@ -53,18 +53,47 @@ fun gitCommitHash(): String {
}

val generateSmithyRuntimeCrateVersion by tasks.registering {
    fun kv(key: String, value: String) = "\"$key\": \"$value\""
    // generate the version of the runtime to use as a resource.
    // this keeps us from having to manually change version numbers in multiple places
    val resourcesDir = "$buildDir/resources/main/software/amazon/smithy/rust/codegen/core"
    val versionFile = file("$resourcesDir/runtime-crate-version.txt")
    outputs.file(versionFile)
    val crateVersion = project.properties["smithy.rs.runtime.crate.version"].toString()
    inputs.property("crateVersion", crateVersion)
    val stableCrateVersion = project.properties["smithy.rs.runtime.crate.stable.version"].toString()
    val unstableCrateVersion = project.properties["smithy.rs.runtime.crate.unstable.version"].toString()
    inputs.property("crateVersion", stableCrateVersion)
    // version format must be in sync with `software.amazon.smithy.rust.codegen.core.Version`
    val version = "$crateVersion\n${gitCommitHash()}"
    val version = StringBuilder().append("{")
    version.append(kv("githash", gitCommitHash())).append(",")
    version.append(kv("stableVersion", stableCrateVersion)).append(",")
    version.append(kv("unstableVersion", unstableCrateVersion)).append(",")
    // hack for internal build
    val smithyStableCrates = listOf(
        // AWS crates
        "aws-config",
        "aws-credential-types",
        "aws-runtime",
        "aws-runtime-api",
        "aws-sigv4",
        "aws-types",

        // smithy crates
        "aws-smithy-async",
        "aws-smithy-runtime-api",
        "aws-smithy-runtime",
        "aws-smithy-types",
    )

    val runtimeCrates =
        smithyStableCrates.joinToString(separator = ",", prefix = "{", postfix = "}") { crate ->
            kv(crate, stableCrateVersion)
        }

    version.append(""""runtimeCrates": $runtimeCrates""").append("}")

    sourceSets.main.get().output.dir(resourcesDir)
    doLast {
        versionFile.writeText(version)
        versionFile.writeText(version.toString())
    }
}

+29 −9
Original line number Diff line number Diff line
@@ -6,29 +6,49 @@
package software.amazon.smithy.rust.codegen.core

import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.model.node.Node

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

data class Version(val fullVersion: String, val crateVersion: String) {
data class Version(
    val fullVersion: String,
    val stableCrateVersion: String,
    val unstableCrateVersion: String,
    val crates: Map<String, 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())
            val node = Node.parse(content).expectObjectNode()
            val githash = node.expectStringMember("githash").value
            val stableVersion = node.expectStringMember("stableVersion").value
            val unstableVersion = node.expectStringMember("unstableVersion").value
            return Version(
                "$stableVersion-$githash",
                stableCrateVersion = stableVersion,
                unstableCrateVersion = unstableVersion,
                node.expectObjectMember("runtimeCrates").members.map {
                    it.key.value to it.value.expectStringNode().value
                }.toMap(),
            )
        }

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

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

        private fun fromDefaultResource(): Version = parse(
        fun unstableCrateVersion(): String =
            fromDefaultResource().unstableCrateVersion

        fun crateVersion(crate: String): String {
            val version = fromDefaultResource()
            return version.crates[crate] ?: version.unstableCrateVersion
        }
        fun fromDefaultResource(): Version = parse(
            Version::class.java.getResource(VERSION_FILENAME)?.readText()
                ?: throw CodegenException("$VERSION_FILENAME does not exist"),
        )
Loading