diff --git a/application/build.gradle b/application/build.gradle index c49e518948..f13f521682 100644 --- a/application/build.gradle +++ b/application/build.gradle @@ -93,3 +93,23 @@ application { mainClass = 'org.togetherjava.tjbot.Application' applicationDefaultJvmArgs = ["--enable-native-access=ALL-UNNAMED"] } + +def generatedBuildPropertiesDir = file("build/resources/main/") +tasks.register('generateBuildProperties') { + def outputFile = new File(generatedBuildPropertiesDir, "build.properties") + + doLast { + if (!generatedBuildPropertiesDir.exists()) { + generatedBuildPropertiesDir.mkdirs() + } + def props = new Properties() + props['app.git-version'] = version + props['app.build-date'] = new Date().format('yyyy-MM-dd HH:mm:ss:S z') + + outputFile.withWriter { writer -> + props.store(writer, null) + } + } +} + +processResources.dependsOn generateBuildProperties \ No newline at end of file diff --git a/application/src/main/java/org/togetherjava/tjbot/Application.java b/application/src/main/java/org/togetherjava/tjbot/Application.java index 4c228cb02a..bad46635af 100644 --- a/application/src/main/java/org/togetherjava/tjbot/Application.java +++ b/application/src/main/java/org/togetherjava/tjbot/Application.java @@ -17,9 +17,11 @@ import org.togetherjava.tjbot.logging.discord.DiscordLogging; import java.io.IOException; +import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.sql.SQLException; +import java.util.Properties; /** * Main class of the application. Use {@link #main(String[])} to start an instance of it. @@ -34,6 +36,8 @@ private Application() { private static final Logger logger = LoggerFactory.getLogger(Application.class); private static final String DEFAULT_CONFIG_PATH = "config.json"; + private static final String DEFAULT_BUILD_PROPERTIES_PATH = "build.properties"; + private static final Properties BUILD_PROPERTIES = new Properties(); /** * Starts the application. @@ -58,6 +62,20 @@ public static void main(final String[] args) { return; } + try (InputStream input = Application.class.getClassLoader() + .getResourceAsStream(DEFAULT_BUILD_PROPERTIES_PATH)) { + if (input == null) { + logger.warn( + "Unable to find {} file, you might not see properties like the git commit of this build", + DEFAULT_BUILD_PROPERTIES_PATH); + } else { + BUILD_PROPERTIES.load(input); + } + } catch (IOException e) { + logger.error("Exception while trying to load {} file", DEFAULT_BUILD_PROPERTIES_PATH, + e); + } + Thread.setDefaultUncaughtExceptionHandler(Application::onUncaughtException); Runtime.getRuntime().addShutdownHook(new Thread(Application::onShutdown)); DiscordLogging.startDiscordLogging(config); @@ -120,4 +138,7 @@ private static void onUncaughtException(Thread failingThread, Throwable failure) logger.error("Unknown error in thread {}.", failingThread.getName(), failure); } + public static Properties getBuildProperties() { + return BUILD_PROPERTIES; + } } diff --git a/application/src/main/java/org/togetherjava/tjbot/features/basic/PingCommand.java b/application/src/main/java/org/togetherjava/tjbot/features/basic/PingCommand.java index c4dc10a7bc..20962326ea 100644 --- a/application/src/main/java/org/togetherjava/tjbot/features/basic/PingCommand.java +++ b/application/src/main/java/org/togetherjava/tjbot/features/basic/PingCommand.java @@ -2,9 +2,12 @@ import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; +import org.togetherjava.tjbot.Application; import org.togetherjava.tjbot.features.CommandVisibility; import org.togetherjava.tjbot.features.SlashCommandAdapter; +import java.util.Properties; + /** * Implementation of an example command to illustrate how to respond to a user. *
@@ -25,6 +28,11 @@ public PingCommand() {
*/
@Override
public void onSlashCommand(SlashCommandInteractionEvent event) {
- event.reply("Pong!").queue();
+ Properties buildProperties = Application.getBuildProperties();
+ String response = "Pong! Running on version %s (%s)".formatted(
+ buildProperties.getProperty("app.git-version"),
+ buildProperties.getProperty("app.build-date"));
+
+ event.reply(response).queue();
}
}
diff --git a/application/src/test/java/org/togetherjava/tjbot/features/basic/PingCommandTest.java b/application/src/test/java/org/togetherjava/tjbot/features/basic/PingCommandTest.java
index fe82957c21..067a8d7f43 100644
--- a/application/src/test/java/org/togetherjava/tjbot/features/basic/PingCommandTest.java
+++ b/application/src/test/java/org/togetherjava/tjbot/features/basic/PingCommandTest.java
@@ -4,10 +4,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
import org.togetherjava.tjbot.features.SlashCommand;
import org.togetherjava.tjbot.jda.JdaTester;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.verify;
final class PingCommandTest {
@@ -35,6 +37,8 @@ void pingRespondsWithPong() {
SlashCommandInteractionEvent event = triggerSlashCommand();
// THEN the bot replies with pong
- verify(event).reply("Pong!");
+ ArgumentCaptor