gradle: Use our own groovy code to compute a version from git

git worktree is not supported by androidgitversion

This makes it work with git worktrees and submodules, unlike the
plugin we used before. Unlike some other gradle plugins,
this allows us to set our own filter for tags that get included in
the version.

Thanks to Tao Pei for noticing the problem and proposing an initial fix.

Co-authored-by: tao.pei <tao.pei@xjmz.com>
Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/2204>
This commit is contained in:
Rylie Pavlik 2024-05-09 14:39:33 +08:00
parent 44552a1722
commit de9b9f79ea
2 changed files with 59 additions and 10 deletions

View file

@ -16,6 +16,7 @@
- mr.1377 - mr.1377
- mr.1385 - mr.1385
- mr.2232 - mr.2232
- mr.2204
--- ---
More improvements to the Android port. More improvements to the Android port.

View file

@ -1,4 +1,4 @@
// Copyright 2020-2022, Collabora, Ltd. // Copyright 2020-2024, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0 // SPDX-License-Identifier: BSL-1.0
import groovy.xml.XmlUtil import groovy.xml.XmlUtil
@ -8,7 +8,6 @@ plugins {
id 'com.mikepenz.aboutlibraries.plugin' id 'com.mikepenz.aboutlibraries.plugin'
id "com.gladed.androidgitversion"
id "de.undercouch.download" id "de.undercouch.download"
// Hilt dependency injection // Hilt dependency injection
@ -35,12 +34,6 @@ spotless {
} }
} }
androidGitVersion {
tagPattern(/^v[0-9]+.*/)
codeFormat = 'MMNPBBBBB'
format = '%tag%%-count%%-gcommit%%-dirty%'
}
def parseOpenXRVersion(def fn) { def parseOpenXRVersion(def fn) {
def matches = file(fn).readLines().find { def matches = file(fn).readLines().find {
it.contains('XR_CURRENT_API_VERSION') it.contains('XR_CURRENT_API_VERSION')
@ -97,6 +90,60 @@ if (!(new File(project.file(project.eigenIncludeDir), "Eigen/Core")).exists()) {
unpackEigen.enabled = false unpackEigen.enabled = false
} }
// ***
// Git version handling
// ***
// Supplied with major, minor, patch, commit count
def versionCodeFormatPattern = "%02d%01d%01d%05d"
// parsed using this regex
def gitDescribePattern = ~/v(?<tag>(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+))-(?<commits>\d+)-g(?<hash>[0-9a-f]+)/
// Get the version code from git describe output
def getVersionCode = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--always', '--long', '--match', 'v*'
standardOutput = stdout
workingDir rootDir
}
def longOutput = stdout.toString().trim()
def matcher = longOutput =~ gitDescribePattern
if (!matcher.matches()) {
println "Could not find an annotated git tag matching the regex!"
return null
}
def codeAsString = String.format(versionCodeFormatPattern,
Integer.parseInt(matcher.group('major')),
Integer.parseInt(matcher.group('minor')),
Integer.parseInt(matcher.group('patch')),
Integer.parseInt(matcher.group('commits')))
return Integer.parseInt(codeAsString)
}
catch (ignored) {
return null;
}
}
// Get the version string from git describe output
def getVersionString = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--always', '--dirty', '--match', 'v*'
standardOutput = stdout
workingDir rootDir
}
return stdout.toString().trim()
}
catch (ignored) {
return null;
}
}
// Pass -PsharedStl=true on the gradle command line to build with the shared runtime library. // Pass -PsharedStl=true on the gradle command line to build with the shared runtime library.
// This has potential compatibility issues and should only be used if you're consuming another // This has potential compatibility issues and should only be used if you're consuming another
// library that exposes a C++ interface. // library that exposes a C++ interface.
@ -117,10 +164,11 @@ android {
minSdkVersion project.sharedMinSdk minSdkVersion project.sharedMinSdk
targetSdkVersion project.sharedTargetSdk targetSdkVersion project.sharedTargetSdk
versionCode androidGitVersion.code() versionCode = getVersionCode()
versionName androidGitVersion.name() versionName = getVersionString()
println versionName println versionName
println versionCode
resValue "string", "monado_lib_version", "${versionName}" resValue "string", "monado_lib_version", "${versionName}"