mirror of
https://github.com/filoghost/ChestCommands.git
synced 2025-02-16 11:31:23 +01:00
Improve legacy upgrades code
This commit is contained in:
parent
b52d80e02c
commit
e3f355bb41
@ -150,10 +150,9 @@ public class ChestCommands extends FCommonsPlugin {
|
||||
errorCollector.add(e, Errors.Config.createDataFolderIOException);
|
||||
return errorCollector;
|
||||
}
|
||||
|
||||
UpgradesExecutor upgradeExecutor = new UpgradesExecutor(configManager);
|
||||
|
||||
|
||||
try {
|
||||
UpgradesExecutor upgradeExecutor = new UpgradesExecutor(configManager);
|
||||
boolean allUpgradesSuccessful = upgradeExecutor.run(isFreshInstall, errorCollector);
|
||||
if (!allUpgradesSuccessful) {
|
||||
errorCollector.add(Errors.Upgrade.failedSomeUpgrades);
|
||||
|
@ -5,13 +5,14 @@
|
||||
*/
|
||||
package me.filoghost.chestcommands.legacy;
|
||||
|
||||
import me.filoghost.fcommons.Preconditions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Arrays;
|
||||
import me.filoghost.fcommons.Preconditions;
|
||||
|
||||
public class Backup {
|
||||
|
||||
@ -37,7 +38,7 @@ public class Backup {
|
||||
Path destination = backupFolder.resolve(dataFolder.relativize(fileToBackup));
|
||||
Files.createDirectories(destination.getParent());
|
||||
|
||||
// Add backup file if no already present
|
||||
// Add backup file if not already present
|
||||
if (!Files.isRegularFile(destination)) {
|
||||
Files.copy(fileToBackup, destination);
|
||||
}
|
||||
|
@ -11,8 +11,8 @@ import me.filoghost.chestcommands.legacy.upgrade.Upgrade;
|
||||
import me.filoghost.chestcommands.legacy.upgrade.UpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.upgrade.UpgradeTaskException;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_LangUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_MenuNodeRenameUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_MenuReformatUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_MenuConfigUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_MenuRawTextFileUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_PlaceholdersUpgradeTask;
|
||||
import me.filoghost.chestcommands.legacy.v4_0.V4_0_SettingsUpgradeTask;
|
||||
import me.filoghost.chestcommands.logging.Errors;
|
||||
@ -34,15 +34,16 @@ public class UpgradeList {
|
||||
* Note: order of declaration determines order of execution
|
||||
*/
|
||||
private static final ImmutableList<Upgrade> orderedUpgrades = ImmutableList.of(
|
||||
// Edit the raw text first
|
||||
multiTaskUpgrade("v4.0-menus-rename", (configManager) -> {
|
||||
return createMenuTasks(configManager, V4_0_MenuNodeRenameUpgradeTask::new);
|
||||
return createMenuTasks(configManager, V4_0_MenuRawTextFileUpgradeTask::new);
|
||||
}),
|
||||
|
||||
// Reformat after nodes have already been renamed
|
||||
// Manipulate the configuration after editing the raw text
|
||||
multiTaskUpgrade("v4.0-menus-reformat", (configManager) -> {
|
||||
String legacyCommandSeparator = readLegacyCommandSeparator(configManager);
|
||||
return createMenuTasks(configManager,
|
||||
file -> new V4_0_MenuReformatUpgradeTask(configManager, file, legacyCommandSeparator));
|
||||
file -> new V4_0_MenuConfigUpgradeTask(configManager, file, legacyCommandSeparator));
|
||||
}),
|
||||
|
||||
// Upgrade config after reading the command separator for menus
|
||||
|
@ -73,7 +73,7 @@ public abstract class RegexUpgradeTask extends UpgradeTask {
|
||||
}
|
||||
}
|
||||
|
||||
protected void replaceSubNode(String oldNode, String newNode) {
|
||||
protected void renameInnerNode(String oldNode, String newNode) {
|
||||
replaceRegex(
|
||||
Pattern.compile("(^\\s+)" + Pattern.quote(oldNode) + "(:)"),
|
||||
matcher -> matcher.group(1) + newNode + matcher.group(2)
|
||||
|
@ -5,12 +5,13 @@
|
||||
*/
|
||||
package me.filoghost.chestcommands.legacy.upgrade;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import me.filoghost.fcommons.config.Config;
|
||||
import me.filoghost.fcommons.config.ConfigLoader;
|
||||
import me.filoghost.fcommons.config.exception.ConfigLoadException;
|
||||
import me.filoghost.fcommons.config.exception.ConfigSaveException;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public abstract class YamlUpgradeTask extends UpgradeTask {
|
||||
|
||||
private final ConfigLoader configLoader;
|
||||
@ -47,17 +48,17 @@ public abstract class YamlUpgradeTask extends UpgradeTask {
|
||||
|
||||
protected abstract void computeYamlChanges(Config config);
|
||||
|
||||
protected void removeNode(Config config, String node) {
|
||||
if (config.contains(node)) {
|
||||
config.remove(node);
|
||||
protected void removeValue(Config config, String configPath) {
|
||||
if (config.contains(configPath)) {
|
||||
config.remove(configPath);
|
||||
setSaveRequired();
|
||||
}
|
||||
}
|
||||
|
||||
protected void replaceStringValue(Config settingsConfig, String node, String target, String replacement) {
|
||||
String value = settingsConfig.getString(node);
|
||||
protected void replaceStringValue(Config settingsConfig, String configPath, String target, String replacement) {
|
||||
String value = settingsConfig.getString(configPath);
|
||||
if (value.contains(target)) {
|
||||
settingsConfig.setString(node, value.replace(target, replacement));
|
||||
settingsConfig.setString(configPath, value.replace(target, replacement));
|
||||
setSaveRequired();
|
||||
}
|
||||
}
|
||||
|
@ -17,8 +17,8 @@ public class V4_0_LangUpgradeTask extends YamlUpgradeTask {
|
||||
|
||||
@Override
|
||||
public void computeYamlChanges(Config settingsConfig) {
|
||||
removeNode(settingsConfig, "open-menu");
|
||||
removeNode(settingsConfig, "open-menu-others");
|
||||
removeValue(settingsConfig, "open-menu");
|
||||
removeValue(settingsConfig, "open-menu-others");
|
||||
replaceStringValue(settingsConfig, "no-required-item", "{datavalue}", "{durability}");
|
||||
}
|
||||
|
||||
|
@ -21,11 +21,14 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class V4_0_MenuReformatUpgradeTask extends YamlUpgradeTask {
|
||||
/*
|
||||
* All the changes that are not easy to make without parsing the YML file
|
||||
*/
|
||||
public class V4_0_MenuConfigUpgradeTask extends YamlUpgradeTask {
|
||||
|
||||
private final String legacyCommandSeparator;
|
||||
|
||||
public V4_0_MenuReformatUpgradeTask(ConfigManager configManager, Path menuFile, String legacyCommandSeparator) {
|
||||
public V4_0_MenuConfigUpgradeTask(ConfigManager configManager, Path menuFile, String legacyCommandSeparator) {
|
||||
super(configManager.getConfigLoader(menuFile));
|
||||
this.legacyCommandSeparator = legacyCommandSeparator;
|
||||
}
|
||||
@ -63,8 +66,8 @@ public class V4_0_MenuReformatUpgradeTask extends YamlUpgradeTask {
|
||||
expandInlineItemstack(section);
|
||||
}
|
||||
|
||||
private void updateActionPrefixes(ConfigSection config, ConfigPath node) {
|
||||
List<String> actions = config.getStringList(node);
|
||||
private void updateActionPrefixes(ConfigSection config, ConfigPath configPath) {
|
||||
List<String> actions = config.getStringList(configPath);
|
||||
if (actions == null) {
|
||||
return;
|
||||
}
|
||||
@ -83,7 +86,7 @@ public class V4_0_MenuReformatUpgradeTask extends YamlUpgradeTask {
|
||||
}
|
||||
}
|
||||
|
||||
config.setStringList(node, actions);
|
||||
config.setStringList(configPath, actions);
|
||||
}
|
||||
|
||||
private String replacePrefix(String action, String oldPrefix, String newPrefix) {
|
@ -10,17 +10,20 @@ import me.filoghost.chestcommands.parsing.icon.AttributeType;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class V4_0_MenuNodeRenameUpgradeTask extends RegexUpgradeTask {
|
||||
/*
|
||||
* All the changes that can be done by searching and replacing the raw text inside the files
|
||||
*/
|
||||
public class V4_0_MenuRawTextFileUpgradeTask extends RegexUpgradeTask {
|
||||
|
||||
public V4_0_MenuNodeRenameUpgradeTask(Path menuFile) {
|
||||
public V4_0_MenuRawTextFileUpgradeTask(Path menuFile) {
|
||||
super(menuFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void computeRegexChanges() {
|
||||
replaceSubNode("command", "commands");
|
||||
replaceSubNode("open-action", "open-actions");
|
||||
replaceSubNode("id", "material");
|
||||
renameInnerNode("command", "commands");
|
||||
renameInnerNode("open-action", "open-actions");
|
||||
renameInnerNode("id", "material");
|
||||
|
||||
replaceOldAttribute("ID", AttributeType.MATERIAL);
|
||||
replaceOldAttribute("DATA-VALUE", AttributeType.DURABILITY);
|
||||
@ -31,8 +34,8 @@ public class V4_0_MenuNodeRenameUpgradeTask extends RegexUpgradeTask {
|
||||
replaceOldAttribute("REQUIRED-ITEM", AttributeType.REQUIRED_ITEMS);
|
||||
}
|
||||
|
||||
private void replaceOldAttribute(String oldNode, AttributeType newAttribute) {
|
||||
replaceSubNode(oldNode, newAttribute.getConfigKey().asRawKey());
|
||||
private void replaceOldAttribute(String oldConfigKey, AttributeType newAttribute) {
|
||||
renameInnerNode(oldConfigKey, newAttribute.getConfigKey().asRawKey());
|
||||
}
|
||||
|
||||
}
|
@ -43,7 +43,7 @@ public class V4_0_PlaceholdersUpgradeTask extends UpgradeTask {
|
||||
|
||||
@Override
|
||||
public void computeChanges() throws ConfigLoadException {
|
||||
if (!Files.isRegularFile(oldPlaceholdersFile)) {
|
||||
if (!Files.isRegularFile(getOriginalFile()) || Files.isRegularFile(getUpgradedFile())) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -17,9 +17,9 @@ public class V4_0_SettingsUpgradeTask extends YamlUpgradeTask {
|
||||
|
||||
@Override
|
||||
public void computeYamlChanges(Config settingsConfig) {
|
||||
removeNode(settingsConfig, "use-only-commands-without-args");
|
||||
removeNode(settingsConfig, "use-console-colors");
|
||||
removeNode(settingsConfig, "multiple-commands-separator");
|
||||
removeValue(settingsConfig, "use-only-commands-without-args");
|
||||
removeValue(settingsConfig, "use-console-colors");
|
||||
removeValue(settingsConfig, "multiple-commands-separator");
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user