-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathversion-bumper.main.kts
More file actions
executable file
·242 lines (206 loc) · 8.01 KB
/
version-bumper.main.kts
File metadata and controls
executable file
·242 lines (206 loc) · 8.01 KB
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
#!/usr/bin/env kotlin
import java.io.File
import java.io.IOException
import java.util.Properties
import kotlin.system.exitProcess
val versionFile = "./plugin-build/gradle.properties"
val readmeFile = "./README.md"
enum class VersionPart { MAJOR, MINOR, PATCH }
fun checkMainBranch() {
try {
val currentBranch = ProcessBuilder("git", "rev-parse", "--abbrev-ref", "HEAD")
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.inputStream.bufferedReader().readText().trim()
if (currentBranch != "main") {
println("Error: Must be on main branch to bump version. Current branch: $currentBranch")
exitProcess(1)
}
} catch (e: IOException) {
println("Error checking git branch: ${e.message}")
exitProcess(1)
}
}
fun bumpVersion(part: VersionPart, release: Boolean, isSnapshot: Boolean) {
checkMainBranch()
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(versionFile).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(versionFile).inputStream().use { properties.load(it) }
properties.setProperty("VERSION", newVersion.removePrefix("v"))
File(versionFile).outputStream().use { properties.store(it, null) }
println(properties)
}
fun updateReadmeVersions(newVersion: String) {
try {
val readmeContent = File(readmeFile).readText()
val updatedContent = readmeContent.replace(
Regex("""dev\.iurysouza:modulegraph:\d+\.\d+\.\d+"""),
"dev.iurysouza:modulegraph:${newVersion.removePrefix("v")}",
)
File(readmeFile).writeText(updatedContent)
println("Updated version references in README to $newVersion")
} catch (e: IOException) {
println("An error occurred while updating README: ${e.message}")
exitProcess(1)
}
}
// Rollback functions for different failure scenarios
fun rollbackStaging(originalVersionContent: String) {
println("Rolling back staging...")
try {
runCommand("git", "restore", "--staged", versionFile)
File(versionFile).writeText(originalVersionContent)
println("Rollback: File unstaged and restored to original content")
} catch (e: IOException) {
println("Warning: Could not rollback staging: ${e.message}")
}
}
fun rollbackCommit(originalVersionContent: String) {
println("Rolling back commit...")
try {
runCommand("git", "reset", "--hard", "HEAD~1")
File(versionFile).writeText(originalVersionContent)
println("Rollback: Reset HEAD and restored version file")
} catch (e: IOException) {
println("Warning: Could not rollback commit: ${e.message}")
}
}
fun rollbackTagAndCommit(newVersion: String, originalVersionContent: String) {
println("Rolling back local tag and commit...")
try {
runCommand("git", "tag", "-d", newVersion)
runCommand("git", "reset", "--hard", "HEAD~1")
File(versionFile).writeText(originalVersionContent)
println("Rollback: Deleted local tag, reset HEAD, and restored version file")
} catch (e: IOException) {
println("Warning: Could not rollback tag and commit: ${e.message}")
}
}
fun rollbackCommitOnRemote(newVersion: String, originalVersionContent: String) {
println("Rolling back after remote commit push...")
try {
runCommand("git", "reset", "--hard", "HEAD~1")
File(versionFile).writeText(originalVersionContent)
println("WARNING: Commit was pushed to remote but tag creation failed.")
println("WARNING: Manual cleanup required - remote has the commit but no tag.")
println("Rollback: Reset local HEAD and restored version file")
} catch (e: IOException) {
println("Warning: Could not rollback local state: ${e.message}")
}
}
fun commitTagAndPush(newVersion: String) {
// Capture original state for rollback
val originalVersionContent = try {
File(versionFile).readText()
} catch (e: IOException) {
println("Error: Cannot read original version file for rollback")
throw e
}
var commitCreated = false
var commitPushed = false
var tagCreated = false
try {
println("Committing changes...")
// Step 1: git add (low risk - no rollback needed for this step)
runCommand("git", "add", versionFile, readmeFile)
// Step 2: git commit
runCommand("git", "commit", "-m", "Bump version to $newVersion")
commitCreated = true
// Step 3: git push
runCommand("git", "push")
commitPushed = true
// Step 4: git tag
runCommand("git", "tag", newVersion)
tagCreated = true
// Step 5: git push tag
runCommand("git", "push", "origin", newVersion)
println("Version updated to $newVersion and tag pushed")
} catch (e: IOException) {
println("Git operation failed: ${e.message}")
// Rollback based on what operations succeeded
when {
tagCreated && commitPushed -> rollbackTagAndCommit(newVersion, originalVersionContent)
commitPushed -> rollbackCommitOnRemote(newVersion, originalVersionContent)
commitCreated -> rollbackCommit(originalVersionContent)
else -> rollbackStaging(originalVersionContent)
}
println("Repository has been rolled back to previous state")
throw e
}
}
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)
}