Make bot version consistent with Maven
All checks were successful
continuous-integration/drone/push Build is passing

A new internal properties file has been added. Maven will scan this file and replace any value it finds.
This commit is contained in:
Bea 2022-11-22 23:28:59 +01:00
parent 70578d2ffc
commit 40aac28e34
6 changed files with 77 additions and 5 deletions

10
pom.xml
View File

@ -6,7 +6,7 @@
<groupId>wtf.beatrice.hidekobot</groupId>
<artifactId>HidekoBot</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.4.0</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
@ -42,7 +42,15 @@
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>

View File

@ -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.

View File

@ -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())
{

View File

@ -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)
{

View File

@ -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); }
}

View File

@ -0,0 +1 @@
bot.version=${project.version}