Skip to content

Commit b8e07eb

Browse files
authored
Standardise and verify the minimum supported Gradle version (#204)
- store the minimal supported Gradle version in libs.versions.toml - Add a new CheckReadmeTask to check that the README is up-to-date - update code for generating plugin constants into a new file, rather than editing the plugin file in-place - add integration tests for checking the supported Gradle version, and logged warning
1 parent e037425 commit b8e07eb

File tree

12 files changed

+158
-26
lines changed

12 files changed

+158
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,4 @@ These examples showcase various use cases and offer practical insights into the
432432

433433
## Contributing
434434

435-
We welcome contributions to `kotlinx-benchmark`! If you want to contribute, please refer to our [Contribution Guidelines](CONTRIBUTING.md).
435+
We welcome contributions to `kotlinx-benchmark`! If you want to contribute, please refer to our [Contribution Guidelines](CONTRIBUTING.md).

build.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import tasks.CheckReadmeTask
2+
13
buildscript {
24
repositories {
35
maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/maven' }
@@ -13,6 +15,7 @@ buildscript {
1315
}
1416

1517
plugins {
18+
id("base")
1619
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.15.0-Beta.1"
1720
}
1821

@@ -72,3 +75,12 @@ apiValidation {
7275
it.enabled = true
7376
}
7477
}
78+
79+
tasks.register("checkReadme", CheckReadmeTask) {
80+
minSupportedGradleVersion = libs.versions.minSupportedGradle
81+
readme = file("README.md")
82+
}
83+
84+
tasks.check {
85+
dependsOn(tasks.named("checkReadme"))
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package tasks
2+
3+
import org.gradle.api.*
4+
import org.gradle.api.file.*
5+
import org.gradle.api.provider.*
6+
import org.gradle.api.tasks.*
7+
import org.gradle.api.tasks.PathSensitivity.*
8+
9+
abstract class CheckReadmeTask : DefaultTask() {
10+
@get:Input
11+
abstract val minSupportedGradleVersion: Property<String>
12+
13+
@get:InputFile
14+
@get:PathSensitive(RELATIVE)
15+
abstract val readme: RegularFileProperty
16+
17+
@TaskAction
18+
fun execute() {
19+
val readme = readme.get().asFile
20+
val readmeContents = readme.readText()
21+
22+
val minSupportedGradleVersion = minSupportedGradleVersion.get()
23+
24+
val matches = Regex("Gradle (?<version>[^ ]+) or newer").findAll(readmeContents).toList()
25+
26+
require(matches.size >= 1) {
27+
"""
28+
$readme does not contain correct min supported Gradle version.
29+
${matches.size} matches found.
30+
""".trimIndent()
31+
}
32+
33+
matches.forEach { match ->
34+
val version = match.groups["version"]?.value ?: error("Regex failed - could not find version")
35+
require(minSupportedGradleVersion == version) {
36+
"""
37+
$readme does not contain correct min supported Gradle version
38+
Actual: ${match.value}
39+
Expected: Gradle $minSupportedGradleVersion or newer
40+
""".trimIndent()
41+
}
42+
}
43+
}
44+
}

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[versions]
2+
3+
minSupportedGradle = "7.4"

integration/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ tasks.test {
2525
systemProperty("runtime_repo_url", rootProject.buildDir.resolve("maven").absoluteFile.invariantSeparatorsPath)
2626
systemProperty("kotlin_repo_url", rootProject.properties["kotlin_repo_url"])
2727
systemProperty("kotlin_version", rootProject.properties["kotlin_version"]!!)
28+
systemProperty("minSupportedGradleVersion", libs.versions.minSupportedGradle.get())
2829
}

integration/src/main/kotlin/kotlinx/benchmark/integration/GradleTestVersion.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ package kotlinx.benchmark.integration
22

33
enum class GradleTestVersion(val versionString: String) {
44
v8_0("8.0.2"),
5-
}
5+
MinSupportedGradleVersion("7.4"),
6+
UnsupportedGradleVersion("7.3"),
7+
}

integration/src/main/kotlin/kotlinx/benchmark/integration/Runner.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package kotlinx.benchmark.integration
22

3-
import org.gradle.testkit.runner.*
4-
import java.io.*
3+
import kotlinx.benchmark.integration.GradleTestVersion.MinSupportedGradleVersion
4+
import org.gradle.testkit.runner.BuildResult
5+
import org.gradle.testkit.runner.GradleRunner
6+
import java.io.File
57

68
class Runner(
79
private val projectDir: File,
810
private val print: Boolean,
911
private val gradleVersion: GradleTestVersion? = null,
1012
) {
11-
1213
private fun gradle(vararg tasks: String): GradleRunner =
1314
GradleRunner.create()
1415
.withProjectDir(projectDir)
1516
.withArguments(*(defaultArguments() + tasks))
17+
.withGradleVersion((gradleVersion ?: MinSupportedGradleVersion).versionString)
1618
.run {
1719
if (print) forwardStdOutput(System.out.bufferedWriter()) else this
1820
}
19-
.run {
20-
if (gradleVersion != null) withGradleVersion(gradleVersion.versionString) else this
21-
}
2221

2322
fun run(vararg tasks: String, fn: BuildResult.() -> Unit = {}) {
2423
val gradle = gradle(*tasks)

integration/src/test/kotlin/kotlinx/benchmark/integration/ConfigurationCacheTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,4 @@ private fun BuildResult.assertConfigurationCacheStored() {
6969

7070
private fun BuildResult.assertConfigurationCacheReused() {
7171
assertOutputContains("Configuration cache entry reused.")
72-
}
72+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package kotlinx.benchmark.integration
2+
3+
import org.gradle.util.GradleVersion
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import kotlin.test.assertTrue
7+
8+
class SupportedGradleVersionTest : GradleTest() {
9+
10+
/** The min supported version used in build scripts, provided as a system property. */
11+
private val minSupportedGradleVersion = System.getProperty("minSupportedGradleVersion")
12+
private val warningMessage =
13+
"JetBrains Gradle Benchmarks plugin requires Gradle version ${GradleTestVersion.MinSupportedGradleVersion.versionString}"
14+
15+
@Test
16+
fun `test MinSupportedGradleVersion matches the version used in build scripts`() {
17+
assertEquals(minSupportedGradleVersion, GradleTestVersion.MinSupportedGradleVersion.versionString)
18+
}
19+
20+
@Test
21+
fun `test MinSupportedGradleVersion is greater than UnsupportedGradleVersion`() {
22+
// verify the test data is valid
23+
assertTrue(
24+
GradleVersion.version(GradleTestVersion.MinSupportedGradleVersion.versionString) >
25+
GradleVersion.version(GradleTestVersion.UnsupportedGradleVersion.versionString)
26+
)
27+
}
28+
29+
@Test
30+
fun `when using min supported Gradle version, expect no warning`() {
31+
val runner = project("kotlin-multiplatform", gradleVersion = GradleTestVersion.MinSupportedGradleVersion)
32+
33+
runner.run(":help", "-q") {
34+
assertOutputDoesNotContain(warningMessage)
35+
}
36+
}
37+
38+
@Test
39+
fun `when using unsupported Gradle version, expect warning`() {
40+
val runner = project("kotlin-multiplatform", gradleVersion = GradleTestVersion.UnsupportedGradleVersion)
41+
42+
runner.run(":help", "-q") {
43+
assertOutputContains(warningMessage)
44+
}
45+
}
46+
}

plugin/build.gradle

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,39 @@ dependencies {
103103
compileOnly "org.openjdk.jmh:jmh-generator-bytecode:$jmhVersion" // used in worker
104104
}
105105

106-
tasks.register("overridePluginVersion") {
107-
description = "Overrides BenchmarksPlugin.PLUGIN_VERSION during release builds"
108-
onlyIf {
109-
project.findProperty("releaseVersion") != null
110-
}
106+
def generatePluginConstants = tasks.register("generatePluginConstants") {
107+
description = "Generates constants file used by BenchmarksPlugin"
108+
109+
File outputDir = temporaryDir
110+
outputs.dir(outputDir).withPropertyName("outputDir")
111+
112+
File constantsKtFile = new File(outputDir, "BenchmarksPluginConstants.kt")
113+
114+
Provider<String> benchmarkPluginVersion = project.providers.gradleProperty("releaseVersion")
115+
.orElse(project.version.toString())
116+
inputs.property("benchmarkPluginVersion", benchmarkPluginVersion)
117+
118+
Provider<String> minSupportedGradleVersion = libs.versions.minSupportedGradle
119+
inputs.property("minSupportedGradleVersion", minSupportedGradleVersion)
120+
111121
doLast {
112-
def benchmarksPluginFile = "${projectDir}/main/src/kotlinx/benchmark/gradle/BenchmarksPlugin.kt"
113-
def releaseVersion = project.findProperty("releaseVersion")
114-
ant.replaceregexp(
115-
file: benchmarksPluginFile,
116-
match: "const val PLUGIN_VERSION = \"[\\d.]+-SNAPSHOT\"",
117-
replace: "const val PLUGIN_VERSION = \"$releaseVersion\"",
118-
encoding: "UTF-8"
122+
constantsKtFile.write(
123+
"""|package kotlinx.benchmark.gradle.internal
124+
|
125+
|internal object BenchmarksPluginConstants {
126+
| const val BENCHMARK_PLUGIN_VERSION = "${benchmarkPluginVersion.get()}"
127+
| const val MIN_SUPPORTED_GRADLE_VERSION = "${minSupportedGradleVersion.get()}"
128+
|}
129+
|""".stripMargin()
119130
)
120131
}
121132
}
122133

123-
tasks.compileKotlin.dependsOn overridePluginVersion
134+
sourceSets {
135+
main {
136+
kotlin.srcDir(generatePluginConstants)
137+
}
138+
}
124139

125140
if (project.findProperty("publication_repository") == "space") {
126141
// publish to Space repository

0 commit comments

Comments
 (0)