add gradle build option to show commit hash (#10264)

This commit is contained in:
Son Phan Trung 2024-10-13 23:13:32 +07:00 committed by GitHub
parent a7c3ce5d8c
commit ea5975737c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -89,6 +89,10 @@ allprojects{
return project.getProperties()["buildversion"]
}
getCommitHash = {
return 'git rev-parse --verify --short HEAD'.execute().text.trim()
}
getPackage = {
return project.ext.mainClassName.substring(0, project.ext.mainClassName.indexOf("desktop") - 1)
}
@ -133,6 +137,10 @@ allprojects{
props["number"] = versionNumber
props["modifier"] = versionModifier
props["build"] = buildid
props["commitHash"] = "unknown"
if(project.hasProperty("showCommitHash")){
props["commitHash"] = getCommitHash()
}
props.store(pfile.newWriter(), "Autogenerated file. Do not modify.")
}

View File

@ -12,6 +12,8 @@ public class Version{
public static String type = "unknown";
/** Build modifier, e.g. 'alpha' or 'release' */
public static String modifier = "unknown";
/** Git commit hash (short) */
public static String commitHash = "unknown";
/** Number specifying the major version, e.g. '4' */
public static int number;
/** Build number, e.g. '43'. set to '-1' for custom builds. */
@ -32,6 +34,7 @@ public class Version{
type = map.get("type");
number = Integer.parseInt(map.get("number", "4"));
modifier = map.get("modifier");
commitHash = map.get("commitHash");
if(map.get("build").contains(".")){
String[] split = map.get("build").split("\\.");
try{
@ -73,6 +76,6 @@ public class Version{
if(build == -1){
return "custom build";
}
return (type.equals("official") ? modifier : type) + " build " + build + (revision == 0 ? "" : "." + revision);
return (type.equals("official") ? modifier : type) + " build " + build + (revision == 0 ? "" : "." + revision) + (commitHash.equals("unknown") ? "" : " (" + commitHash + ")");
}
}