forked from Kotlin/kotlinx-kover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
256 lines (205 loc) · 8.41 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm")
id("org.jetbrains.kotlinx.binary-compatibility-validator") version "0.13.0"
id("org.jetbrains.dokka") version "1.8.10"
`kotlin-dsl`
`java-gradle-plugin`
id("kover-publishing-conventions")
}
repositories {
mavenCentral()
gradlePluginPortal()
google()
}
val kotlinVersion = property("kotlinVersion")
val localRepositoryUri = uri("build/.m2")
val junitParallelism = findProperty("kover.test.junit.parallelism")?.toString()
sourceSets {
create("functionalTest") {
compileClasspath += sourceSets.main.get().output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
}
}
// adding the ability to use internal classes inside functional tests
kotlin.target.compilations.run {
getByName("functionalTest").associateWith(getByName(KotlinCompilation.MAIN_COMPILATION_NAME))
}
dependencies {
// exclude transitive dependency on stdlib, the Gradle version should be used
compileOnly(kotlin("stdlib"))
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
testImplementation(kotlin("test"))
"functionalTestImplementation"("org.junit.jupiter:junit-jupiter:5.9.0")
"functionalTestImplementation"("org.junit.jupiter:junit-jupiter-params:5.9.0")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
val functionalTest by tasks.registering(Test::class) {
group = LifecycleBasePlugin.VERIFICATION_GROUP
testClassesDirs = sourceSets["functionalTest"].output.classesDirs
classpath = sourceSets["functionalTest"].runtimeClasspath
// use JUnit 5
useJUnitPlatform()
dependsOn(tasks.named("publishAllPublicationsToLocalRepository"))
doFirst {
// basic build properties
setSystemPropertyFromProject("kover.test.kotlin.version")
systemProperties["kotlinVersion"] = kotlinVersion
systemProperties["koverVersion"] = version
systemProperties["localRepositoryPath"] = localRepositoryUri.path
// parallel execution
systemProperties["junit.jupiter.execution.parallel.mode.default"] = "concurrent"
systemProperties["junit.jupiter.execution.parallel.mode.classes.default"] = "concurrent"
systemProperties["junit.jupiter.execution.parallel.config.strategy"] = "fixed"
systemProperties["junit.jupiter.execution.parallel.config.fixed.parallelism"] =
junitParallelism?.toIntOrNull()?.toString() ?: "2"
// this is necessary if tests are run for debugging, in this case it is more difficult to stop at the test you need when they are executed in parallel and you are not sure on which test the execution will pause
systemProperties["junit.jupiter.execution.parallel.enabled"] = if (junitParallelism == "no") "false" else "true"
// customizing functional tests
setSystemPropertyFromProject("kover.release.version")
setSystemPropertyFromProject("kover.test.gradle.version")
setSystemPropertyFromProject("kover.test.android.sdk")
setBooleanSystemPropertyFromProject("kover.test.android.disable")
setBooleanSystemPropertyFromProject("kover.test.junit.logs.info", "testLogsEnabled")
setBooleanSystemPropertyFromProject("debug", "isDebugEnabled")
}
}
fun Test.setSystemPropertyFromProject(name: String) {
if (project.hasProperty(name)) systemProperties[name] = project.property(name)
}
fun Test.setBooleanSystemPropertyFromProject(
projectPropertyName: String,
systemPropertyName: String = projectPropertyName
) {
if (project.hasProperty(projectPropertyName)) systemProperties[systemPropertyName] = true.toString()
}
tasks.check { dependsOn(functionalTest) }
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
allWarningsAsErrors = true
// Kover works with the stdlib of at least version `1.4.x`
languageVersion = "1.4"
apiVersion = "1.4"
// Kotlin compiler 1.7 issues a warning if `languageVersion` or `apiVersion` 1.4 is used - suppress it
freeCompilerArgs = freeCompilerArgs + "-Xsuppress-version-warnings"
}
}
tasks.dokkaHtml {
moduleName.set("Kover Gradle Plugin")
outputDirectory.set(layout.projectDirectory.dir("docs/gradle-plugin/dokka").asFile)
if (project.hasProperty("releaseVersion")) {
moduleVersion.set(project.property("releaseVersion") as String)
}
dokkaSourceSets.configureEach {
// source set configuration section
perPackageOption {
skipDeprecated.set(true)
}
}
}
extensions.configure<Kover_publishing_conventions_gradle.KoverPublicationExtension> {
description.set("Kover Gradle Plugin - Kotlin code coverage")
//`java-gradle-plugin` plugin already creates publication with name `pluginMaven`
addPublication.set(false)
}
publishing {
repositories {
/**
* Maven repository in build directory to store artifacts for using in functional tests.
*/
maven(localRepositoryUri) {
name = "local"
}
}
}
gradlePlugin {
plugins {
create("Kover") {
id = "org.jetbrains.kotlinx.kover"
implementationClass = "kotlinx.kover.gradle.plugin.KoverGradlePlugin"
displayName = "Gradle Plugin for Kotlin Code Coverage Tools"
description = "Evaluate code coverage for projects written in Kotlin"
}
}
}
apiValidation {
ignoredProjects.addAll(listOf("kover-cli", "kover-offline-runtime"))
}
// ====================
// Release preparation
// ====================
tasks.register("prepareRelease") {
dependsOn(tasks.named("dokkaHtml"))
doLast {
if (!project.hasProperty("releaseVersion")) {
throw GradleException("Property 'releaseVersion' is required to run this task")
}
val releaseVersion = project.property("releaseVersion") as String
val prevReleaseVersion = project.property("kover.release.version") as String
val dir = layout.projectDirectory
dir.file("gradle.properties").asFile.patchProperties(releaseVersion)
dir.file("CHANGELOG.md").asFile.patchChangeLog(releaseVersion)
dir.file("README.md").asFile.replaceInFile(prevReleaseVersion, releaseVersion)
// replace versions in examples
dir.dir("examples").asFileTree.matching {
include("**/*gradle")
include("**/*gradle.kts")
}.files.forEach {
it.replaceInFile(prevReleaseVersion, releaseVersion)
}
// replace versions in docs
dir.dir("docs").asFileTree.files.forEach {
it.replaceInFile(prevReleaseVersion, releaseVersion)
}
}
}
fun File.patchChangeLog(releaseVersion: String) {
val oldContent = readText()
writer().use {
it.appendLine("$releaseVersion / ${LocalDate.now().format(DateTimeFormatter.ISO_DATE)}")
it.appendLine("===================")
it.appendLine("TODO add changelog!")
it.appendLine()
it.append(oldContent)
}
}
fun File.patchProperties(releaseVersion: String) {
val oldLines = readLines()
writer().use { writer ->
oldLines.forEach { line ->
when {
line.startsWith("version=") -> writer.append("version=").appendLine(increaseSnapshotVersion(releaseVersion))
line.startsWith("kover.release.version=") -> writer.append("kover.release.version=").appendLine(releaseVersion)
else -> writer.appendLine(line)
}
}
}
}
// modify version '1.2.3' to '1.2.4' and '1.2.3-Beta' to '1.2.3-SNAPSHOT'
fun increaseSnapshotVersion(releaseVersion: String): String {
// remove postfix like '-Alpha'
val correctedVersion = releaseVersion.substringBefore('-')
if (correctedVersion != releaseVersion) {
return "$correctedVersion-SNAPSHOT"
}
// split version 0.0.0 to int parts
val parts = correctedVersion.split('.')
val newVersion = parts.mapIndexed { index, value ->
if (index == parts.size - 1) {
(value.toInt() + 1).toString()
} else {
value
}
}.joinToString(".")
return "$newVersion-SNAPSHOT"
}
fun File.replaceInFile(old: String, new: String) {
val newContent = readText().replace(old, new)
writeText(newContent)
}