From 132f486868e39b14ebdb7be228d5bc56caf73b3c Mon Sep 17 00:00:00 2001 From: Brian Guertin Date: Fri, 15 Jul 2016 13:05:10 -0400 Subject: [PATCH] Use Git branches/tags for versioning --- .travis.yml | 6 ++++ VideoLocker/build.gradle | 72 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0522e92e9d..ff94e2587c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,6 +27,12 @@ notifications: email: true before_script: + - git fetch --tags + - git fetch --unshallow || true + - export DESCRIBE=$(git describe) + - export PR=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST + - export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo `curl -s $PR | jq -r .head.ref`; fi) + - echo "TRAVIS_BRANCH=$TRAVIS_BRANCH, PR=$PR, BRANCH=$BRANCH, DESCRIBE=$DESCRIBE" - chmod +x gradlew # ARM architecture is used instead of x86 (which is 10x faster) of the lack of support from CI due # to complications of creating a virtual machine within a virtual machine. This may be solved diff --git a/VideoLocker/build.gradle b/VideoLocker/build.gradle index 47164160b5..20d61e5f69 100644 --- a/VideoLocker/build.gradle +++ b/VideoLocker/build.gradle @@ -49,6 +49,74 @@ class AndroidHelper { } } } +/** + * Computes a semantic version string, e.g. "1.0" or "2.5.7" by parsing git branches or tags. + * Branch name takes priority, otherwise the last annotated tag name will be used. + * @return The semantic version string, or "0.0.1" if we failed to determine one. + */ +def getVersionName = { -> + def description = "0.0.1"; + try { + def branch = System.getenv("BRANCH") + if (null == branch || branch.isEmpty()) { + def branchout = new ByteArrayOutputStream() + exec { + commandLine 'git', 'rev-parse', '--abbrev-ref', 'HEAD' + standardOutput = branchout + } + branch = branchout.toString().trim() + } + + def hasSemanticVersion = { s -> + // This branch/tag contains a semantic version (e.g. "rc/2.6.0" or "release/2.5") + return s.indexOf("rc/") >= 0 || s.indexOf("release/") >= 0 + } + if (hasSemanticVersion(branch)) { + description = branch; + } else { + def stdout = new ByteArrayOutputStream() + exec { + commandLine 'git', 'describe' + standardOutput = stdout + } + def describe = stdout.toString().trim() + if (hasSemanticVersion(describe)) { + description = describe; + } + } + } + catch (e) { + logger.error("Could not determine version name", e) + } + return description.substring(description.indexOf("/") + 1).split("-")[0].trim() +} + +/** + * @return An integer representation of the string returned by getVersionName() + */ +def getVersionCode = { -> + try { + def versionName = getVersionName() + def semVer = versionName.split('\\.') + def vCode; + vCode = semVer[0].toInteger() * 1000000 // Major version + if (semVer.length > 1) { + vCode += semVer[1].toInteger() * 1000 // Minor version + if (semVer.length > 2) { + vCode += semVer[2].toInteger() * 1 // Patch version + } + } + return vCode + } catch (e) { + logger.error("Could not determine version code", e) + return 1; + } +} + +task(version) << { + println String.format('%s (%s)', getVersionName(), getVersionCode()) +} + dependencies { compile project (':android-iconify') @@ -168,8 +236,8 @@ android { minSdkVersion 14 targetSdkVersion 21 - versionCode 90 - versionName "2.6.0" + versionCode getVersionCode() + versionName getVersionName() renderscriptTargetApi 22 renderscriptSupportModeEnabled true