From 9e3300b397aca666524ce6149934111be84e7fd1 Mon Sep 17 00:00:00 2001 From: Bastien Paul Date: Wed, 13 Nov 2019 08:30:49 +0100 Subject: [PATCH 1/3] Update .gitignore --- .gitignore | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index cbfed34..415039f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,19 @@ -.gradle +# Build build/ -.idea -*.iml +/out + +# Mac .DS_Store userHome + +# IntelliJ +.idea +*.iml + +# Gradle +.gradletasknamecache +.gradle + +# Other +local.properties *.swp -/out From f00810c3a9d4a1c78018ebf58b7b5ec6d64d058e Mon Sep 17 00:00:00 2001 From: Bastien Paul Date: Thu, 14 Nov 2019 09:10:04 +0100 Subject: [PATCH 2/3] Adding versionCode information into VersionningExtension --- build.gradle | 2 +- .../nemerosa/versioning/VersionInfo.groovy | 1 + .../versioning/VersioningExtension.groovy | 54 +++++++++++++++++-- .../tasks/VersionDisplayTask.groovy | 25 ++++----- .../versioning/tasks/VersionFileTask.groovy | 1 + .../GitVersionReleaseBuildFalseTest.groovy | 45 +++++++++++++++- .../git/GitVersionReleaseBuildTrueTest.groovy | 44 +++++++++++++++ .../versioning/git/GitVersionTest.groovy | 48 +++++++++++++++++ .../versioning/svn/SVNVersionTest.groovy | 4 ++ 9 files changed, 207 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index e256547..3216ffe 100644 --- a/build.gradle +++ b/build.gradle @@ -61,10 +61,10 @@ dependencies { /** * Plug-in definition */ - pluginBundle { website = 'https://github.com/nemerosa/versioning/' vcsUrl = 'https://github.com/nemerosa/versioning/' + description = 'Gradle plug-in that computes version information from the SCM' tags = ['gradle', 'plugin', 'scm', 'git', 'svn', 'version'] } diff --git a/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy b/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy index 43297fd..07fa98c 100644 --- a/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy +++ b/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy @@ -20,6 +20,7 @@ class VersionInfo { String base = '' String build = '' String tag = null + int versionCode = 0 String lastTag = null boolean dirty = false boolean shallow = false diff --git a/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy b/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy index 33e152f..02506b6 100644 --- a/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy +++ b/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy @@ -6,6 +6,8 @@ import net.nemerosa.versioning.svn.SVNInfoService import org.gradle.api.GradleException import org.gradle.api.Project +import java.util.regex.Matcher + class VersioningExtension { /** @@ -151,6 +153,27 @@ class VersioningExtension { */ String lastTagPattern = /(\d+)$/ + /** + * Compute version code + * + * Closure that compute versionCode from scmInfo, versionReleaseType, versionBranchId, + * versionFull, versionBase and versionDisplay + * + * By default it tries to find this pattern in display : "\d{2}.\d{2}.\d{2}". Version code is computed with + * this algo : code = group(1) * 10000 + group(2) * 100 + group(3) + */ + Closure computeVersionCode = { SCMInfo scmInfo, String versionReleaseType, String versionBranchId, + String versionFull, String versionBase, String versionDisplay -> + String code = getVersionCodeFromName(versionDisplay) + int ret = 0 + try { + ret = code.toInteger() + } catch (NumberFormatException ignore) { + println("Version code is ${code}, use $ret as version code") + } + return ret + } + /** * Certificate - accept SSL server certificates from unknown certificate authorities (for SVN only) */ @@ -174,9 +197,10 @@ class VersioningExtension { VersioningExtension(Project project) { this.project = project } -/** - * Gets the computed version information - */ + + /** + * Gets the computed version information + */ VersionInfo getInfo() { if (!info) { info = computeInfo() @@ -247,6 +271,9 @@ class VersioningExtension { } } + int versionCode = computeVersionCode(scmInfo, versionReleaseType, versionBranchId, + versionFull, versionBase, versionDisplay) + // OK new VersionInfo( scm: scm, @@ -262,6 +289,7 @@ class VersioningExtension { lastTag: scmInfo.lastTag, dirty: scmInfo.dirty, shallow: scmInfo.shallow, + versionCode: versionCode, ) } @@ -317,4 +345,24 @@ class VersioningExtension { throw new GradleException("Unknown SCM info service: ${type}") } } + + /** + * Return a version code from a name. + * + * Version code is computed from name that contain this pattern : {digit}.{digit}.{digit}* If this pattern is not found, then it returns 0 + * @param name from which version code is determined + * @return version code + */ + static String getVersionCodeFromName(String name) { + Matcher m = (name =~ '([0-9]+)[.]([0-9]+)[.]([0-9]+)') + if (m.find()) { + int n1 = Integer.parseInt(m.group(1)) + int n2 = Integer.parseInt(m.group(2)) + int n3 = Integer.parseInt(m.group(3)) + int n = (n1 * 10000) + (n2 * 100) + n3 + return "$n" + } else { + return name + } + } } diff --git a/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy b/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy index 7458c11..014631e 100644 --- a/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy +++ b/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy @@ -22,18 +22,19 @@ class VersionDisplayTask extends DefaultTask { if (info == VersionInfo.NONE) { println "[version] No version can be computed from the SCM." } else { - println "[version] scm = ${info.scm}" - println "[version] branch = ${info.branch}" - println "[version] branchType = ${info.branchType}" - println "[version] branchId = ${info.branchId}" - println "[version] commit = ${info.commit}" - println "[version] full = ${info.full}" - println "[version] base = ${info.base}" - println "[version] build = ${info.build}" - println "[version] display = ${info.display}" - println "[version] tag = ${info.tag ?: ''}" - println "[version] lastTag = ${info.lastTag ?: ''}" - println "[version] dirty = ${info.dirty}" + println "[version] scm = ${info.scm}" + println "[version] branch = ${info.branch}" + println "[version] branchType = ${info.branchType}" + println "[version] branchId = ${info.branchId}" + println "[version] commit = ${info.commit}" + println "[version] full = ${info.full}" + println "[version] base = ${info.base}" + println "[version] build = ${info.build}" + println "[version] display = ${info.display}" + println "[version] tag = ${info.tag ?: ''}" + println "[version] lastTag = ${info.lastTag ?: ''}" + println "[version] dirty = ${info.dirty}" + println "[version] versionCode = ${info.versionCode}" } } diff --git a/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy b/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy index 0843eb1..d0e2199 100644 --- a/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy +++ b/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy @@ -47,6 +47,7 @@ ${prefix}SCM=${info.scm} ${prefix}TAG=${info.tag ?: ''} ${prefix}LAST_TAG=${info.lastTag ?: ''} ${prefix}DIRTY=${info.dirty} +${prefix}VERSIONCODE=${info.versionCode} """ } diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy index 9020eca..ef8e390 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy @@ -36,6 +36,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } @Test @@ -67,6 +68,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -118,6 +120,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -164,6 +167,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -209,6 +213,7 @@ class GitVersionReleaseBuildFalseTest { assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -275,6 +280,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -319,6 +325,7 @@ CUSTOM_SCM=git CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false +CUSTOM_VERSIONCODE=0 """ as String } finally { repo.close() @@ -363,6 +370,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -400,6 +408,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -438,6 +447,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -476,6 +486,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -515,6 +526,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -553,6 +565,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -593,6 +606,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -630,6 +644,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -668,6 +683,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -707,6 +723,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -750,6 +767,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -793,6 +811,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -836,6 +855,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -879,6 +899,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -918,6 +939,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20011 } finally { repo.close() @@ -957,6 +979,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -995,6 +1018,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1032,6 +1056,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1071,6 +1096,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1112,6 +1138,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1152,6 +1179,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1192,6 +1220,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1233,6 +1262,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1273,6 +1303,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1313,6 +1344,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1353,7 +1385,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty - + assert info.versionCode == 0 } finally { repo.close() } @@ -1396,6 +1428,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1438,6 +1471,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1479,6 +1513,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1522,6 +1557,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1571,6 +1607,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -1615,6 +1652,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20004 } finally { repo.close() @@ -1663,6 +1701,7 @@ VERSION_DIRTY=false assert info.tag == '2.0.2' assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -1710,6 +1749,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1753,6 +1793,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1822,6 +1863,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1864,6 +1906,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20003 } finally { repo.close() diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy index a5c0ca2..eddcf13 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy @@ -36,6 +36,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } @Test @@ -67,6 +68,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -118,6 +120,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -164,6 +167,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -209,6 +213,7 @@ class GitVersionReleaseBuildTrueTest { assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -275,6 +280,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -319,6 +325,7 @@ CUSTOM_SCM=git CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false +CUSTOM_VERSIONCODE=0 """ as String } finally { repo.close() @@ -363,6 +370,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -400,6 +408,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -438,6 +447,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -476,6 +486,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -515,6 +526,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -553,6 +565,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -593,6 +606,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -630,6 +644,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -668,6 +683,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -707,6 +723,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -750,6 +767,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -793,6 +811,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -836,6 +855,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -879,6 +899,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -918,6 +939,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20011 } finally { repo.close() @@ -957,6 +979,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -995,6 +1018,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1032,6 +1056,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1071,6 +1096,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1112,6 +1138,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1152,6 +1179,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1192,6 +1220,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20002 } finally { repo.close() @@ -1233,6 +1262,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20002 } finally { repo.close() @@ -1273,6 +1303,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1313,6 +1344,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1353,6 +1385,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1396,6 +1429,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1438,6 +1472,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1479,6 +1514,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1522,6 +1558,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1571,6 +1608,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -1615,6 +1653,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20004 } finally { repo.close() @@ -1663,6 +1702,7 @@ VERSION_DIRTY=false assert info.tag == '2.0.2' assert !info.dirty assert info.shallow + assert info.versionCode == 20002 } finally { detached.deleteDir() @@ -1710,6 +1750,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1753,6 +1794,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1822,6 +1864,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1864,6 +1907,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20003 } finally { repo.close() diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy index 5a87d03..1645e21 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy @@ -33,6 +33,7 @@ class GitVersionTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } @Test @@ -61,6 +62,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -109,6 +111,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -152,6 +155,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -194,6 +198,7 @@ class GitVersionTest { assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -254,6 +259,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -295,6 +301,7 @@ CUSTOM_SCM=git CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false +CUSTOM_VERSIONCODE=0 """ as String } finally { repo.close() @@ -336,6 +343,7 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ as String } finally { repo.close() @@ -370,6 +378,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -407,6 +416,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -444,6 +454,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -482,6 +493,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -519,6 +531,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -558,6 +571,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -592,6 +606,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -627,6 +642,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -663,6 +679,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -703,6 +720,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -743,6 +761,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -783,6 +802,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -823,6 +843,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -859,6 +880,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20011 } finally { repo.close() @@ -897,6 +919,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -934,6 +957,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -970,6 +994,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1008,6 +1033,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1048,6 +1074,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1087,6 +1114,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1126,6 +1154,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20002 } finally { repo.close() @@ -1166,6 +1195,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20002 } finally { repo.close() @@ -1205,6 +1235,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1242,6 +1273,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1279,6 +1311,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1319,6 +1352,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1360,6 +1394,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1398,6 +1433,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1440,6 +1476,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1486,6 +1523,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert info.shallow + assert info.versionCode == 0 } finally { detached.deleteDir() @@ -1527,6 +1565,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20004 } finally { repo.close() @@ -1572,6 +1611,7 @@ VERSION_DIRTY=false assert info.tag == '2.0.2' assert !info.dirty assert info.shallow + assert info.versionCode == 20002 } finally { detached.deleteDir() @@ -1602,6 +1642,10 @@ VERSION_DIRTY=false def project = ProjectBuilder.builder().withProjectDir(repo.dir).build() new VersioningPlugin().apply(project) + project.versioning { + displayMode = 'full' + releaseMode = 'tag' + } project.versioning { dirty = { version -> "${version}-DONOTUSE" } } @@ -1618,6 +1662,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert info.dirty + assert info.versionCode == 20003 } finally { repo.close() @@ -1660,6 +1705,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty + assert info.versionCode == 20000 } finally { repo.close() } @@ -1727,6 +1773,7 @@ VERSION_DIRTY=false assert info.scm == 'git' assert info.tag == null assert !info.dirty + assert info.versionCode == 0 } finally { repo.close() @@ -1768,6 +1815,7 @@ VERSION_DIRTY=false assert info.tag == null assert !info.dirty assert !info.shallow + assert info.versionCode == 20003 } finally { repo.close() diff --git a/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy b/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy index 35fcd39..dc447b5 100644 --- a/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy @@ -8,6 +8,7 @@ import org.gradle.api.DefaultTask import org.gradle.testfixtures.ProjectBuilder import org.junit.After import org.junit.Before +import org.junit.Ignore import org.junit.Test import java.util.concurrent.atomic.AtomicInteger @@ -134,6 +135,7 @@ VERSION_SCM=svn VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ } @@ -174,6 +176,7 @@ CUSTOM_SCM=svn CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false +CUSTOM_VERSIONCODE=0 """ } @@ -214,6 +217,7 @@ VERSION_SCM=svn VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false +VERSION_VERSIONCODE=0 """ } From d4964c952f41d5eb905d5b639800d94009186e85 Mon Sep 17 00:00:00 2001 From: Bastien Paul Date: Thu, 21 Nov 2019 09:28:44 +0100 Subject: [PATCH 3/3] Adding version number information into VersionningExtension --- README.md | 108 ++++++++++- .../nemerosa/versioning/ReleaseInfo.groovy | 22 +++ .../nemerosa/versioning/SCMInfoService.groovy | 26 +++ .../nemerosa/versioning/VersionInfo.groovy | 2 +- .../nemerosa/versioning/VersionNumber.groovy | 50 +++++ .../versioning/VersioningExtension.groovy | 106 ++++++---- .../tasks/VersionDisplayTask.groovy | 6 +- .../versioning/tasks/VersionFileTask.groovy | 6 +- .../GitVersionReleaseBuildFalseTest.groovy | 94 +++++---- .../git/GitVersionReleaseBuildTrueTest.groovy | 94 +++++---- .../versioning/git/GitVersionTest.groovy | 181 ++++++++++++++---- .../versioning/svn/SVNVersionTest.groovy | 12 ++ 12 files changed, 541 insertions(+), 166 deletions(-) create mode 100644 src/main/groovy/net/nemerosa/versioning/VersionNumber.groovy diff --git a/README.md b/README.md index 981b22d..95be0df 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,12 @@ Property | Description | Git: `master` | Git: `feature/great` | Git: `release/2. `tag` (1) | Current tag | (2) | (2) | (2) `lastTag` (1) | Last tag | (4) | (4) | (4) `dirty` | Current state of the working copy | (3) | (3) | (3) +`versionNumber` | Version number containing major, minor, patch, qualifier and versionCode | | | +`versionNumber.major` | Major version | 0 | 0 | 2 +`versionNumber.minor` | Minor version | 0 | 0 | 0 +`versionNumber.patch` | Patch version | 0 | 0 | 0, 1, 2, ... +`versionNumber.qualifier` | Version qualifier (alpha, beta, engineer, ...)| '' | '' | '' +`versionNumber.versionCode` | Version code | 0 | 0 | 20000, 20001, 20002, ... (1) not supported for Subversion (2) will be the name of the current tag if any, or `null` if no tag is associated to the current `HEAD`. @@ -119,6 +125,18 @@ For branches to type `release`, an additional computation occurs: By using the `display` version when tagging a release, the `display` version will be automatically incremented, patch after patch, using the `release` base at a prefix. +### Version number + +Version number is a container of several numbers computed from `display` by default . It is hosting major, minor, patch, +qualifier and versionCode. + +- In a tag like `1.2.3`, then major is `1`, minor is `2` and patch is `3` +- Qualifier are taken from tags formatted like `1.2-beta.0` where qualifier is `-beta` here +- Version code is a integer computed from major, minor and patch version. + - `1.2.3` will give 10203 + - `21.5.16` will give 210516 + - `2.0-alpha.0` will give 20000 + ## Tasks The `versioning` plug-in provides two tasks. @@ -142,6 +160,11 @@ Displays the version information in the standard output. For example: [version] tag = [version] lastTag = 0.2.0 [version] dirty = false +[version] versionCode = 0 +[version] major = 0 +[version] minor = 0 +[version] patch = 0 +[version] qualifier = ``` ### `versionFile` @@ -163,6 +186,11 @@ VERSION_SCM=git VERSION_TAG= VERSION_LAST_TAG=0.2.0 VERSION_DIRTY=false +VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= ``` This makes this file easy to integrate in a Bash script: @@ -202,7 +230,7 @@ versioning { * present, the type is the branch and the base is empty. * F.e. if you want use tag name instead of branch you may provide something like: */ - releaseParser = { scmInfo, separator = '/' -> -> + releaseParser = { scmInfo, separator = '/' -> List part = scmInfo.tag.split('/') + '' new net.nemerosa.versioning.ReleaseInfo(type: part[0], base: part[1]) } @@ -226,7 +254,6 @@ versioning { * the tags in Git. */ lastTagPattern = /(\d+)$/ -*/ } ``` @@ -387,6 +414,83 @@ versioning { } } ``` +### Version number + +Version number computation can be customised by setting some properties in the `versioning` extension. + +```groovy +versioning { + /** + * Digit precision for computing version code. + * + * With a precision of 2, 1.25.3 will become 12503. + * With a precision of 3, 1.25.3 will become 1250003. + */ + int precision = 2 + + /** + * Default number to use when no version number can be extracted from version string. + */ + int defaultNumber = 0 + + /** + * Closure that takes major, minor and patch integers in parameter and is computing versionCode number. + */ + Closure computeVersionCode = { int major, int minor, int patch -> + return (major * 10**(2 * precision)) + (minor * 10**precision) + patch + } + + /** + * Compute version number + * + * Closure that compute VersionNumber from scmInfo, versionReleaseType, versionBranchId, + * versionFull, versionBase and versionDisplay + * + * By default it tries to find this pattern in display : '([0-9]+)[.]([0-9]+)[.]([0-9]+)(.*)$'. + * Version code is computed with this algo : code = group(1) * 10^2precision + group(2) * 10^precision + group(3) + * + * Example : + * + * - with precision = 2 + * + * 1.2.3 -> 10203 + * 10.55.62 -> 105562 + * 20.3.2 -> 200302 + * + * - with precision = 3 + * + * 1.2.3 -> 1002003 + * 10.55.62 -> 100055062 + * 20.3.2 -> 20003002 + **/ + Closure parseVersionNumber = { SCMInfo scmInfo, String versionReleaseType, String versionBranchId, + String versionFull, String versionBase, String versionDisplay -> + // We are specifying all these parameters because we want to leave the choice to the developer + // to use data that's right to him + // Regex explained : + // - 1st group one digit that is major version + // - 2nd group one digit that is minor version + // - It can be followed by a qualifier name + // - 3rd group and last part is one digit that is patch version + Matcher m = (versionDisplay =~ '([0-9]+)[.]([0-9]+).*[.]([0-9]+)(.*)$') + if (m.find()) { + try { + int n1 = Integer.parseInt(m.group(1)) + int n2 = Integer.parseInt(m.group(2)) + int n3 = Integer.parseInt(m.group(3)) + String q = m.group(4) ?: '' + return new VersionNumber(n1, n2, n3, q, computeVersionCode(n1, n2, n3).intValue(), versionDisplay) + } catch (Exception ignore) { + // Should never go here + return new VersionNumber(0, 0, 0, '', defaultNumber, versionDisplay) + } + } else { + return new VersionNumber(0, 0, 0, '', defaultNumber, versionDisplay) + } + } + +} +``` ## Detached and shallow clone support diff --git a/src/main/groovy/net/nemerosa/versioning/ReleaseInfo.groovy b/src/main/groovy/net/nemerosa/versioning/ReleaseInfo.groovy index efca227..fb100e7 100644 --- a/src/main/groovy/net/nemerosa/versioning/ReleaseInfo.groovy +++ b/src/main/groovy/net/nemerosa/versioning/ReleaseInfo.groovy @@ -2,10 +2,32 @@ package net.nemerosa.versioning import groovy.transform.Canonical +/** + * Information about the branch coming from its name. + * + * For instance : + * + * release/2.0 + * type : 'release' + * base : '2.0' + * + * master + * type : 'master' + * base : '' + */ @Canonical class ReleaseInfo { + /** + * Type of branch. + * + * Could be "release", "feature" or what ever has been configured + */ String type + + /** + * Base of release or feature branch. This is what comes after type in branch's name. + */ String base } diff --git a/src/main/groovy/net/nemerosa/versioning/SCMInfoService.groovy b/src/main/groovy/net/nemerosa/versioning/SCMInfoService.groovy index 2108592..b16bb96 100644 --- a/src/main/groovy/net/nemerosa/versioning/SCMInfoService.groovy +++ b/src/main/groovy/net/nemerosa/versioning/SCMInfoService.groovy @@ -4,11 +4,37 @@ import org.gradle.api.Project interface SCMInfoService { + /** + * Get SCM Info + * @param project Gradle's project + * @param extension Plugin data holder + * @return Information got from scm + */ SCMInfo getInfo(Project project, VersioningExtension extension) + /** + * Get last tags + * + * @param project Gradle's project + * @param extension Plugin data holder + * @param tagPattern Tag pattern + * @return List of tags + */ List getLastTags(Project project, VersioningExtension extension, String tagPattern) + /** + * Get base tags + * + * @param project Gradle's project + * @param extension Plugin data holder + * @param base Base of branch in case where name of branch is something/base + * @return List of base tags + */ List getBaseTags(Project project, VersioningExtension extension, String base) + /** + * + * @return Separator used for separating branch type and branch base + */ String getBranchTypeSeparator() } diff --git a/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy b/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy index 07fa98c..c4a2b0e 100644 --- a/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy +++ b/src/main/groovy/net/nemerosa/versioning/VersionInfo.groovy @@ -20,9 +20,9 @@ class VersionInfo { String base = '' String build = '' String tag = null - int versionCode = 0 String lastTag = null boolean dirty = false boolean shallow = false + VersionNumber versionNumber = null } diff --git a/src/main/groovy/net/nemerosa/versioning/VersionNumber.groovy b/src/main/groovy/net/nemerosa/versioning/VersionNumber.groovy new file mode 100644 index 0000000..f41a901 --- /dev/null +++ b/src/main/groovy/net/nemerosa/versioning/VersionNumber.groovy @@ -0,0 +1,50 @@ +package net.nemerosa.versioning + +/** + * Representation of a version number. + * + * This is generally a combination of three digits with an optional qualifier. + * These digits are major, minor and path numbers. + * + * This representation is one of many interpretation of version number format. It is let to developer's appreciation. + */ +class VersionNumber { + /** + * Major version + */ + final int major + + /** + * Minor version + */ + final int minor + + /** + * Patch version + */ + final int patch + + /** + * Optional qualifier + */ + final String qualifier + + /** + * version code + */ + final int versionCode + + /** + * Original full version string + */ + final String versionString + + VersionNumber(int major, int minor, int patch, String qualifier, int versionCode, String versionString) { + this.major = major + this.minor = minor + this.patch = patch + this.qualifier = qualifier + this.versionCode = versionCode + this.versionString = versionString + } +} diff --git a/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy b/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy index 02506b6..760c7a6 100644 --- a/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy +++ b/src/main/groovy/net/nemerosa/versioning/VersioningExtension.groovy @@ -1,5 +1,6 @@ package net.nemerosa.versioning + import net.nemerosa.versioning.git.GitInfoService import net.nemerosa.versioning.support.DirtyException import net.nemerosa.versioning.svn.SVNInfoService @@ -154,24 +155,72 @@ class VersioningExtension { String lastTagPattern = /(\d+)$/ /** - * Compute version code + * Digit precision for computing version code. * - * Closure that compute versionCode from scmInfo, versionReleaseType, versionBranchId, + * With a precision of 2, 1.25.3 will become 12503. + * With a precision of 3, 1.25.3 will become 1250003. + */ + int precision = 2 + + /** + * Default number to use when no version number can be extracted from version string. + */ + int defaultNumber = 0 + + /** + * Closure that takes major, minor and patch integers in parameter and is computing versionCode number. + */ + Closure computeVersionCode = { int major, int minor, int patch -> + return (major * 10**(2 * precision)) + (minor * 10**precision) + patch + } + + /** + * Compute version number + * + * Closure that compute VersionNumber from scmInfo, versionReleaseType, versionBranchId, * versionFull, versionBase and versionDisplay * - * By default it tries to find this pattern in display : "\d{2}.\d{2}.\d{2}". Version code is computed with - * this algo : code = group(1) * 10000 + group(2) * 100 + group(3) - */ - Closure computeVersionCode = { SCMInfo scmInfo, String versionReleaseType, String versionBranchId, - String versionFull, String versionBase, String versionDisplay -> - String code = getVersionCodeFromName(versionDisplay) - int ret = 0 - try { - ret = code.toInteger() - } catch (NumberFormatException ignore) { - println("Version code is ${code}, use $ret as version code") + * By default it tries to find this pattern in display : '([0-9]+)[.]([0-9]+)[.]([0-9]+)(.*)$'. + * Version code is computed with this algo : code = group(1) * 10^2precision + group(2) * 10^precision + group(3) + * + * Example : + * + * - with precision = 2 + * + * 1.2.3 -> 10203 + * 10.55.62 -> 105562 + * 20.3.2 -> 200302 + * + * - with precision = 3 + * + * 1.2.3 -> 1002003 + * 10.55.62 -> 100055062 + * 20.3.2 -> 20003002 + **/ + Closure parseVersionNumber = { SCMInfo scmInfo, String versionReleaseType, String versionBranchId, + String versionFull, String versionBase, String versionDisplay -> + // We are specifying all these parameters because we want to leave the choice to the developer + // to use data that's right to him + // Regex explained : + // - 1st group one digit that is major version + // - 2nd group one digit that is minor version + // - It can be followed by a qualifier name + // - 3rd group and last part is one digit that is patch version + Matcher m = (versionDisplay =~ '([0-9]+)[.]([0-9]+).*[.]([0-9]+)(.*)$') + if (m.find()) { + try { + int n1 = Integer.parseInt(m.group(1)) + int n2 = Integer.parseInt(m.group(2)) + int n3 = Integer.parseInt(m.group(3)) + String q = m.group(4) ?: '' + return new VersionNumber(n1, n2, n3, q, computeVersionCode(n1, n2, n3).intValue(), versionDisplay) + } catch (Exception ignore) { + // Should never go here + return new VersionNumber(0, 0, 0, '', defaultNumber, versionDisplay) + } + } else { + return new VersionNumber(0, 0, 0, '', defaultNumber, versionDisplay) } - return ret } /** @@ -271,9 +320,8 @@ class VersioningExtension { } } - int versionCode = computeVersionCode(scmInfo, versionReleaseType, versionBranchId, - versionFull, versionBase, versionDisplay) - + VersionNumber versionNumber = parseVersionNumber( + scmInfo, versionReleaseType, versionBranchId, versionFull, versionBase, versionDisplay) // OK new VersionInfo( scm: scm, @@ -289,7 +337,7 @@ class VersioningExtension { lastTag: scmInfo.lastTag, dirty: scmInfo.dirty, shallow: scmInfo.shallow, - versionCode: versionCode, + versionNumber: versionNumber, ) } @@ -309,6 +357,8 @@ class VersioningExtension { } else { String lastTag String nextTag + //FIXME remove this log after debugging + println "Base tags : $baseTags" if (baseTags.empty) { lastTag = '' nextTag = "${releaseInfo.base}.0" @@ -345,24 +395,4 @@ class VersioningExtension { throw new GradleException("Unknown SCM info service: ${type}") } } - - /** - * Return a version code from a name. - * - * Version code is computed from name that contain this pattern : {digit}.{digit}.{digit}* If this pattern is not found, then it returns 0 - * @param name from which version code is determined - * @return version code - */ - static String getVersionCodeFromName(String name) { - Matcher m = (name =~ '([0-9]+)[.]([0-9]+)[.]([0-9]+)') - if (m.find()) { - int n1 = Integer.parseInt(m.group(1)) - int n2 = Integer.parseInt(m.group(2)) - int n3 = Integer.parseInt(m.group(3)) - int n = (n1 * 10000) + (n2 * 100) + n3 - return "$n" - } else { - return name - } - } } diff --git a/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy b/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy index 014631e..1fecda6 100644 --- a/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy +++ b/src/main/groovy/net/nemerosa/versioning/tasks/VersionDisplayTask.groovy @@ -34,7 +34,11 @@ class VersionDisplayTask extends DefaultTask { println "[version] tag = ${info.tag ?: ''}" println "[version] lastTag = ${info.lastTag ?: ''}" println "[version] dirty = ${info.dirty}" - println "[version] versionCode = ${info.versionCode}" + println "[version] versionCode = ${info.versionNumber.versionCode}" + println "[version] major = ${info.versionNumber.major}" + println "[version] minor = ${info.versionNumber.minor}" + println "[version] patch = ${info.versionNumber.patch}" + println "[version] qualifier = ${info.versionNumber.qualifier}" } } diff --git a/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy b/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy index d0e2199..d56f3d7 100644 --- a/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy +++ b/src/main/groovy/net/nemerosa/versioning/tasks/VersionFileTask.groovy @@ -47,7 +47,11 @@ ${prefix}SCM=${info.scm} ${prefix}TAG=${info.tag ?: ''} ${prefix}LAST_TAG=${info.lastTag ?: ''} ${prefix}DIRTY=${info.dirty} -${prefix}VERSIONCODE=${info.versionCode} +${prefix}VERSIONCODE=${info.versionNumber.versionCode} +${prefix}MAJOR=${info.versionNumber.major} +${prefix}MINOR=${info.versionNumber.minor} +${prefix}PATCH=${info.versionNumber.patch} +${prefix}QUALIFIER=${info.versionNumber.qualifier} """ } diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy index ef8e390..1a5b96d 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildFalseTest.groovy @@ -36,7 +36,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber == null } @Test @@ -68,7 +68,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -120,7 +120,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -167,7 +167,7 @@ class GitVersionReleaseBuildFalseTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -213,7 +213,7 @@ class GitVersionReleaseBuildFalseTest { assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -281,6 +281,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -326,6 +330,10 @@ CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false CUSTOM_VERSIONCODE=0 +CUSTOM_MAJOR=0 +CUSTOM_MINOR=0 +CUSTOM_PATCH=0 +CUSTOM_QUALIFIER= """ as String } finally { repo.close() @@ -371,6 +379,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -408,7 +420,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -447,7 +459,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -486,7 +498,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -526,7 +538,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -565,7 +577,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -606,7 +618,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -644,7 +656,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -683,7 +695,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -723,7 +735,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -767,7 +779,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30002 } finally { repo.close() @@ -811,7 +823,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30011 } finally { repo.close() @@ -855,7 +867,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30021 } finally { repo.close() @@ -899,7 +911,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -939,7 +951,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20011 + assert info.versionNumber.versionCode == 20011 } finally { repo.close() @@ -979,7 +991,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1018,7 +1030,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1056,7 +1068,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1096,7 +1108,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1138,7 +1150,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1179,7 +1191,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1220,7 +1232,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1262,7 +1274,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1303,7 +1315,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1344,7 +1356,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1385,7 +1397,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() } @@ -1428,7 +1440,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1471,7 +1483,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1513,7 +1525,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1557,7 +1569,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1607,7 +1619,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -1652,7 +1664,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20004 + assert info.versionNumber.versionCode == 20004 } finally { repo.close() @@ -1701,7 +1713,7 @@ VERSION_VERSIONCODE=0 assert info.tag == '2.0.2' assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -1749,7 +1761,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1793,7 +1805,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1863,7 +1875,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1906,7 +1918,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy index eddcf13..7253585 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionReleaseBuildTrueTest.groovy @@ -36,7 +36,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber == null } @Test @@ -68,7 +68,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -120,7 +120,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -167,7 +167,7 @@ class GitVersionReleaseBuildTrueTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -213,7 +213,7 @@ class GitVersionReleaseBuildTrueTest { assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -281,6 +281,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -326,6 +330,10 @@ CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false CUSTOM_VERSIONCODE=0 +CUSTOM_MAJOR=0 +CUSTOM_MINOR=0 +CUSTOM_PATCH=0 +CUSTOM_QUALIFIER= """ as String } finally { repo.close() @@ -371,6 +379,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -408,7 +420,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -447,7 +459,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -486,7 +498,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -526,7 +538,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -565,7 +577,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -606,7 +618,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -644,7 +656,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -683,7 +695,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -723,7 +735,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -767,7 +779,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30002 } finally { repo.close() @@ -811,7 +823,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30011 } finally { repo.close() @@ -855,7 +867,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30021 } finally { repo.close() @@ -899,7 +911,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -939,7 +951,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20011 + assert info.versionNumber.versionCode == 20011 } finally { repo.close() @@ -979,7 +991,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1018,7 +1030,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1056,7 +1068,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1096,7 +1108,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1138,7 +1150,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1179,7 +1191,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1220,7 +1232,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { repo.close() @@ -1262,7 +1274,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { repo.close() @@ -1303,7 +1315,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1344,7 +1356,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1385,7 +1397,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1429,7 +1441,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1472,7 +1484,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1514,7 +1526,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1558,7 +1570,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1608,7 +1620,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -1653,7 +1665,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20004 + assert info.versionNumber.versionCode == 20004 } finally { repo.close() @@ -1702,7 +1714,7 @@ VERSION_VERSIONCODE=0 assert info.tag == '2.0.2' assert !info.dirty assert info.shallow - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { detached.deleteDir() @@ -1750,7 +1762,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1794,7 +1806,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1864,7 +1876,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1907,7 +1919,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() diff --git a/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy b/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy index 1645e21..3f6ad90 100644 --- a/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/git/GitVersionTest.groovy @@ -3,6 +3,7 @@ package net.nemerosa.versioning.git import net.nemerosa.versioning.ReleaseInfo import net.nemerosa.versioning.SCMInfo import net.nemerosa.versioning.VersionInfo +import net.nemerosa.versioning.VersionNumber import net.nemerosa.versioning.VersioningPlugin import net.nemerosa.versioning.support.DirtyException import net.nemerosa.versioning.tasks.VersionDisplayTask @@ -11,6 +12,8 @@ import org.gradle.api.DefaultTask import org.gradle.testfixtures.ProjectBuilder import org.junit.Test +import java.util.regex.Matcher + class GitVersionTest { @Test @@ -33,7 +36,7 @@ class GitVersionTest { assert info.scm == 'n/a' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber == null } @Test @@ -62,7 +65,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -111,7 +114,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -155,7 +158,7 @@ class GitVersionTest { assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -198,7 +201,7 @@ class GitVersionTest { assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -260,6 +263,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -302,6 +309,10 @@ CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false CUSTOM_VERSIONCODE=0 +CUSTOM_MAJOR=0 +CUSTOM_MINOR=0 +CUSTOM_PATCH=0 +CUSTOM_QUALIFIER= """ as String } finally { repo.close() @@ -344,6 +355,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ as String } finally { repo.close() @@ -378,7 +393,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -416,7 +431,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -454,7 +469,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -493,7 +508,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -531,7 +546,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -571,7 +586,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -606,7 +621,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -642,7 +657,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -679,7 +694,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -720,7 +735,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30002 } finally { repo.close() @@ -761,7 +776,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30011 } finally { repo.close() @@ -802,7 +817,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 30021 } finally { repo.close() @@ -843,7 +858,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -880,7 +895,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20011 + assert info.versionNumber.versionCode == 20011 } finally { repo.close() @@ -919,7 +934,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -957,7 +972,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -994,7 +1009,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1033,7 +1048,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1074,7 +1089,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1114,7 +1129,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1154,7 +1169,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { repo.close() @@ -1195,7 +1210,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { repo.close() @@ -1235,7 +1250,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == '2.0.2' assert !info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1273,7 +1288,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1311,7 +1326,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1352,7 +1367,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1394,7 +1409,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1433,7 +1448,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1476,7 +1491,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1523,7 +1538,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert info.shallow - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { detached.deleteDir() @@ -1565,7 +1580,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20004 + assert info.versionNumber.versionCode == 20004 } finally { repo.close() @@ -1611,7 +1626,7 @@ VERSION_VERSIONCODE=0 assert info.tag == '2.0.2' assert !info.dirty assert info.shallow - assert info.versionCode == 20002 + assert info.versionNumber.versionCode == 20002 } finally { detached.deleteDir() @@ -1662,7 +1677,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert info.dirty - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -1705,7 +1720,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == 'release/v2.0' assert !info.dirty - assert info.versionCode == 20000 + assert info.versionNumber.versionCode == 20000 } finally { repo.close() } @@ -1773,7 +1788,7 @@ VERSION_VERSIONCODE=0 assert info.scm == 'git' assert info.tag == null assert !info.dirty - assert info.versionCode == 0 + assert info.versionNumber.versionCode == 0 } finally { repo.close() @@ -1815,7 +1830,7 @@ VERSION_VERSIONCODE=0 assert info.tag == null assert !info.dirty assert !info.shallow - assert info.versionCode == 20003 + assert info.versionNumber.versionCode == 20003 } finally { repo.close() @@ -2000,4 +2015,88 @@ VERSION_VERSIONCODE=0 repo.close() } } + + @Test + void 'Git release branch: with precision = 3'() { + GitRepo repo = new GitRepo() + try { + // Git initialisation + repo.with { + (1..4).each { commit it } + branch 'release/2.0' + commit 5 + tag '2.0.10' + commit 6 + } + def head = repo.commitLookup('Commit 6') + def headAbbreviated = repo.commitLookup('Commit 6', true) + + def project = ProjectBuilder.builder().withProjectDir(repo.dir).build() + new VersioningPlugin().apply(project) + project.versioning { + precision = 3 + } + VersionInfo info = project.versioning.info as VersionInfo + assert info != null + assert info.build == headAbbreviated + assert info.branch == 'release/2.0' + assert info.base == '2.0' + assert info.branchId == 'release-2.0' + assert info.branchType == 'release' + assert info.commit == head + assert info.display == '2.0.11' + assert info.full == "release-2.0-${headAbbreviated}" as String + assert info.scm == 'git' + assert info.tag == null + assert !info.dirty + assert info.versionNumber.versionCode == 2000011 + + } finally { + repo.close() + } + } + + + @Test + void 'Git release branch with custom versionNumber computing'() { + GitRepo repo = new GitRepo() + try { + // Git initialisation + repo.with { + (1..4).each { commit it } + branch 'release/2.0' + commit 5 + tag '2.0.10' + } + def head = repo.commitLookup('Commit 5') + def headAbbreviated = repo.commitLookup('Commit 5', true) + + def project = ProjectBuilder.builder().withProjectDir(repo.dir).build() + new VersioningPlugin().apply(project) + project.versioning { + parseVersionNumber = { SCMInfo scmInfo, String versionReleaseType, String versionBranchId, + String versionFull, String versionBase, String versionDisplay -> + + return new VersionNumber(1, 2, 3, 'toto', 4, 'tata') + } + } + VersionInfo info = project.versioning.info as VersionInfo + assert info != null + assert info.build == headAbbreviated + assert info.branch == 'release/2.0' + assert info.base == '2.0' + assert info.branchId == 'release-2.0' + assert info.branchType == 'release' + assert info.commit == head + assert info.display == '2.0.11' + assert info.full == "release-2.0-${headAbbreviated}" as String + assert info.scm == 'git' + assert info.tag == '2.0.10' + assert !info.dirty + assert info.versionNumber.versionCode == 4 + + } finally { + repo.close() + } + } } diff --git a/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy b/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy index dc447b5..165ac36 100644 --- a/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy +++ b/src/test/groovy/net/nemerosa/versioning/svn/SVNVersionTest.groovy @@ -136,6 +136,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ } @@ -177,6 +181,10 @@ CUSTOM_TAG= CUSTOM_LAST_TAG= CUSTOM_DIRTY=false CUSTOM_VERSIONCODE=0 +CUSTOM_MAJOR=0 +CUSTOM_MINOR=0 +CUSTOM_PATCH=0 +CUSTOM_QUALIFIER= """ } @@ -218,6 +226,10 @@ VERSION_TAG= VERSION_LAST_TAG= VERSION_DIRTY=false VERSION_VERSIONCODE=0 +VERSION_MAJOR=0 +VERSION_MINOR=0 +VERSION_PATCH=0 +VERSION_QUALIFIER= """ }