Skip to content

Commit 10a8ca1

Browse files
authored
Convert Gradle scripts from Groovy DSL to Kotlin DSL (#232)
* Convert Gradle scripts from Groovy DSL to Kotlin DSL * convert java example to kts
1 parent 0af2a2b commit 10a8ca1

11 files changed

+156
-151
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ out/
66
target/
77
.DS_Store
88
local.properties
9+
.kotlin/
+36-30
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapperKt
1+
import kotlinx.team.infra.InfraExtension
2+
import kotlinx.validation.ExperimentalBCVApi
3+
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
24
import tasks.CheckReadmeTask
35

46
buildscript {
57
repositories {
6-
maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/maven' }
8+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/maven")
79
gradlePluginPortal()
810

9-
KotlinCommunity.addDevRepositoryIfEnabled(delegate, project)
11+
addDevRepositoryIfEnabled(this, project)
1012
}
1113

1214
dependencies {
1315
classpath(libs.kotlinx.teamInfraGradlePlugin)
1416

15-
String kotlinVersion = providers.gradleProperty("kotlin_version").orNull
16-
if (kotlinVersion != null && !kotlinVersion.isBlank()) {
17+
val kotlinVersion = providers.gradleProperty("kotlin_version").orNull
18+
if (!kotlinVersion.isNullOrBlank()) {
1719
// In addition to overriding the Kotlin version in the Version Catalog,
1820
// also enforce the KGP version using a dependency constraint.
1921
// Constraints are stricter than Version Catalog.
@@ -30,9 +32,9 @@ plugins {
3032
alias(libs.plugins.kotlinx.binaryCompatibilityValidator)
3133
}
3234

33-
apply plugin: 'kotlinx.team.infra'
35+
apply(plugin = "kotlinx.team.infra")
3436

35-
infra {
37+
extensions.configure<InfraExtension> {
3638
teamcity {
3739
libraryStagingRepoDescription = project.name
3840
}
@@ -55,64 +57,68 @@ repositories {
5557

5658
// region Workarounds for https://github.com/gradle/gradle/issues/22335
5759
tasks.register("apiDump") {
58-
it.dependsOn(gradle.includedBuild("plugin").task(":apiDump"))
60+
dependsOn(gradle.includedBuild("plugin").task(":apiDump"))
5961
}
6062

6163
tasks.register("apiCheck") {
62-
it.dependsOn(gradle.includedBuild("plugin").task(":apiCheck"))
64+
dependsOn(gradle.includedBuild("plugin").task(":apiCheck"))
6365
}
6466

6567
afterEvaluate {
6668
gradle.includedBuilds.forEach { included ->
6769
project(":kotlinx-benchmark-runtime").tasks.named("publishToMavenLocal") { dependsOn(included.task(":publishToMavenLocal")) }
6870
}
6971
}
72+
tasks.register("publishToMavenLocal") {
73+
dependsOn(gradle.includedBuild("plugin").task(":publishToMavenLocal"))
74+
}
7075
//endregion
7176

72-
String currentKgpVersion = KotlinPluginWrapperKt.getKotlinPluginVersion(project)
73-
logger.info("Using Kotlin Gradle Plugin ${currentKgpVersion}")
77+
val currentKgpVersion = getKotlinPluginVersion()
78+
logger.info("Using Kotlin Gradle Plugin $currentKgpVersion")
7479

75-
String kotlinVersionOverride = providers.gradleProperty("kotlin_version").getOrNull()
80+
val kotlinVersionOverride = providers.gradleProperty("kotlin_version").getOrNull()
7681

7782
if (kotlinVersionOverride != null) {
78-
String versionCatalogKotlinVersion = libs.versions.kotlin.get()
83+
val versionCatalogKotlinVersion = libs.versions.kotlin.get()
7984
if (kotlinVersionOverride != versionCatalogKotlinVersion) {
80-
throw new IllegalStateException("Kotlin version in Version Catalog was not overridden. Expected:$kotlinVersionOverride, actual:$versionCatalogKotlinVersion.")
85+
throw IllegalStateException("Kotlin version in Version Catalog was not overridden. Expected:$kotlinVersionOverride, actual:$versionCatalogKotlinVersion.")
8186
}
8287
if (kotlinVersionOverride != currentKgpVersion) {
83-
throw new IllegalStateException("Kotlin Gradle Plugin version was not overridden. Expected:$kotlinVersionOverride, actual:$currentKgpVersion.")
88+
throw IllegalStateException("Kotlin Gradle Plugin version was not overridden. Expected:$kotlinVersionOverride, actual:$currentKgpVersion.")
8489
}
8590
}
8691

8792
allprojects {
8893
repositories {
89-
KotlinCommunity.addDevRepositoryIfEnabled(delegate, project)
94+
addDevRepositoryIfEnabled(this, project)
9095
}
9196
}
9297

9398
apiValidation {
94-
ignoredProjects += [
95-
"examples",
96-
"java",
97-
"kotlin",
98-
"kotlin-kts",
99-
"kotlin-multiplatform",
100-
"integration",
101-
]
102-
103-
nonPublicMarkers += ["kotlinx.benchmark.internal.KotlinxBenchmarkRuntimeInternalApi"]
104-
99+
ignoredProjects += listOf(
100+
"examples",
101+
"java",
102+
"kotlin",
103+
"kotlin-kts",
104+
"kotlin-multiplatform",
105+
"integration",
106+
)
107+
108+
nonPublicMarkers += listOf("kotlinx.benchmark.internal.KotlinxBenchmarkRuntimeInternalApi")
109+
110+
@OptIn(ExperimentalBCVApi::class)
105111
klib {
106-
it.enabled = true
112+
enabled = true
107113
}
108114
}
109115

110-
tasks.register("checkReadme", CheckReadmeTask) {
116+
val checkReadme by tasks.registering(CheckReadmeTask::class) {
111117
minSupportedGradleVersion = libs.versions.minSupportedGradle
112118
readme = file("README.md")
113119
}
114120

115121
tasks.check {
116-
dependsOn(tasks.named("checkReadme"))
122+
dependsOn(checkReadme)
117123
dependsOn(tasks.named("apiCheck"))
118124
}

examples/build.gradle

-8
This file was deleted.

examples/build.gradle.kts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
group = "org.jetbrains.kotlinx.benchmark.examples"
2+
version = "0.1-SNAPSHOT"
3+
4+
subprojects {
5+
repositories {
6+
mavenCentral()
7+
}
8+
}

examples/java/build.gradle renamed to examples/java/build.gradle.kts

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
buildscript {
2-
dependencies {
3-
classpath "org.jetbrains.kotlinx:kotlinx-benchmark-plugin"
4-
}
5-
}
1+
import kotlinx.benchmark.gradle.JvmBenchmarkTarget
62

73
plugins {
8-
id 'java'
4+
java
5+
id("org.jetbrains.kotlinx.benchmark")
96
}
107

11-
apply plugin: 'org.jetbrains.kotlinx.benchmark'
12-
138
dependencies {
149
implementation(project(":kotlinx-benchmark-runtime"))
1510
}
1611

1712
benchmark {
1813
configurations {
19-
main {
14+
named("main") {
2015
iterationTime = 300
2116
iterationTimeUnit = "ms"
2217
}
23-
singleParam {
18+
create("singleParam") {
2419
iterationTime = 300
2520
iterationTimeUnit = "ms"
2621
param("stringValue", "C", "D")
@@ -29,6 +24,7 @@ benchmark {
2924
}
3025
targets {
3126
register("main") {
27+
this as JvmBenchmarkTarget
3228
jmhVersion = "1.21"
3329
}
3430
}

examples/kotlin/build.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ dependencies {
4242
benchmark {
4343
targets {
4444
register("benchmarks") {
45-
if(this is JvmBenchmarkTarget) {
45+
if (this is JvmBenchmarkTarget) {
4646
jmhVersion = "1.21"
4747
}
4848
}
4949
}
50-
}
50+
}

plugin/build.gradle renamed to plugin/build.gradle.kts

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
2-
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
2+
import kotlinx.team.infra.InfraExtension
33

44
buildscript {
5-
ext.kotlinDevUrl = rootProject.properties["kotlin_repo_url"]
65
repositories {
7-
maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/maven' }
6+
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/maven")
87
mavenCentral()
9-
if (kotlinDevUrl != null) {
10-
maven { url = kotlinDevUrl }
8+
val kotlinRepoUrl = providers.gradleProperty("kotlin_repo_url").orNull
9+
if (kotlinRepoUrl != null) {
10+
maven(kotlinRepoUrl)
1111
}
1212
}
1313

@@ -21,16 +21,16 @@ buildscript {
2121
}
2222

2323
plugins {
24-
id 'java-gradle-plugin'
25-
id 'maven-publish'
24+
`java-gradle-plugin`
25+
`maven-publish`
2626
alias(libs.plugins.gradle.pluginPublish)
2727
alias(libs.plugins.kotlinx.binaryCompatibilityValidator)
2828
alias(libs.plugins.kotlin.jvm)
2929
}
3030

31-
apply(plugin: 'kotlinx.team.infra')
31+
apply(plugin = "kotlinx.team.infra")
3232

33-
infra {
33+
extensions.configure<InfraExtension> {
3434
teamcity {
3535
libraryStagingRepoDescription = project.name
3636
}
@@ -47,20 +47,21 @@ repositories {
4747
mavenCentral()
4848
gradlePluginPortal()
4949

50-
if (kotlinDevUrl != null) {
51-
maven { url = kotlinDevUrl }
50+
val kotlinRepoUrl = providers.gradleProperty("kotlin_repo_url").orNull
51+
if (kotlinRepoUrl != null) {
52+
maven(kotlinRepoUrl)
5253
}
5354
}
5455

5556
pluginBundle {
56-
website = 'https://github.com/Kotlin/kotlinx-benchmark'
57-
vcsUrl = 'https://github.com/Kotlin/kotlinx-benchmark.git'
58-
tags = ['benchmarking', 'multiplatform', 'kotlin']
57+
website = "https://github.com/Kotlin/kotlinx-benchmark"
58+
vcsUrl = "https://github.com/Kotlin/kotlinx-benchmark.git"
59+
tags = listOf("benchmarking", "multiplatform", "kotlin")
5960
}
6061

6162
gradlePlugin {
6263
plugins {
63-
benchmarkPlugin {
64+
register("benchmarkPlugin") {
6465
id = "org.jetbrains.kotlinx.benchmark"
6566
implementationClass = "kotlinx.benchmark.gradle.BenchmarksPlugin"
6667
displayName = "Gradle plugin for benchmarking"
@@ -71,24 +72,24 @@ gradlePlugin {
7172

7273
sourceSets {
7374
main {
74-
kotlin.srcDirs = ['main/src']
75-
java.srcDirs = ['main/src']
76-
resources.srcDirs = ['main/resources']
75+
kotlin.srcDirs(listOf("main/src"))
76+
java.srcDirs(listOf("main/src"))
77+
resources.srcDirs(listOf("main/resources"))
7778
}
7879
test {
79-
kotlin.srcDirs = ['test/src']
80-
java.srcDirs = ['test/src']
81-
resources.srcDirs = ['test/resources']
80+
kotlin.srcDirs("test/src")
81+
java.srcDirs("test/src")
82+
resources.srcDirs("test/resources")
8283
}
8384
}
8485

85-
tasks.named("compileKotlin", KotlinCompilationTask.class) {
86+
tasks.compileKotlin {
8687
compilerOptions {
8788
optIn.addAll(
8889
"kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi",
8990
"kotlin.RequiresOptIn",
9091
)
91-
//noinspection GrDeprecatedAPIUsage
92+
@Suppress("DEPRECATION")
9293
apiVersion = KotlinVersion.KOTLIN_1_4 // the version of Kotlin embedded in Gradle
9394
}
9495
}
@@ -107,34 +108,34 @@ dependencies {
107108
compileOnly(libs.jmh.generatorBytecode) // used in worker
108109
}
109110

110-
def generatePluginConstants = tasks.register("generatePluginConstants") {
111+
val generatePluginConstants by tasks.registering {
111112
description = "Generates constants file used by BenchmarksPlugin"
112113

113-
File outputDir = temporaryDir
114+
val outputDir = temporaryDir
114115
outputs.dir(outputDir).withPropertyName("outputDir")
115116

116-
File constantsKtFile = new File(outputDir, "BenchmarksPluginConstants.kt")
117+
val constantsKtFile = File(outputDir, "BenchmarksPluginConstants.kt")
117118

118-
Provider<String> benchmarkPluginVersion = project.providers.gradleProperty("releaseVersion")
119+
val benchmarkPluginVersion = project.providers.gradleProperty("releaseVersion")
119120
.orElse(project.version.toString())
120121
inputs.property("benchmarkPluginVersion", benchmarkPluginVersion)
121122

122-
Provider<String> minSupportedGradleVersion = libs.versions.minSupportedGradle
123+
val minSupportedGradleVersion = libs.versions.minSupportedGradle
123124
inputs.property("minSupportedGradleVersion", minSupportedGradleVersion)
124125

125-
Provider<String> kotlinCompilerVersion = libs.versions.kotlin
126+
val kotlinCompilerVersion = libs.versions.kotlin
126127
inputs.property("kotlinCompilerVersion", kotlinCompilerVersion)
127128

128129
doLast {
129-
constantsKtFile.write(
130+
constantsKtFile.writeText(
130131
"""|package kotlinx.benchmark.gradle.internal
131132
|
132133
|internal object BenchmarksPluginConstants {
133134
| const val BENCHMARK_PLUGIN_VERSION = "${benchmarkPluginVersion.get()}"
134135
| const val MIN_SUPPORTED_GRADLE_VERSION = "${minSupportedGradleVersion.get()}"
135136
| const val DEFAULT_KOTLIN_COMPILER_VERSION = "${kotlinCompilerVersion.get()}"
136137
|}
137-
|""".stripMargin()
138+
|""".trimMargin()
138139
)
139140
}
140141
}
@@ -151,16 +152,16 @@ if (project.findProperty("publication_repository") == "space") {
151152
repositories {
152153
maven {
153154
name = "space"
154-
url = "https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/dev"
155+
url = uri("https://maven.pkg.jetbrains.space/kotlin/p/kotlinx/dev")
155156
credentials {
156-
username = project.findProperty("space.user")
157-
password = project.findProperty("space.token")
157+
username = project.findProperty("space.user") as? String?
158+
password = project.findProperty("space.token") as? String?
158159
}
159160
}
160161
}
161162
}
162163
}
163164

164165
apiValidation {
165-
nonPublicMarkers += ["kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi"]
166+
nonPublicMarkers += listOf("kotlinx.benchmark.gradle.internal.KotlinxBenchmarkPluginInternalApi")
166167
}

plugin/settings.gradle renamed to plugin/settings.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ dependencyResolutionManagement {
1212
}
1313
}
1414

15-
rootProject.name = 'kotlinx-benchmark-plugin'
15+
rootProject.name = "kotlinx-benchmark-plugin"

0 commit comments

Comments
 (0)