forked from Kotlin/kotlinx-kover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
120 lines (100 loc) · 3.96 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
/*
* Copyright 2000-2023 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.time.LocalDate
import java.time.format.DateTimeFormatter
plugins {
kotlin("jvm") apply false
alias(libs.plugins.kotlinx.dokka) apply false
alias(libs.plugins.kotlinx.binaryCompatibilityValidator) apply false
}
// ====================
// Release preparation
// ====================
tasks.register("prepareRelease") {
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 projectDir = layout.projectDirectory
projectDir.file("gradle.properties").asFile.patchProperties(releaseVersion)
projectDir.file("CHANGELOG.md").asFile.patchChangeLog(releaseVersion)
projectDir.file("README.md").asFile.replaceInFile(prevReleaseVersion, releaseVersion)
// replace versions in examples
projectDir.dir("kover-gradle-plugin").dir("examples").patchExamples(releaseVersion, prevReleaseVersion)
projectDir.dir("kover-offline-runtime").dir("examples").patchExamples(releaseVersion, prevReleaseVersion)
// replace versions in docs
projectDir.dir("docs").patchDocs(releaseVersion, prevReleaseVersion)
}
}
fun Directory.patchExamples(releaseVersion: String, prevReleaseVersion: String) {
asFileTree.matching {
include("**/*gradle")
include("**/*gradle.kts")
}.files.forEach {
it.replaceInFile(prevReleaseVersion, releaseVersion)
}
}
fun Directory.patchDocs(releaseVersion: String, prevReleaseVersion: String) {
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)
}