From 47c14f9c28116f4a0e0e6d85c618dcd4b30571bb Mon Sep 17 00:00:00 2001 From: JFronny Date: Thu, 10 Oct 2024 10:17:12 +0200 Subject: [PATCH] feat(autoversion): special casing for mods specifying mod loaders in their build --- .../gitlab/jfronny/scripts/SemanticVersion.kt | 14 +++++--- .../src/main/kotlin/jf.autoversion.gradle.kts | 32 ++++++++++++++----- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt index 44f2cb3..bfbcbf1 100644 --- a/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt +++ b/convention/src/main/kotlin/io/gitlab/jfronny/scripts/SemanticVersion.kt @@ -18,11 +18,13 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t override fun toString(): String { return "$major.$minor.$patch" + when (type) { - VersionType.RELEASE -> typeSupplement?.let { "-$it" } ?: "" - else -> "-${type.semanticName}" + if(typeSupplement != null) ".$typeSupplement" else "" - } + if (build == null) "" else "+$build" + VersionType.RELEASE -> typeSupplement.possiblyPrefix("-") + else -> "-${type.semanticName}" + typeSupplement.possiblyPrefix(".") + } + build.possiblyPrefix("+") } + private fun String?.possiblyPrefix(prefix: String) = if (this == null) "" else "$prefix$this" + fun unclassifiedToString(): String { return "$major.$minor.$patch" + if (build == null) "" else "+$build" } @@ -45,6 +47,10 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t private val restrictedSemver = Regex("$versionCore(?:-(alpha|beta|rc|pre)(\\.?[a-zA-Z0-9-]+)?)?(\\+$buildPattern)?") fun parse(source: String): SemanticVersion { + return tryParse(source) ?: throw IllegalArgumentException("Source does not match supported version patterns: $source") + } + + fun tryParse(source: String): SemanticVersion? { val legacyMatch = legacyVersion.matchEntire(source) if (legacyMatch != null) { val m = legacyMatch.groupValues @@ -65,7 +71,7 @@ data class SemanticVersion(val major: Int, val minor: Int, val patch: Int, val t m[6].ifEmpty { null }?.substring(1) ) } - throw IllegalArgumentException("Source does not match supported version patterns: $source") + return null } } } diff --git a/convention/src/main/kotlin/jf.autoversion.gradle.kts b/convention/src/main/kotlin/jf.autoversion.gradle.kts index 5cf43ff..8640a9d 100644 --- a/convention/src/main/kotlin/jf.autoversion.gradle.kts +++ b/convention/src/main/kotlin/jf.autoversion.gradle.kts @@ -4,32 +4,48 @@ import kotlin.jvm.optionals.getOrDefault val isRelease = project.hasProperty("release") +val specialCaseBuilds = setOf("forge", "fabric", "neoforge") +fun List.filter(): Pair? { + if (isEmpty()) return null + if (size == 1) return first() to null + + val ver = SemanticVersion.tryParse(first().name) + // special case: some mod projects use the format 1.0.0+forge to classify builds for different loaders + // this ensures changelogs get generated within the same loader branch + if (ver?.build in specialCaseBuilds) { + val preceding = drop(1).firstOrNull { SemanticVersion.tryParse(it.name)?.build == ver?.build } + if (preceding != null) return first() to preceding + } + + return first() to get(1) +} + versionS = "0.0.0+nogit" versionType = VersionType.ALPHA if (File(projectDir, ".git").exists()) { initializeGit() Git.open(projectDir).use { git -> versionS = "0.0.0+notag" - val tags: List = git.getTags() - if (tags.isNotEmpty()) { - if (tags[0].fullMessage != null) { - changelog += "${tags[0].fullMessage}\n" - changelogHtml += "

${tags[0].fullMessage}

\n" + val tags = git.getTags().filter() + if (tags != null) { + if (tags.first.fullMessage != null) { + changelog += "${tags.first.fullMessage}\n" + changelogHtml += "

${tags.first.fullMessage}

\n" } - versionS = tags[0].name + versionS = tags.first.name lastRelease = versionS val parsedVersion = SemanticVersion.parse(versionS) versionType = parsedVersion.type versionS = parsedVersion.unclassifiedToString() if (isRelease) { changelog += "Commits in ${versionType.displayName} $versionS:\n" - val log = git.log(if (tags.size >= 2) tags[1].peeledId else null, tags[0].peeledId).reversed() + val log = git.log(tags.second?.peeledId, tags.first.peeledId).reversed() changelog += log.joinToString("\n") { "- ${it.shortMessage}" } changelogHtml += "
    \n" + log.joinToString("") { "
  • ${it.shortMessage}
  • \n" } + "
" nextRelease = parsedVersion } else { changelog += "Commits after ${versionType.displayName} $versionS:\n" - val log = git.log(tags[0].peeledId, git.repository.resolve("HEAD")).reversed() + val log = git.log(tags.first.peeledId, git.repository.resolve("HEAD")).reversed() changelog += log.joinToString("\n") { "- ${it.shortMessage}" } changelogHtml += "
    \n" + log.joinToString("") { "
  • ${it.shortMessage}
  • \n" } + "
" val type = log.stream()