From 39a356f82431d0c54d22a07208ce26a032e36433 Mon Sep 17 00:00:00 2001 From: garyschulte Date: Wed, 13 Mar 2024 10:52:41 -0700 Subject: [PATCH] proposed "develop" build versions (#6699) * proposed "develop" build versions Signed-off-by: garyschulte --- build.gradle | 58 +++++++++++++++++++++++++++-------------------- gradle.properties | 2 -- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/build.gradle b/build.gradle index 6ad1bee41d..17d2d317ff 100644 --- a/build.gradle +++ b/build.gradle @@ -119,7 +119,7 @@ allprojects { apply plugin: 'net.ltgt.errorprone' apply from: "${rootDir}/gradle/versions.gradle" - version = rootProject.version + version = calculateVersion() jacoco { toolVersion = '0.8.8' @@ -1038,42 +1038,50 @@ def buildTime() { return df.format(new Date()) } -// Takes the version, and if -SNAPSHOT is part of it replaces SNAPSHOT -// with the git commit version. @Memoized def calculateVersion() { - String version = rootProject.version - if (version.endsWith("-SNAPSHOT")) { - version = version.replace("-SNAPSHOT", "-dev-" + getCheckedOutGitCommitHash()) + // Regex pattern for basic calendar versioning, with provision to omit patch rev + def calVerPattern = ~/\d+\.\d+(\.\d+)?(-.*)?/ + + if (project.hasProperty('version') && (project.version =~ calVerPattern)) { + return "${project.version}" + } else { + // If no version is supplied or it doesn't match the semantic versioning, calculate from git + println("Generating project version as supplied is version not semver: ${project.version}") + def gitDetails = getGitCommitDetails(10) // Adjust length as needed + return "${gitDetails.date}-develop-${gitDetails.hash}" } - return version } -def getCheckedOutGitCommitHash(length = 8) { +def getGitCommitDetails(length = 8) { try { def gitFolder = "$projectDir/.git/" if (!file(gitFolder).isDirectory()) { - // We are in a submodule. The file's contents are `gitdir: \n`. - // Read the file, cut off the front, and trim the whitespace. gitFolder = file(gitFolder).text.substring(length).trim() + "/" } def takeFromHash = length - /* - * '.git/HEAD' contains either - * in case of detached head: the currently checked out commit hash - * otherwise: a reference to a file containing the current commit hash - */ - def head = new File(gitFolder + "HEAD").text.split(":") // .git/HEAD - def isCommit = head.length == 1 // e5a7c79edabbf7dd39888442df081b1c9d8e88fd - - if (isCommit) return head[0].trim().take(takeFromHash) // e5a7c79edabb - - def refHead = new File(gitFolder + head[1].trim()) // .git/refs/heads/master - refHead.text.trim().take takeFromHash + def head = new File(gitFolder + "HEAD").text.split(":") + def isCommit = head.length == 1 + + def commitHash, refHeadFile + if (isCommit) { + commitHash = head[0].trim().take(takeFromHash) + refHeadFile = new File(gitFolder + "HEAD") + } else { + refHeadFile = new File(gitFolder + head[1].trim()) + commitHash = refHeadFile.text.trim().take(takeFromHash) + } + + // Use head file modification time as a proxy for the build date + def lastModified = new Date(refHeadFile.lastModified()) + // Format the date as "yy.M" (e.g. 24.3 for March 2024) + def formattedDate = new SimpleDateFormat("yy.M").format(lastModified) + + return [hash: commitHash, date: formattedDate] } catch (Exception e) { - logger.warn('Could not calculate git commit, using "xxxxxxxx" (run with --info for stacktrace)') - logger.info('Error retrieving git commit', e) - return "xxxxxxxx" + logger.warn('Could not calculate git commit details, using defaults (run with --info for stacktrace)') + logger.info('Error retrieving git commit details', e) + return [hash: "xxxxxxxx", date: "00.0"] } } diff --git a/gradle.properties b/gradle.properties index cc51c2b730..265507cea4 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,3 @@ -version=24.2.0-SNAPSHOT - org.gradle.welcome=never # Set exports/opens flags required by Google Java Format and ErrorProne plugins. (JEP-396) org.gradle.jvmargs=-Xmx4g \