Scripts/convention/src/main/kotlin/jf.autoversion.gradle.kts

46 lines
1.6 KiB
Plaintext

import io.gitlab.jfronny.convention.*
import io.gitlab.jfronny.convention.ext.*
import org.eclipse.jgit.api.Git
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
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
val vt = when(versionS[0]) {
'v' -> VersionType.RELEASE
'b' -> VersionType.BETA
'a' -> VersionType.ALPHA
else -> null
}
versionType = vt ?: VersionType.RELEASE
if (vt != null) versionS = versionS.substring(1)
changelog += "Commits in $versionType $versionS:\n"
changelog += git.log(if (tags.size >= 2) tags[1].id else null, tags[0].id)
.reversed()
.joinToString("\n") { "- ${it.shortMessage}" }
}
}
}
if (changelog == "") changelog = "No io.gitlab.jfronny.scripts.getChangelog"
val timestamp: Long = if (project.hasProperty("timestamp")) "${project.property("timestamp")}".toLong()
else (System.currentTimeMillis() / 1000L)
if (project.hasProperty("release")) versionS += "-$timestamp"
println(changelog)
tasks.register("copyVersionNumber") {
doLast {
Toolkit.getDefaultToolkit().systemClipboard.setContents(StringSelection(versionS), null)
println("Copied version number: $versionS")
}
}