feat: build info

i have this plan for quite a while now, finally i am able to do this
This commit is contained in:
Chayapak 2024-11-24 16:20:27 +07:00
parent d8c8a9750d
commit 975199bf4f
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
4 changed files with 91 additions and 4 deletions

1
build-number.txt Normal file
View file

@ -0,0 +1 @@
1107

View file

@ -1,3 +1,5 @@
import java.text.SimpleDateFormat
plugins {
id 'java'
id 'application'
@ -57,6 +59,51 @@ dependencies {
implementation 'org.concentus:Concentus:1.0-SNAPSHOT'
}
static def getGitCommitHash() {
try {
return 'git rev-parse --short HEAD'.execute().text.trim()
} catch (Exception e) {
e.printStackTrace()
return "unknown"
}
}
static def getGitCommitCount() {
try {
return 'git rev-list --count HEAD'.execute().text.trim()
} catch (Exception e) {
e.printStackTrace()
return "unknown"
}
}
def buildNumberFile = file("build-number.txt")
def buildNumber = 0
if (buildNumberFile.exists()) {
buildNumber = buildNumberFile.text.trim().toInteger() + 1
} else {
buildNumber = 1
}
buildNumberFile.text = buildNumber
ext.buildInfo = [
gitCommitCount: getGitCommitCount(),
compileDate: new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z").format(new Date()),
gitCommitHash: getGitCommitHash(),
buildNumber: buildNumber,
]
processResources {
inputs.property("buildInfo", buildInfo)
filesMatching("application.properties") {
expand(buildInfo)
}
}
application {
mainClass = 'me.chayapak1.chomens_bot.Main'
}

View file

@ -15,6 +15,7 @@ import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryUsage;
@ -29,15 +30,20 @@ import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Date;
import java.util.Optional;
import java.util.TimeZone;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class InfoCommand extends Command {
public static final String ORIGINAL_REPOSITORY_URL = "https://code.chipmunk.land/ChomeNS/chomens-bot-java";
public static final Properties BUILD_INFO = new Properties();
static {
try (final InputStream input = ClassLoader.getSystemClassLoader().getResourceAsStream("application.properties")) {
BUILD_INFO.load(input);
} catch (IOException ignored) {}
}
public InfoCommand () {
super(
"info",
@ -261,6 +267,35 @@ public class InfoCommand extends Command {
.clickEvent(
ClickEvent.openUrl(ORIGINAL_REPOSITORY_URL)
)
)
.append(Component.newline())
.append(Component.text("Compiled at "))
.append(
Component
.text(BUILD_INFO.getProperty("build.date", "unknown"))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.string))
)
.append(
Component
.translatable(
", Git commit %s (%s)",
Component
.text(BUILD_INFO.getProperty("build.git.commit.hash", "unknown"))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.string)),
Component
.text(BUILD_INFO.getProperty("build.git.commit.count", "unknown"))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
)
)
.append(Component.newline())
.append(
Component
.translatable(
"Build %s",
Component
.text(BUILD_INFO.getProperty("build.number", "unknown"))
.color(ColorUtilities.getColorByString(bot.config.colorPalette.number))
)
);
}
}

View file

@ -0,0 +1,4 @@
build.date=${compileDate}
build.git.commit.count=${gitCommitCount}
build.git.commit.hash=${gitCommitHash}
build.number=${buildNumber}