diff --git a/build.gradle.kts b/build.gradle.kts index 07435a8f7..bace77b58 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import java.util.Date import java.text.SimpleDateFormat import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream +import kotlin.text.Regex val mod_version: String by project val mc_version: String by project @@ -11,7 +12,6 @@ val mod_id: String by project val handle_deps: String by project val use_commit_hash_in_version: String by project val handleDeps = handle_deps == "true" -val useCommitHashInVersion = use_commit_hash_in_version == "true" plugins { java @@ -26,10 +26,41 @@ configurations { get("implementation").extendsFrom(get("library"), get("klibrary")) } -version = if (useCommitHashInVersion) getCommitVersion() else mod_version +data class GitInfo(val version: String, val count: String, val tag: String) { + // val tagIsVersion: Boolean get() = tag != "" && tag.matches(Regex("v[0-9]+\\.[0-9]\\.[0-9]")) + + val mainVersion: String get() = if (tag != "") tag else mod_version + + val jarName: String get() { + if (count != "") { + return "$mainVersion-SNAPSHOT-${version}_$count" + } + + if (version != "") { + return "$mainVersion-SNAPSHOT-$version" + } else { + return "$mainVersion-SNAPSHOT" + } + } + + val modVersion: String get() { + if (tag != "") + return mod_version + + if (version != "") { + return "$mainVersion-SNAPSHOT-$version" + } else { + return "$mainVersion-SNAPSHOT" + } + } +} + +val gitVersion = getCommitVersion() ?: GitInfo("", "", "") + +version = gitVersion.modVersion group = "ru.dbotthepony" -fun getCommitVersion(): String { +fun getCommitVersion(): GitInfo? { try { val versionStream = FastByteArrayOutputStream() val tagStream = FastByteArrayOutputStream() @@ -53,33 +84,21 @@ fun getCommitVersion(): String { standardOutput = tagStream }.exitValue == 0 + if (!gotVersion || !gotCount || !gotTag) { + return null + } + val version = versionStream.array.copyOfRange(0, versionStream.length).toString(Charsets.UTF_8).trim() val tag = tagStream.array.copyOfRange(0, tagStream.length).toString(Charsets.UTF_8).trim() val count = countStream.array.copyOfRange(0, countStream.length).toString(Charsets.UTF_8).trim() - if (!gotVersion) { - return mod_version - } - - if (gotTag && tag != "") { - if (gotCount && count != "") { - return "$tag-SNAPSHOT-${version}_$count" - } else { - return "$tag-SNAPSHOT-$version" - } - } - - if (gotCount && count != "") { - return "$mod_version-SNAPSHOT-${version}_$count" - } - - return "$mod_version-SNAPSHOT-$version" + return GitInfo(version, count, tag) } catch(err: Throwable) { println("Error getting git version") println(err) } - return mod_version + return null } java.toolchain.languageVersion.set(JavaLanguageVersion.of(17)) @@ -268,7 +287,7 @@ fun org.gradle.jvm.tasks.Jar.attachManifest() { "Specification-Vendor" to "DBotThePony", "Specification-Version" to "1", // We are version 1 of ourselves "Implementation-Title" to project.name, - "Implementation-Version" to tasks.jar.get().archiveVersion, + "Implementation-Version" to gitVersion.modVersion, "Implementation-Vendor" to "DBotThePony", "Implementation-Timestamp" to SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(Date()) )) @@ -281,6 +300,7 @@ tasks.jar.configure { from(configurations["library"].map { if (it.isDirectory) it else zipTree(it) }) finalizedBy("reobfJar") attachManifest() + archiveVersion.set(gitVersion.jarName) } tasks {