From 99d767d1b0988756a0666e010f1b1ab83b4b464f Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Tue, 31 Mar 2015 15:54:23 +0200 Subject: [PATCH] Gradle: create build info in assets folder while build - no compile error within Eclipse - with helper function in AppUtils --- .gitignore | 1 + .../sailing/android/shared/util/AppUtils.java | 33 +++++++++ mobile/git.gradle | 70 +++++++++++-------- mobile/git.template.xml | 6 -- 4 files changed, 76 insertions(+), 34 deletions(-) delete mode 100644 mobile/git.template.xml diff --git a/.gitignore b/.gitignore index deadb797fb8..5b4b102919c 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ out/ .gradle local.properties git.xml +build.info diff --git a/mobile/com.sap.sailing.android.shared/src/com/sap/sailing/android/shared/util/AppUtils.java b/mobile/com.sap.sailing.android.shared/src/com/sap/sailing/android/shared/util/AppUtils.java index 17a4adc5834..ba2efc14a31 100644 --- a/mobile/com.sap.sailing.android.shared/src/com/sap/sailing/android/shared/util/AppUtils.java +++ b/mobile/com.sap.sailing.android.shared/src/com/sap/sailing/android/shared/util/AppUtils.java @@ -3,8 +3,20 @@ package com.sap.sailing.android.shared.util; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; public class AppUtils { + + private final static String TAG = AppUtils.class.getName(); + + private AppUtils() { + + } + public static PackageInfo getPackageInfo(Context app) { try { return app.getPackageManager().getPackageInfo(app.getPackageName(), 0); @@ -12,4 +24,25 @@ public class AppUtils { return null; } } + + public static String getBuildInfo(Context context) { + String buildInfo = ""; + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(context.getAssets().open("build.info"))); + buildInfo = reader.readLine(); + } catch (IOException e) { + Log.d(TAG, "Can't open file with build info", e); + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + return buildInfo; + } } diff --git a/mobile/git.gradle b/mobile/git.gradle index 8e885cd9f95..85987cd78b5 100644 --- a/mobile/git.gradle +++ b/mobile/git.gradle @@ -1,30 +1,44 @@ -/** - * returns current git hash - */ -def getGitHash = { -> - def stdout = new ByteArrayOutputStream() - exec { - commandLine "git", "log", "-n", "1", "--format='%h'" - standardOutput = stdout - } - return stdout.toString().trim().replace("'", "") -} - -/** - * set git hash into git.xml - */ preBuild.doLast { - copy { - from ("../git.template.xml") - - into ("res/values") - - rename { - String fileName -> fileName.replace(".template", "") - } - - filter { - String line -> line.replaceAll("#GITHASH#", getGitHash()) - } + // get the current commit hash in git + def git_version = new ByteArrayOutputStream() + exec { + commandLine 'git', 'log', '-1', '--format=%h' + standardOutput = git_version } -} \ No newline at end of file + git_version = git_version.toString().trim() + + // get the current git branch, if any + def git_branch = new ByteArrayOutputStream() + exec { + commandLine 'git', 'symbolic-ref', '--short', '-q', 'HEAD' + standardOutput = git_branch + // ignore error output as we might not be on a branch + ignoreExitValue = true + } + + // if we are not on a branch, try to get a tag for the commit + def git_branch_or_tag + if (git_branch.size() > 0) { + git_branch_or_tag = git_branch.toString().trim() + } else { + def git_tag = new ByteArrayOutputStream() + exec { + commandLine 'git', 'describe', '--tags', '--exact-match' + standardOutput = git_tag + // ignore error output + errorOutput = new ByteArrayOutputStream() + ignoreExitValue = true + } + + git_branch_or_tag = git_tag.toString().trim() + } + + def build_time = new Date().toString() + + // save the combined build info into assets/build.info file + def result_line = git_branch_or_tag + "-" + git_version + " (" + + build_time + ")\n" + def assetsDir = android.sourceSets.main.assets.srcDirs.toArray()[0] + def buildInfoFile = new File(assetsDir, 'build.info').getAbsolutePath() + new File(buildInfoFile).write(result_line) +} diff --git a/mobile/git.template.xml b/mobile/git.template.xml deleted file mode 100644 index 4be58d9576f..00000000000 --- a/mobile/git.template.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - #GITHASH# - - \ No newline at end of file