forked from Darkrock-Studios/hammer-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
534 lines (471 loc) · 20.1 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
534 lines (471 loc) · 20.1 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import com.darkrockstudios.build.configureRelease
import com.darkrockstudios.build.extractLatestChangelog
import com.darkrockstudios.build.isPlatformReleaseTag
import com.darkrockstudios.build.registerLinuxDistributionTasks
import com.darkrockstudios.build.registerPublishTasks
import com.darkrockstudios.build.updateFlatpakFiles
import com.darkrockstudios.build.updateIosShortVersion
import com.darkrockstudios.build.updateSnapcraftYaml
import com.darkrockstudios.build.writeChangelogMarkdown
import com.darkrockstudios.build.writeSemvar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
group = "com.darkrockstudios.apps.hammer"
version = libs.versions.app.get()
buildscript {
repositories {
gradlePluginPortal()
mavenCentral()
}
dependencies {
classpath(libs.kotlinx.atomicfu.plugin)
classpath(libs.jetbrains.kover)
}
}
val xlibs = extensions.getByType<VersionCatalogsExtension>().named("libs")
allprojects {
repositories {
mavenLocal()
google()
mavenCentral()
maven("https://jitpack.io")
}
tasks.withType<Test> {
useJUnitPlatform()
}
// Compiler flags applied globally
tasks.withType<KotlinCompilationTask<*>>().configureEach {
compilerOptions {
freeCompilerArgs.addAll(
listOf(
"-Xexpect-actual-classes",
"-opt-in=kotlin.time.ExperimentalTime",
"-opt-in=androidx.compose.material.ExperimentalMaterialApi",
"-opt-in=androidx.compose.material3.ExperimentalMaterialApi",
"-opt-in=androidx.compose.runtime.ExperimentalComposeApi",
"-opt-in=com.arkivanov.decompose.ExperimentalDecomposeApi",
"-opt-in=androidx.compose.animation.ExperimentalSharedTransitionApi",
)
)
}
}
dependencies {
enforcedPlatform(xlibs.findLibrary("junit.bom").get())
}
}
plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.jetbrains.compose) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.kotlin.parcelize) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.android.kotlin.multiplatform.library) apply false
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.compose.compiler) apply false
//alias(libs.plugins.compose.report.generator) apply false
alias(libs.plugins.buildconfig) apply false
alias(libs.plugins.aboutlibraries.plugin) apply false
alias(libs.plugins.aboutlibraries.plugin.android) apply false
alias(libs.plugins.jetbrains.kover)
alias(libs.plugins.kotlinx.atomicfu)
}
dependencies {
//kover(project(":base"))
kover(project(":common"))
kover(project(":server"))
}
kover {
reports {
filters {
excludes {
// Generated Compose Multiplatform resource accessors.
classes(
"com.darkrockstudios.apps.hammer.String*",
"com.darkrockstudios.apps.hammer.Drawable*",
"com.darkrockstudios.apps.hammer.Font*",
"com.darkrockstudios.apps.hammer.Res",
"*ResourceCollectorsKt",
)
// Generated SQLDelight code: query wrappers and DB impls.
packages("com.darkrockstudios.apps.hammer.legacy")
classes(
"*Queries",
"*.ServerDatabaseImpl",
"*.LegacySqliteDatabaseImpl",
"*.LegacySqliteDatabaseImplKt",
)
}
}
total {
}
}
}
registerPublishTasks()
registerLinuxDistributionTasks(libs.versions.app.get())
val releasePreFlightChecks = tasks.register("releasePreFlightChecks") {
doLast {
fun runGit(vararg args: String): String {
val process = ProcessBuilder(*args)
.directory(project.rootDir)
.start()
val stdout = process.inputStream.bufferedReader().readText().trim()
process.waitFor()
return stdout
}
println("Fetching origin...")
ProcessBuilder("git", "fetch", "origin").directory(project.rootDir).inheritIO().start().waitFor()
// Check for unstaged/uncommitted changes
val statusText = runGit("git", "status", "--porcelain")
if (statusText.isNotEmpty()) {
error(
"Working tree has uncommitted changes. Please commit or stash them before preparing a release.\n$statusText"
)
}
// Check if develop is behind origin/develop
val developBehind = runGit("git", "rev-list", "--count", "develop..origin/develop").toIntOrNull() ?: 0
if (developBehind > 0) {
error("Local 'develop' is behind 'origin/develop' by $developBehind commit(s). Please pull before preparing a release.")
}
println("Pre-flight checks passed.")
}
}
tasks.register("prepareForRelease") {
dependsOn(releasePreFlightChecks)
doLast {
val lastReleaseChangelog = extractLatestChangelog(File("${project.rootDir}/CHANGELOG.md"))
val releaseInfo =
configureRelease(libs.versions.app.get(), lastReleaseChangelog)
?: error("Failed to configure new release")
println("Creating new release")
val versionCode = releaseInfo.semVar.createVersionCode(true, 0)
// Write the new version number
val versionsPath = "gradle/libs.versions.toml".replace("/", File.separator)
val versionsFile = project.rootDir.resolve(versionsPath)
writeSemvar(libs.versions.app.get(), releaseInfo.semVar, versionsFile)
// Google Play has a hard limit of 500 characters
val truncatedChangelog = if (releaseInfo.changeLog.length > 500) {
"${releaseInfo.changeLog.take(480)}... and more"
} else {
releaseInfo.changeLog
}
// Write the Fastlane changelog file
val rootDir: File = project.rootDir
val changelogsPath =
"fastlane/metadata/android/en-US/changelogs".replace("/", File.separator)
val changeLogsDir = rootDir.resolve(changelogsPath)
val changeLogFile = File(changeLogsDir, "$versionCode.txt")
changeLogFile.writeText(truncatedChangelog)
println("Changelog for version ${releaseInfo.semVar} written to $changelogsPath/$versionCode.txt")
// Apple App Store has a 4000 character limit for release notes
val truncatedAppleChangelog = if (releaseInfo.changeLog.length > 4000) {
"${releaseInfo.changeLog.take(3980)}... and more"
} else {
releaseInfo.changeLog
}
// Write the macOS App Store release notes
val macReleaseNotesPath = "fastlane/metadata/osx/en-US".replace("/", File.separator)
val macReleaseNotesDir = rootDir.resolve(macReleaseNotesPath)
macReleaseNotesDir.mkdirs()
val macReleaseNotesFile = File(macReleaseNotesDir, "release_notes.txt")
macReleaseNotesFile.writeText(truncatedAppleChangelog)
println("macOS release notes written to $macReleaseNotesPath/release_notes.txt")
// Write the iOS App Store release notes
val iosReleaseNotesPath = "fastlane/metadata/ios/en-US".replace("/", File.separator)
val iosReleaseNotesDir = rootDir.resolve(iosReleaseNotesPath)
iosReleaseNotesDir.mkdirs()
val iosReleaseNotesFile = File(iosReleaseNotesDir, "release_notes.txt")
iosReleaseNotesFile.writeText(truncatedAppleChangelog)
println("iOS release notes written to $iosReleaseNotesPath/release_notes.txt")
// Write the Global changelog file
val globalChangelogFile = File("${project.rootDir}/CHANGELOG.md")
writeChangelogMarkdown(releaseInfo, globalChangelogFile)
// Update snapcraft.yaml with new version and JVM version
val snapcraftPath = "snap/snapcraft.yaml".replace("/", File.separator)
val snapcraftFile = project.rootDir.resolve(snapcraftPath)
val jvmVersion = libs.versions.jvm.get()
updateSnapcraftYaml(releaseInfo.semVar, jvmVersion, snapcraftFile)
// Update Flatpak manifest and metainfo with new version and JVM version
val flatpakManifestPath = "flatpak/studio.darkrock.hammer.yaml".replace("/", File.separator)
val flatpakManifestFile = project.rootDir.resolve(flatpakManifestPath)
val flatpakMetainfoPath = "flatpak/studio.darkrock.hammer.metainfo.xml".replace("/", File.separator)
val flatpakMetainfoFile = project.rootDir.resolve(flatpakMetainfoPath)
updateFlatpakFiles(releaseInfo.semVar, jvmVersion, flatpakManifestFile, flatpakMetainfoFile, releaseInfo.changeLog)
// Keep the iOS marketing version in sync with the semver. macOS pulls this
// from Compose's packageVersion automatically; iOS has no such hook.
val iosInfoPlistPath = "ios/ios/Info.plist".replace("/", File.separator)
val iosInfoPlistFile = project.rootDir.resolve(iosInfoPlistPath)
updateIosShortVersion(releaseInfo.semVar, iosInfoPlistFile)
fun git(vararg args: String) {
val cmd = listOf("git") + args.toList()
println("> ${cmd.joinToString(" ")}")
val process = ProcessBuilder(cmd)
.directory(project.rootDir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (exitCode != 0) {
error("Git command failed: ${cmd.joinToString(" ")}\n${output.trim()}")
}
}
// Commit the changes to the repo
git("add", changeLogFile.absolutePath)
git("add", macReleaseNotesFile.absolutePath)
git("add", iosReleaseNotesFile.absolutePath)
git("add", versionsFile.absolutePath)
git("add", globalChangelogFile.absolutePath)
git("add", snapcraftFile.absolutePath)
git("add", flatpakManifestFile.absolutePath)
git("add", flatpakMetainfoFile.absolutePath)
git("add", iosInfoPlistFile.absolutePath)
git("commit", "-m", "Prepared for release: v${releaseInfo.semVar}")
// Merge develop into release in a throwaway worktree, never checking
// release out in the main tree: the running daemon holds gradle-wrapper.jar
// open, so a checkout that replaces it fails on Windows (unable to unlink).
fun gitIn(dir: File, vararg args: String): Int {
val cmd = listOf("git") + args.toList()
println("> (${dir.name}) ${cmd.joinToString(" ")}")
val process = ProcessBuilder(cmd)
.directory(dir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (output.isNotBlank()) println(output.trim())
return exitCode
}
git("branch", "-f", "release", "origin/release")
val releaseWorktree = File(project.rootDir, "build/release-merge")
// Clear any leftover worktree from a previous failed run (ignore failure).
gitIn(project.rootDir, "worktree", "remove", "--force", releaseWorktree.absolutePath)
if (gitIn(project.rootDir, "worktree", "add", releaseWorktree.absolutePath, "release") != 0) {
error("Failed to create release worktree at ${releaseWorktree.absolutePath}")
}
try {
if (gitIn(releaseWorktree, "merge", "-X", "theirs", "develop") != 0) {
error("Failed to merge develop into release")
}
} finally {
gitIn(project.rootDir, "worktree", "remove", "--force", releaseWorktree.absolutePath)
}
// Tag the merge commit explicitly; the main tree stays on develop.
git("tag", "-a", releaseInfo.tag, "-m", releaseInfo.changeLog, "release")
// Push the branches and only this release's tag. Pushing --tags would try
// to sync every stale local tag and fail when one already exists on origin.
git("push", "origin", "develop", "release")
git("push", "origin", "refs/tags/${releaseInfo.tag}")
}
}
tasks.register("backoutLastRelease") {
doLast {
val version = libs.versions.app.get()
val tagName = "v$version"
println("Attempting to back out release $tagName...")
fun gitSafe(vararg args: String): Boolean {
val cmd = listOf("git") + args.toList()
println("> ${cmd.joinToString(" ")}")
val process = ProcessBuilder(cmd)
.directory(project.rootDir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (exitCode != 0) {
println(" (failed: ${output.trim()})")
}
return exitCode == 0
}
// Lists every local tag for this version — both the bare `vX.Y.Z` (full
// release) and any `vX.Y.Z+platform+...` (partial release) variants.
// Filtered through `isPlatformReleaseTag` so unrelated tags that share
// the prefix (`vX.Y.Z+rc1`, `vX.Y.Z+sbom`) aren't included.
fun findReleaseTags(): List<String> {
val proc = ProcessBuilder("git", "tag", "-l", tagName, "$tagName+*")
.directory(project.rootDir)
.start()
val tags = proc.inputStream.bufferedReader().readLines().filter { it.isNotBlank() }
proc.waitFor()
return tags.filter { isPlatformReleaseTag(it, tagName) }
}
// Make sure we're on develop
gitSafe("checkout", "develop")
// Check if HEAD commit is the release commit
val headProcess = ProcessBuilder("git", "log", "-1", "--format=%s")
.directory(project.rootDir).start()
val headMessage = headProcess.inputStream.bufferedReader().readText().trim()
headProcess.waitFor()
if (headMessage == "Prepared for release: $tagName") {
println("Resetting develop to before release commit...")
gitSafe("reset", "--hard", "HEAD~1")
} else {
println("HEAD commit is not the release commit, discarding any uncommitted changes...")
println(" HEAD: $headMessage")
gitSafe("checkout", "--", ".")
}
// Delete every tag for this version (exact + any +platform suffixes)
val matchingTags = findReleaseTags()
if (matchingTags.isEmpty()) {
println("No local tags matching $tagName or $tagName+* found, skipping.")
} else {
matchingTags.forEach { tag ->
println("Deleting local tag $tag...")
gitSafe("tag", "-d", tag)
}
}
// Reset release branch to origin/release
println("Resetting release branch to origin/release...")
gitSafe("checkout", "release")
gitSafe("reset", "--hard", "origin/release")
// Return to develop
gitSafe("checkout", "develop")
println("Backout complete. Remote was NOT modified — if the push already went through, you'll need to force-push manually.")
}
}
tasks.register("revertLastRelease") {
doLast {
val version = libs.versions.app.get()
val tagName = "v$version"
println("Reverting release $tagName from local and remote...")
fun git(vararg args: String) {
val cmd = listOf("git") + args.toList()
println("> ${cmd.joinToString(" ")}")
val process = ProcessBuilder(cmd)
.directory(project.rootDir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (exitCode != 0) error("Git command failed: ${cmd.joinToString(" ")}\n${output.trim()}")
}
fun gitSafe(vararg args: String): Boolean {
val cmd = listOf("git") + args.toList()
println("> ${cmd.joinToString(" ")}")
val process = ProcessBuilder(cmd)
.directory(project.rootDir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText()
val exitCode = process.waitFor()
if (exitCode != 0) println(" (skipped: ${output.trim()})")
return exitCode == 0
}
fun gitOutput(vararg args: String): String {
val cmd = listOf("git") + args.toList()
val process = ProcessBuilder(cmd)
.directory(project.rootDir)
.redirectErrorStream(true)
.start()
val output = process.inputStream.bufferedReader().readText().trim()
val exitCode = process.waitFor()
// Throw on non-zero — otherwise a failing `ls-remote` (network
// blip, auth expiry) silently returns its error text on stdout and
// findReleaseTags() treats it as "no remote tags", skipping remote
// deletion and leaving the suffix tag on origin.
if (exitCode != 0) error("Git command failed: ${cmd.joinToString(" ")}\n${output}")
return output
}
// Lists every tag for this version on local OR remote — bare `vX.Y.Z`
// plus any `vX.Y.Z+platform+...` partial-release variants. Filtered
// through `isPlatformReleaseTag` so unrelated tags that share the
// prefix (`vX.Y.Z+rc1`, `vX.Y.Z+sbom`) aren't included.
fun findReleaseTags(): List<String> {
val local = gitOutput("tag", "-l", tagName, "$tagName+*").lines()
val remote = gitOutput("ls-remote", "--tags", "origin", tagName, "$tagName+*")
.lines()
.mapNotNull { line ->
// Format: "<sha>\trefs/tags/<name>" (and optionally "...^{}" for peeled tag refs)
line.substringAfter("refs/tags/", "").removeSuffix("^{}").ifBlank { null }
}
return (local + remote)
.filter { it.isNotBlank() }
.distinct()
.filter { isPlatformReleaseTag(it, tagName) }
}
git("fetch", "origin")
// Validate develop HEAD is the release commit we expect to undo
git("checkout", "develop")
val developHead = gitOutput("log", "-1", "--format=%s")
if (developHead != "Prepared for release: $tagName") {
error("develop HEAD is '$developHead', expected 'Prepared for release: $tagName'. Cannot safely revert.")
}
// Validate release HEAD is a merge commit (has 2 parents). Read the ref
// directly rather than checking it out — see below for why we must not
// materialize the release working tree.
val releaseParents = gitOutput("log", "-1", "--format=%P", "refs/heads/release").split("\\s+".toRegex()).filter { it.isNotEmpty() }
if (releaseParents.size < 2) {
error("release HEAD is not a merge commit. Cannot safely revert.")
}
// Reset release to its pre-merge state (first parent of the merge commit).
// Move the ref with `git branch -f` instead of `checkout release` +
// `reset --hard`: on Windows the running Gradle daemon holds
// gradle/wrapper/gradle-wrapper.jar open, and the merge changes that jar,
// so a working-tree reset fails with "unable to unlink ... Invalid argument".
// Updating the ref without ever checking release out never touches the file.
println("Resetting release to pre-merge state...")
git("branch", "-f", "release", releaseParents[0])
git("push", "--force", "origin", "release")
// Reset develop to before the release commit. We're still on develop and
// this commit doesn't touch the wrapper jar, so a hard reset is safe here.
println("Resetting develop to pre-release state...")
git("reset", "--hard", "HEAD~1")
git("push", "--force", "origin", "develop")
// Delete every tag for this version (exact + any +platform suffixes) from
// remote then local. Remote delete is tried first so a partial failure
// (network blip) doesn't leave us with a local tag that re-pushes later.
val matchingTags = findReleaseTags()
if (matchingTags.isEmpty()) {
println("No tags matching $tagName or $tagName+* found.")
} else {
matchingTags.forEach { tag ->
println("Deleting tag $tag from remote and local...")
gitSafe("push", "origin", "--delete", tag)
gitSafe("tag", "-d", tag)
}
}
// Delete GitHub draft release for this version if one exists.
val ghToken = System.getenv("GH_TOKEN") ?: System.getenv("GITHUB_TOKEN")
if (ghToken == null) {
println("No GH_TOKEN or GITHUB_TOKEN found — skipping GitHub draft release deletion.")
} else {
val repoSlug = "Wavesonics/hammer-editor"
println("Looking up GitHub release for $tagName...")
val lookupConn = java.net.URL("https://api.github.com/repos/$repoSlug/releases/tags/$tagName")
.openConnection() as java.net.HttpURLConnection
lookupConn.setRequestProperty("Authorization", "Bearer $ghToken")
lookupConn.setRequestProperty("Accept", "application/vnd.github+json")
lookupConn.setRequestProperty("X-GitHub-Api-Version", "2022-11-28")
when (val lookupStatus = lookupConn.responseCode) {
404 -> println("No GitHub release found for $tagName — nothing to delete.")
200 -> {
val body = lookupConn.inputStream.bufferedReader().readText()
val isDraft = body.contains("\"draft\":true")
if (!isDraft) {
println("GitHub release $tagName is not a draft — skipping deletion.")
} else {
val releaseId = Regex("\"id\":(\\d+)").find(body)?.groupValues?.get(1)
if (releaseId == null) {
println("Warning: Could not parse release ID from GitHub response.")
} else {
println("Deleting GitHub draft release $tagName (id=$releaseId)...")
val deleteConn = java.net.URL("https://api.github.com/repos/$repoSlug/releases/$releaseId")
.openConnection() as java.net.HttpURLConnection
deleteConn.requestMethod = "DELETE"
deleteConn.setRequestProperty("Authorization", "Bearer $ghToken")
deleteConn.setRequestProperty("Accept", "application/vnd.github+json")
deleteConn.setRequestProperty("X-GitHub-Api-Version", "2022-11-28")
val deleteStatus = deleteConn.responseCode
if (deleteStatus == 204) {
println("GitHub draft release $tagName deleted.")
} else {
println("Warning: Failed to delete GitHub release (HTTP $deleteStatus).")
}
}
}
}
else -> println("Warning: GitHub API returned HTTP $lookupStatus for $tagName.")
}
}
println("Done. $tagName has been fully reverted on local and remote.")
}
}