Skip to content

Commit e6b154b

Browse files
authored
Generate MSRV in Kotlin file (#3869)
Currently, when generating Rust crates, the MSRV is read from `gradle.properties` by determining the project root using `git rev-parse`. However, some build environments smithy-rs needs to build on don't have `git` available. This commit uses Gradle's `rootDir` to determine the project root, and generates a Kotlin file exposing the MSRV as part of a Gradle build task that is then published as part of `codegen-core.jar`. This also has the benefit that consumers of smithy-rs can read the MSRV from there. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 2470341 commit e6b154b

File tree

3 files changed

+53
-24
lines changed

3 files changed

+53
-24
lines changed

codegen-core/build.gradle.kts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
77
import java.io.ByteArrayOutputStream
8+
import java.util.Properties
89

910
plugins {
1011
kotlin("jvm")
@@ -53,6 +54,44 @@ fun gitCommitHash(): String {
5354
}
5455
}
5556

57+
// Define the directory where the generated Kotlin file will be placed
58+
val generatedSrcDir = layout.buildDirectory.dir("generated/src/main/kotlin")
59+
60+
sourceSets {
61+
main {
62+
kotlin {
63+
srcDir(generatedSrcDir)
64+
}
65+
}
66+
}
67+
68+
val generateBuildEnvironmentConstants = tasks.register("generateBuildEnvironmentConstants") {
69+
// Specify that the task generates sources.
70+
val outputDir = generatedSrcDir.get().asFile
71+
outputs.dir(outputDir)
72+
73+
doLast {
74+
// Load properties from `gradle.properties`.
75+
val properties = Properties()
76+
val gradlePropertiesFile = file("${rootDir}/gradle.properties")
77+
properties.load(gradlePropertiesFile.inputStream())
78+
79+
val rustMsrv = properties.getProperty("rust.msrv")
80+
81+
// Generate the Kotlin file.
82+
val generatedFile = file("$outputDir/BuildEnvironment.kt")
83+
generatedFile.writeText("""
84+
// This file is automatically generated. Do not modify manually.
85+
package software.amazon.smithy.rust.codegen.core.generated
86+
87+
object BuildEnvironment {
88+
const val MSRV: String = "$rustMsrv"
89+
const val PROJECT_DIR: String = "$rootDir"
90+
}
91+
""".trimIndent())
92+
}
93+
}
94+
5695
val generateSmithyRuntimeCrateVersion by tasks.registering {
5796
// Generate the version of the runtime to use as a resource.
5897
// This keeps us from having to manually change version numbers in multiple places.
@@ -125,6 +164,7 @@ java {
125164
tasks.compileKotlin {
126165
kotlinOptions.jvmTarget = "11"
127166
dependsOn(generateSmithyRuntimeCrateVersion)
167+
dependsOn(generateBuildEnvironmentConstants)
128168
}
129169

130170
// Reusable license copySpec

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import software.amazon.smithy.model.loader.ModelAssembler
1515
import software.amazon.smithy.model.node.Node
1616
import software.amazon.smithy.model.node.ObjectNode
1717
import software.amazon.smithy.model.shapes.ShapeId
18+
import software.amazon.smithy.rust.codegen.core.generated.BuildEnvironment
1819
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
1920
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
2021
import software.amazon.smithy.rust.codegen.core.rustlang.DependencyScope
@@ -40,11 +41,10 @@ import software.amazon.smithy.rust.codegen.core.util.letIf
4041
import software.amazon.smithy.rust.codegen.core.util.orNullIfEmpty
4142
import software.amazon.smithy.rust.codegen.core.util.runCommand
4243
import java.io.File
43-
import java.io.FileInputStream
4444
import java.nio.file.Files
4545
import java.nio.file.Files.createTempDirectory
4646
import java.nio.file.Path
47-
import java.util.Properties
47+
import kotlin.io.path.Path
4848
import kotlin.io.path.absolutePathString
4949
import kotlin.io.path.writeText
5050

@@ -56,8 +56,6 @@ val TestModuleDocProvider =
5656
}
5757
}
5858

59-
val projectRootDir by lazy { File("git rev-parse --show-toplevel".runCommand().replace("\n", "")) }
60-
6159
/**
6260
* Waiting for Kotlin to stabilize their temp directory functionality
6361
*/
@@ -73,23 +71,14 @@ private fun tempDir(directory: File? = null): File {
7371
* This function returns the minimum supported Rust version, as specified in the `gradle.properties` file
7472
* located at the root of the project.
7573
*/
76-
fun msrv(): String {
77-
val properties = Properties()
78-
val propertiesFilePath = projectRootDir.resolve("gradle.properties")
79-
80-
FileInputStream(propertiesFilePath).use { inputStream ->
81-
properties.load(inputStream)
82-
}
83-
84-
return properties.getProperty("rust.msrv")
85-
}
74+
fun msrv(): String = BuildEnvironment.MSRV
8675

8776
/**
8877
* Generates the `rust-toolchain.toml` file in the specified directory.
8978
*
9079
* The compiler version is set in `gradle.properties` under the `rust.msrv` property.
91-
* The Gradle task `GenerateMsrvTask` generates the Kotlin class
92-
* `software.amazon.smithy.rust.codegen.core.Msrv` and writes the value of `rust.msrv` into it.
80+
* The Gradle task `generateRustMsrvFile` generates the Kotlin class
81+
* `software.amazon.smithy.rust.codegen.core.generated.RustMsrv.kt` and writes the value of `rust.msrv` into it.
9382
*/
9483
private fun File.generateRustToolchainToml() {
9584
resolve("rust-toolchain.toml").writeText(
@@ -123,7 +112,7 @@ object TestWorkspace {
123112
private val subprojects = mutableListOf<String>()
124113

125114
private val cargoLock: File by lazy {
126-
projectRootDir.resolve("aws/sdk/Cargo.lock")
115+
File(BuildEnvironment.PROJECT_DIR).resolve("aws/sdk/Cargo.lock")
127116
}
128117

129118
init {

codegen-core/src/test/kotlin/software/amazon/smithy/rust/codegen/core/util/RustToolChainTomlTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import io.kotest.matchers.booleans.shouldBeTrue
99
import io.kotest.matchers.paths.shouldExist
1010
import io.kotest.matchers.shouldNotBe
1111
import org.junit.jupiter.api.Test
12+
import software.amazon.smithy.rust.codegen.core.generated.BuildEnvironment
1213
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
1314
import software.amazon.smithy.rust.codegen.core.testutil.generatePluginContext
14-
import software.amazon.smithy.rust.codegen.core.testutil.projectRootDir
15+
import java.io.File
1516
import java.nio.file.Files.createTempDirectory
1617
import java.util.regex.Pattern
1718

@@ -48,7 +49,7 @@ internal class RustToolChainTomlTest {
4849

4950
// Read the MSRV written in `gradle.properties` file.
5051
val msrvPattern = Pattern.compile("rust\\.msrv=(.+)")
51-
val gradlePropertiesPath = projectRootDir.resolve("gradle.properties")
52+
val gradlePropertiesPath = File(BuildEnvironment.PROJECT_DIR).resolve("gradle.properties")
5253
val msrv =
5354
gradlePropertiesPath.useLines { lines ->
5455
lines.firstNotNullOfOrNull { line ->
@@ -75,12 +76,11 @@ internal class RustToolChainTomlTest {
7576

7677
// There should be a [toolchain] table, and it must have a key called 'channel' whose value must
7778
// match the `rust.msrv` specified in gradle.properties.
78-
toolchainSection != null &&
79-
toolchainSection.any { line ->
80-
channelPattern.matcher(line).let { matcher ->
81-
matcher.find() && matcher.group(1) == msrv
82-
}
79+
toolchainSection.any { line ->
80+
channelPattern.matcher(line).let { matcher ->
81+
matcher.find() && matcher.group(1) == msrv
8382
}
83+
}
8484
}
8585

8686
channelMatches.shouldBeTrue()

0 commit comments

Comments
 (0)