generated from cortinico/kotlin-gradle-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
version-bumper.main.kts
executable file
·137 lines (119 loc) · 4.34 KB
/
version-bumper.main.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
#!/usr/bin/env kotlin
import java.io.File
import java.io.IOException
import java.util.Properties
import kotlin.system.exitProcess
val VERSION_FILE = "./plugin-build/gradle.properties"
val README_FILE = "./README.md"
enum class VersionPart { MAJOR, MINOR, PATCH }
fun bumpVersion(part: VersionPart, release: Boolean, isSnapshot: Boolean) {
val currentVersion = getCurrentVersion().removeSuffix("-SNAPSHOT")
if (isSnapshot) {
val snapshotVersion = "$currentVersion-SNAPSHOT"
updateVersionFile(snapshotVersion)
commitTagAndPush(snapshotVersion)
return
}
val newVersion = incrementVersion(currentVersion, part)
updateVersionFile(newVersion)
updateReadmeVersions(newVersion)
commitTagAndPush(newVersion)
if (release) {
createGithubRelease(newVersion)
waitAndViewGithubAction()
}
}
fun getCurrentVersion(): String {
return try {
val properties = Properties()
File(VERSION_FILE).inputStream().use { properties.load(it) }
properties.getProperty("VERSION") ?: throw IOException("Version not found in properties file")
} catch (e: IOException) {
println("Version file not found!")
exitProcess(1)
}
}
fun incrementVersion(currentVersion: String, part: VersionPart): String {
val (major, minor, patch) = currentVersion.split(".").map { it.toInt() }
return when (part) {
VersionPart.MAJOR -> "v${major + 1}.0.0"
VersionPart.MINOR -> "v$major.${minor + 1}.0"
VersionPart.PATCH -> "v$major.$minor.${patch + 1}"
}
}
fun updateVersionFile(newVersion: String) {
val properties = Properties()
File(VERSION_FILE).inputStream().use { properties.load(it) }
properties.setProperty("VERSION", newVersion.removePrefix("v"))
File(VERSION_FILE).outputStream().use { properties.store(it, null) }
println(properties)
}
fun updateReadmeVersions(newVersion: String) {
try {
val readmeContent = File(README_FILE).readText()
val updatedContent = readmeContent.replace(
Regex("""dev\.iurysouza:modulegraph:\d+\.\d+\.\d+"""),
"dev.iurysouza:modulegraph:${newVersion.removePrefix("v")}",
)
File(README_FILE).writeText(updatedContent)
println("Updated version references in README to $newVersion")
} catch (e: IOException) {
println("An error occurred while updating README: ${e.message}")
exitProcess(1)
}
}
fun commitTagAndPush(newVersion: String) {
try {
println("Committing changes...")
runCommand("git", "add", VERSION_FILE)
runCommand("git", "commit", "-m", "Bump version to $newVersion")
runCommand("git", "push")
runCommand("git", "tag", newVersion)
runCommand("git", "push", "origin", newVersion)
println("Version updated to $newVersion and tag pushed")
} catch (e: IOException) {
println("An error occurred while running git commands: ${e.message}")
exitProcess(1)
}
}
fun createGithubRelease(newVersion: String) {
try {
println("Creating GitHub release...")
runCommand("gh", "release", "create", newVersion, "--title", newVersion, "--notes", "Auto release")
println("GitHub release for $newVersion created successfully")
} catch (e: IOException) {
println("An error occurred while creating GitHub release: ${e.message}")
exitProcess(1)
}
}
fun waitAndViewGithubAction() {
Thread.sleep(1000)
runCommand("gh", "run", "view", "-w")
}
fun runCommand(vararg command: String) {
val process = ProcessBuilder(command.toList())
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
val exitCode = process.waitFor()
if (exitCode != 0) {
throw IOException("Command '${command.joinToString(" ")}' failed with exit code $exitCode")
}
}
val part = when (args.getOrNull(0)?.uppercase()) {
"MAJOR" -> VersionPart.MAJOR
"MINOR" -> VersionPart.MINOR
"PATCH" -> VersionPart.PATCH
else -> VersionPart.PATCH
}
val release = args.getOrNull(1)?.toBoolean() ?: false
val isSnapshot = args.getOrNull(2)?.toBoolean() ?: false
println("Bumping version...")
println("Part: $part")
println("Release: $release")
try {
bumpVersion(part, release, isSnapshot)
} catch (e: Exception) {
println("Error: ${e.message}")
exitProcess(1)
}