From 92f4010e8ee16f0ebebad91e9d217fa20e2e0418 Mon Sep 17 00:00:00 2001 From: Stypox Date: Mon, 2 Mar 2020 20:50:35 +0100 Subject: [PATCH] Add more checks to prevent build failures in gradle branch suffix - Add function `getGitWorkingBranch` that returns the current working branch, and "" if it could not be determined (either because git is not installed or because the directory is not a git repo). - Make sure normalizedWorkingBranch is not empty (leading to an invalid app id terminating with `.`) - Make normalizedWorkingBranch lowercase - Add comments --- app/build.gradle | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 2329a7a0e..61929173e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,6 +3,24 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' + +static String getGitWorkingBranch() { + try { + def gitProcess = "git rev-parse --abbrev-ref HEAD".execute() + gitProcess.waitFor() + if (gitProcess.exitValue() == 0) { + return gitProcess.text.trim() + } else { + // not a git repository + return "" + } + } catch (IOException ignored) { + // git was not found + return "" + } +} + + android { compileSdkVersion 28 buildToolsVersion '28.0.3' @@ -31,12 +49,14 @@ android { debuggable true // suffix the app id and the app name with git branch name - def workingBranch = "git rev-parse --abbrev-ref HEAD".execute().text.trim() - if (workingBranch.isEmpty() || workingBranch == "master" || workingBranch == "dev") { + def workingBranch = getGitWorkingBranch() + def normalizedWorkingBranch = workingBranch.replaceAll("[^A-Za-z]+", "").toLowerCase() + if (normalizedWorkingBranch.isEmpty() || workingBranch == "master" || workingBranch == "dev") { + // default values when branch name could not be determined or is master or dev applicationIdSuffix ".debug" resValue "string", "app_name", "NewPipe Debug" } else { - applicationIdSuffix ".debug." + workingBranch.replaceAll("[^A-Za-z]+", "") + applicationIdSuffix ".debug." + normalizedWorkingBranch resValue "string", "app_name", "NewPipe " + workingBranch } }