From 40aac28e3430f3f1cc747e2e47dc4cbe72e3a372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 22 Nov 2022 23:28:59 +0100 Subject: [PATCH] Make bot version consistent with Maven A new internal properties file has been added. Maven will scan this file and replace any value it finds. --- pom.xml | 10 ++++- .../java/wtf/beatrice/hidekobot/Cache.java | 19 ++++++++-- .../wtf/beatrice/hidekobot/HidekoBot.java | 9 +++++ .../datasource/ConfigurationSource.java | 6 ++- .../datasource/PropertiesSource.java | 37 +++++++++++++++++++ src/main/resources/default.properties | 1 + 6 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/main/java/wtf/beatrice/hidekobot/datasource/PropertiesSource.java create mode 100644 src/main/resources/default.properties diff --git a/pom.xml b/pom.xml index c8875c5..4ab6d5f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ wtf.beatrice.hidekobot HidekoBot - 1.0-SNAPSHOT + 0.4.0 16 @@ -42,7 +42,15 @@ + + + + src/main/resources + true + + + org.apache.maven.plugins diff --git a/src/main/java/wtf/beatrice/hidekobot/Cache.java b/src/main/java/wtf/beatrice/hidekobot/Cache.java index 08063d2..08cd638 100644 --- a/src/main/java/wtf/beatrice/hidekobot/Cache.java +++ b/src/main/java/wtf/beatrice/hidekobot/Cache.java @@ -3,6 +3,7 @@ package wtf.beatrice.hidekobot; import org.jetbrains.annotations.Nullable; import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.datasource.DatabaseSource; +import wtf.beatrice.hidekobot.datasource.PropertiesSource; import wtf.beatrice.hidekobot.listeners.MessageCommandListener; import wtf.beatrice.hidekobot.listeners.MessageLogger; import wtf.beatrice.hidekobot.listeners.SlashCommandListener; @@ -19,6 +20,8 @@ public class Cache // todo: make this compatible with the message listener's regex private static final String botPrefix = "hideko"; private static final Logger logger = new Logger(Cache.class); + + private static PropertiesSource propertiesSource = null; private static ConfigurationSource configurationSource = null; private static DatabaseSource databaseSource = null; private static boolean verbose = false; @@ -33,8 +36,6 @@ public class Cache private static LocalDateTime startupTime = null; private final static String execPath = System.getProperty("user.dir"); - - private static final String botVersion = "0.4.0"; // todo: we should probably find a way to make this consistent with Maven private static final String botName = "Hideko"; private static SlashCommandListener slashCommandListener = null; @@ -160,6 +161,16 @@ public class Cache */ public static @Nullable DatabaseSource getDatabaseSource() { return databaseSource; } + /** + * Set the properties source instance loaded from the JAR archive. + * + * @param propertiesSourceInstance the properties source instance. + */ + public static void setPropertiesSourceInstance(PropertiesSource propertiesSourceInstance) + { + propertiesSource = propertiesSourceInstance; + } + /** * Get the DateTimeFormatter string for parsing the expired messages timestamp. * @@ -182,7 +193,9 @@ public class Cache * * @return a String of the bot version. */ - public static String getBotVersion() { return botVersion; } + public static String getBotVersion() { + return propertiesSource.getProperty("bot.version"); + } /** * Get the bot's global color. diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index f5123c5..a562039 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -10,6 +10,7 @@ import wtf.beatrice.hidekobot.commands.message.HelloCommand; import wtf.beatrice.hidekobot.commands.slash.*; import wtf.beatrice.hidekobot.datasource.ConfigurationSource; import wtf.beatrice.hidekobot.datasource.DatabaseSource; +import wtf.beatrice.hidekobot.datasource.PropertiesSource; import wtf.beatrice.hidekobot.listeners.ButtonInteractionListener; import wtf.beatrice.hidekobot.listeners.MessageCommandListener; import wtf.beatrice.hidekobot.listeners.SlashCommandCompleter; @@ -45,6 +46,14 @@ public class HidekoBot Cache.setConfigurationSource(configurationSource); logger.log("Configuration loaded!"); + // load properties + logger.log("Loading properties..."); + PropertiesSource propertiesSource = new PropertiesSource(); + propertiesSource.load(); + Cache.setPropertiesSourceInstance(propertiesSource); + logger.log("Properties loaded!"); + + // check loaded bot token String botToken = Cache.getBotToken(); if(botToken == null || botToken.isEmpty()) { diff --git a/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java b/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java index 9b9d1c2..16eecc2 100644 --- a/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java +++ b/src/main/java/wtf/beatrice/hidekobot/datasource/ConfigurationSource.java @@ -31,7 +31,11 @@ public class ConfigurationSource .getClassLoader() .getResourceAsStream("config.yml")) { internalConfigContents = internalConfigYaml.load(internalConfigStream); } - catch (IOException e) { logger.log(e.getMessage()); } + catch (IOException e) { + logger.log(e.getMessage()); + HidekoBot.shutdown(); + return; + } if(internalConfigContents == null) { diff --git a/src/main/java/wtf/beatrice/hidekobot/datasource/PropertiesSource.java b/src/main/java/wtf/beatrice/hidekobot/datasource/PropertiesSource.java new file mode 100644 index 0000000..7afb549 --- /dev/null +++ b/src/main/java/wtf/beatrice/hidekobot/datasource/PropertiesSource.java @@ -0,0 +1,37 @@ +package wtf.beatrice.hidekobot.datasource; + +import wtf.beatrice.hidekobot.HidekoBot; +import wtf.beatrice.hidekobot.util.Logger; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class PropertiesSource +{ + + private Properties properties = null; + private final String fileName = "default.properties"; + private final Logger logger = new Logger(getClass()); + + public void load() + { + properties = new Properties(); + + try (InputStream internalPropertiesStream = getClass() + .getClassLoader() + .getResourceAsStream(fileName)) + { + properties.load(internalPropertiesStream); + + } + catch (IOException e) { + logger.log(e.getMessage()); + HidekoBot.shutdown(); + return; + } + } + + public String getProperty(String property) + { return properties == null ? "" : properties.getProperty(property); } +} diff --git a/src/main/resources/default.properties b/src/main/resources/default.properties new file mode 100644 index 0000000..f98ac11 --- /dev/null +++ b/src/main/resources/default.properties @@ -0,0 +1 @@ +bot.version=${project.version} \ No newline at end of file