2022-10-07 16:03:38 +02:00
|
|
|
import io.gitlab.jfronny.scripts.*
|
2022-10-06 19:30:52 +02:00
|
|
|
import org.eclipse.jgit.api.Git
|
|
|
|
import java.awt.Toolkit
|
|
|
|
import java.awt.datatransfer.StringSelection
|
2023-06-29 12:34:44 +02:00
|
|
|
import kotlin.jvm.optionals.getOrDefault
|
2022-10-06 19:30:52 +02:00
|
|
|
|
2022-11-04 19:14:40 +01:00
|
|
|
val isRelease = project.hasProperty("release")
|
|
|
|
|
2022-10-06 19:30:52 +02:00
|
|
|
versionS = "0.0.0+nogit"
|
|
|
|
versionType = VersionType.ALPHA
|
|
|
|
if (File(projectDir, ".git").exists()) {
|
|
|
|
Git.open(projectDir).use { git ->
|
|
|
|
versionS = "0.0.0+notag"
|
|
|
|
val tags: List<Tag> = git.getTags()
|
|
|
|
if (tags.isNotEmpty()) {
|
|
|
|
if (tags[0].fullMessage != null) changelog += "${tags[0].fullMessage}\n"
|
|
|
|
versionS = tags[0].name
|
2023-06-29 12:34:44 +02:00
|
|
|
val parsedVersion = SemanticVersion.parse(versionS)
|
|
|
|
versionType = parsedVersion.type
|
|
|
|
versionS = parsedVersion.unclassifiedToString()
|
|
|
|
lastRelease = versionS
|
2022-11-04 19:14:40 +01:00
|
|
|
if (isRelease) {
|
|
|
|
changelog += "Commits in ${versionType.displayName} $versionS:\n"
|
2022-12-07 20:27:27 +01:00
|
|
|
changelog += git.log(if (tags.size >= 2) tags[1].peeledId else null, tags[0].peeledId)
|
2022-11-04 19:14:40 +01:00
|
|
|
.reversed()
|
|
|
|
.joinToString("\n") { "- ${it.shortMessage}" }
|
2023-06-29 12:34:44 +02:00
|
|
|
nextRelease = parsedVersion
|
2022-11-04 19:14:40 +01:00
|
|
|
} else {
|
|
|
|
changelog += "Commits after ${versionType.displayName} $versionS:\n"
|
2023-06-29 12:34:44 +02:00
|
|
|
val log = git.log(tags[0].peeledId, git.repository.resolve("HEAD")).reversed()
|
|
|
|
changelog += log.joinToString("\n") { "- ${it.shortMessage}" }
|
|
|
|
val type = log.stream()
|
|
|
|
.map { it.fullMessage }
|
|
|
|
.map { msg -> CommitType.from(msg) { logger.warn(it) } }
|
|
|
|
.max { o1, o2 -> o1.compareTo(o2) }
|
|
|
|
.getOrDefault(CommitType.FIX)
|
|
|
|
nextRelease = parsedVersion.incrementBy(type)
|
2022-11-04 19:14:40 +01:00
|
|
|
}
|
2022-11-25 16:51:40 +01:00
|
|
|
} else {
|
|
|
|
changelog += "Commits after inception:\n"
|
|
|
|
changelog += git.log().all().call()
|
|
|
|
.reversed()
|
|
|
|
.joinToString("\n") { "- ${it.shortMessage}" }
|
2022-10-06 19:30:52 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-25 16:51:40 +01:00
|
|
|
} else changelog = "No changelog"
|
2022-10-06 19:30:52 +02:00
|
|
|
|
2022-11-04 19:14:40 +01:00
|
|
|
if (!isRelease) {
|
2023-06-29 12:34:44 +02:00
|
|
|
versionS = "$nextRelease-SNAPSHOT"
|
2022-10-13 19:27:02 +02:00
|
|
|
}
|
2022-10-06 19:30:52 +02:00
|
|
|
|
|
|
|
println(changelog)
|
|
|
|
|
|
|
|
tasks.register("copyVersionNumber") {
|
2023-06-29 12:34:44 +02:00
|
|
|
description = "Copy the current version number to the system clipboard"
|
2022-10-06 19:30:52 +02:00
|
|
|
doLast {
|
|
|
|
Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(versionS), null)
|
|
|
|
println("Copied version number: $versionS")
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 12:34:44 +02:00
|
|
|
|
|
|
|
tasks.register("bumpVersion") {
|
|
|
|
description = "Bump the version by parsing commits since the last tag and creating a new tag based on them"
|
|
|
|
doLast {
|
|
|
|
if (!File(projectDir, ".git").exists()) throw IllegalStateException("Cannot bump without repository")
|
|
|
|
if (isRelease) throw IllegalStateException("Cannot bump while 'release' is set")
|
|
|
|
if (!project.hasProperty("versionType")) throw IllegalStateException("bumpVersion requires you to set -PversionType=release|beta|alpha")
|
|
|
|
val vt = VersionType.byName(prop("versionType")) ?: throw IllegalStateException("Unrecognized version type")
|
|
|
|
val name = nextRelease!!.withType(vt).toString()
|
|
|
|
Git.open(projectDir).use { git ->
|
|
|
|
git.tag().setName(name).call()
|
|
|
|
logger.warn("Created release $name (last was $lastRelease). Make sure to push it!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|