From 975199bf4f59a88a3f3b559bf87b059e18d0d20f Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 24 Nov 2024 16:20:27 +0700 Subject: [PATCH] feat: build info i have this plan for quite a while now, finally i am able to do this --- build-number.txt | 1 + build.gradle | 47 +++++++++++++++++++ .../chomens_bot/commands/InfoCommand.java | 43 +++++++++++++++-- src/main/resources/application.properties | 4 ++ 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 build-number.txt create mode 100644 src/main/resources/application.properties diff --git a/build-number.txt b/build-number.txt new file mode 100644 index 0000000..6a333d3 --- /dev/null +++ b/build-number.txt @@ -0,0 +1 @@ +1107 \ No newline at end of file diff --git a/build.gradle b/build.gradle index d671b8c..53b27bb 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/InfoCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/InfoCommand.java index 5480ad7..2dd7ed2 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/InfoCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/InfoCommand.java @@ -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)) + ) ); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..36fd481 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +build.date=${compileDate} +build.git.commit.count=${gitCommitCount} +build.git.commit.hash=${gitCommitHash} +build.number=${buildNumber} \ No newline at end of file