Skip to content
Snippets Groups Projects
Commit 820f3d4d authored by Sebastian's avatar Sebastian
Browse files

Make git branch available in BuildData, too

parent 52fb09a6
1 merge request!90Add plugin in buildSrc to provide some build metadata to app at runtime
...@@ -43,7 +43,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -43,7 +43,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
String version = project.getVersion().toString(); String version = project.getVersion().toString();
/* /*
* Get Git hash and last commit time using JGit * Get Git hash, last commit time and current branch using JGit
*/ */
// Open git repository // Open git repository
File dir = project.getProjectDir(); File dir = project.getProjectDir();
...@@ -65,8 +65,21 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -65,8 +65,21 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
// Convert from seconds to milliseconds // Convert from seconds to milliseconds
long commitTime = first.getCommitTime() * 1000L; long commitTime = first.getCommitTime() * 1000L;
// Get current branch, if any
String gitBranch = "<unknown>";
String prefix = "refs/heads/";
String fullBranch = repository.getFullBranch();
if (fullBranch.startsWith(prefix)) {
gitBranch = fullBranch.substring(prefix.length());
}
/*
* Generate output file
*/
if (packageName == null) { if (packageName == null) {
throw new IllegalStateException("Please specify 'packageName'."); throw new IllegalStateException(
"Please specify 'packageName'.");
} }
String[] parts = packageName.split("\\."); String[] parts = packageName.split("\\.");
...@@ -83,7 +96,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -83,7 +96,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
Path file = path.resolve(className + ".java"); Path file = path.resolve(className + ".java");
String content = createSource(packageName, className, version, String content = createSource(packageName, className, version,
commitTime, gitHash); commitTime, gitHash, gitBranch);
InputStream in = new ByteArrayInputStream( InputStream in = new ByteArrayInputStream(
content.getBytes(StandardCharsets.UTF_8)); content.getBytes(StandardCharsets.UTF_8));
...@@ -101,7 +114,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -101,7 +114,7 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
} }
private String createSource(String packageName, String className, private String createSource(String packageName, String className,
String version, long gitTime, String gitHash) { String version, long gitTime, String gitHash, String gitBranch) {
FileBuilder buffer = new FileBuilder(); FileBuilder buffer = new FileBuilder();
// // this file is generated, do not edit // // this file is generated, do not edit
// package org.briarproject.briar.desktop; // package org.briarproject.briar.desktop;
...@@ -118,12 +131,14 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -118,12 +131,14 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
buffer.line(" public static String getVersion() {"); buffer.line(" public static String getVersion() {");
buffer.line(" return \"" + version + "\";"); buffer.line(" return \"" + version + "\";");
buffer.line(" }"); buffer.line(" }");
buffer.line();
// public static long getGitTime() { // public static long getGitTime() {
// return 1641645802088L; // return 1641645802088L;
// } // }
buffer.line(" public static long getGitTime() {"); buffer.line(" public static long getGitTime() {");
buffer.line(" return " + gitTime + "L;"); buffer.line(" return " + gitTime + "L;");
buffer.line(" }"); buffer.line(" }");
buffer.line();
// public static long getGitHash() { // public static long getGitHash() {
// return "749dda081c3e7862050255817bc239b9255b1582"; // return "749dda081c3e7862050255817bc239b9255b1582";
// } // }
...@@ -131,6 +146,13 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask { ...@@ -131,6 +146,13 @@ public class GenerateBuildDataSourceTask extends AbstractBuildDataTask {
buffer.line(" return \"" + gitHash + "\";"); buffer.line(" return \"" + gitHash + "\";");
buffer.line(" }"); buffer.line(" }");
buffer.line(); buffer.line();
// public static String getGitBranch() {
// return "master";
// }
buffer.line(" public static String getGitBranch() {");
buffer.line(" return \"" + gitBranch + "\";");
buffer.line(" }");
buffer.line();
buffer.line("}"); buffer.line("}");
return buffer.toString(); return buffer.toString();
......
...@@ -77,8 +77,10 @@ private class Main : CliktCommand( ...@@ -77,8 +77,10 @@ private class Main : CliktCommand(
val buildTime = Instant.ofEpochMilli(BuildData.getGitTime()).atZone(ZoneId.systemDefault()).toLocalDateTime() val buildTime = Instant.ofEpochMilli(BuildData.getGitTime()).atZone(ZoneId.systemDefault()).toLocalDateTime()
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
LOG.info { "This is briar-desktop version ${BuildData.getVersion()}" } LOG.info { "This is briar-desktop version ${BuildData.getVersion()}" }
LOG.info { "Built from Git hash ${BuildData.getGitHash()}" } LOG.info { "Build info:" }
LOG.info { "Last commit time ${formatter.format(buildTime)}" } LOG.info { " Git hash ${BuildData.getGitHash()}" }
LOG.info { " Commit time ${formatter.format(buildTime)}" }
LOG.info { " Branch ${BuildData.getGitBranch()}" }
val dataDir = getDataDir() val dataDir = getDataDir()
val app = val app =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment