#411 Improve command migration handling, write tests

This commit is contained in:
ljacqu
2016-11-24 17:39:57 +01:00
parent e83935c11e
commit f6ed39b118
14 changed files with 248 additions and 57 deletions
@@ -2,11 +2,9 @@ package fr.xephi.authme.settings.commandconfig;
import com.github.authme.configme.SettingsManager;
import com.github.authme.configme.resource.YamlFileResource;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.initialization.DataFolder;
import fr.xephi.authme.initialization.Reloadable;
import fr.xephi.authme.service.BukkitService;
import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.FileUtils;
import org.bukkit.entity.Player;
@@ -22,17 +20,14 @@ public class CommandManager implements Reloadable {
private final File dataFolder;
private final BukkitService bukkitService;
private final CommandsMigrater commandsMigrater;
private final Settings settings;
private CommandConfig commandConfig;
@Inject
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService,
CommandsMigrater commandsMigrater, Settings settings) {
CommandManager(@DataFolder File dataFolder, BukkitService bukkitService, CommandsMigrater commandsMigrater) {
this.dataFolder = dataFolder;
this.bukkitService = bukkitService;
this.commandsMigrater = commandsMigrater;
this.settings = settings;
reload();
}
@@ -65,21 +60,8 @@ public class CommandManager implements Reloadable {
FileUtils.copyFileFromResource(file, "commands.yml");
SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(file), null, CommandSettingsHolder.class);
CommandConfig commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
if (commandsMigrater.transformOldCommands(commandConfig)) {
ConsoleLogger.warning("Old setting properties (such as settings.forceCommands) were found. "
+ "They have been moved to commands.yml");
settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig);
settingsManager.save();
settingsManager.reload();
settings.save();
settings.reload();
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
}
this.commandConfig = commandConfig;
new YamlFileResource(file), commandsMigrater, CommandSettingsHolder.class);
commandConfig = settingsManager.getProperty(CommandSettingsHolder.COMMANDS);
}
@@ -25,7 +25,7 @@ public final class CommandSettingsHolder implements SettingsHolder {
String[] comments = {
"This configuration file allows you to execute commands on various events.",
"%p in commands will be replaced with the player name.",
"For example, if you want to message a welcome message to a player who just registered:",
"For example, if you want to send a welcome message to a player who just registered:",
"onRegister:",
" welcome:",
" command: 'msg %p Welcome to the server!'",
@@ -33,15 +33,17 @@ public final class CommandSettingsHolder implements SettingsHolder {
"",
"This will make the console execute the msg command to the player.",
"Each command under an event has a name you can choose freely (e.g. 'welcome' as above),",
"after which a mandatory 'command' field defines the command to run, ",
"after which a mandatory 'command' field defines the command to run,",
"and 'as' defines who will run the command (either PLAYER or CONSOLE). Longer example:",
"onLogin:",
" welcome:",
" command: 'msg %p Welcome back!'",
" # as: PLAYER # player is the default, you can leave this out if you want",
" as: PLAYER",
" broadcast:",
" command: 'broadcast %p has joined, welcome back!'",
" as: CONSOLE"
" as: CONSOLE",
"",
"Supported command events: onLogin, onJoin, onRegister"
};
Map<String, String[]> commentMap = new HashMap<>();
commentMap.put("onLogin", comments);
@@ -1,5 +1,9 @@
package fr.xephi.authme.settings.commandconfig;
import com.github.authme.configme.migration.MigrationService;
import com.github.authme.configme.properties.Property;
import com.github.authme.configme.resource.PropertyResource;
import com.google.common.annotations.VisibleForTesting;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.SettingsMigrationService;
import fr.xephi.authme.util.RandomStringUtils;
@@ -13,7 +17,7 @@ import java.util.stream.Collectors;
/**
* Migrates the commands from their old location, in config.yml, to the dedicated commands configuration file.
*/
class CommandsMigrater {
class CommandsMigrater implements MigrationService {
@Inject
private SettingsMigrationService settingsMigrationService;
@@ -21,12 +25,25 @@ class CommandsMigrater {
CommandsMigrater() {
}
@Override
public boolean checkAndMigrate(PropertyResource resource, List<Property<?>> properties) {
final CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getValue(resource);
final boolean didMoveCommands = transformOldCommands(commandConfig);
if (didMoveCommands) { // TODO ConfigMe/#29: Check that loaded file isn't completely empty
resource.setValue("", commandConfig);
return true;
}
return false;
}
/**
* Adds command settings from their old location (in config.yml) to the given command configuration object.
*
* @param commandConfig the command config object to move old commands to
* @return true if commands have been moved, false if no migration was necessary
*/
@VisibleForTesting
boolean transformOldCommands(CommandConfig commandConfig) {
boolean didMoveCommands = false;
for (MigratableCommandSection section : MigratableCommandSection.values()) {
@@ -97,7 +114,8 @@ class CommandsMigrater {
}
Map<String, Command> commandMap = commandMapGetter.apply(commandConfig);
commands.forEach(cmd -> commandMap.put(RandomStringUtils.generate(10), cmd));
ConsoleLogger.info("Migrated " + commands.size() + " commands of type " + this);
ConsoleLogger.info("Moving " + commands.size() + " commands of type " + this
+ " from config.yml to commands.yml");
return true;
}
}