Update configme (#1631)

* Upgrade to ConfigMe 1.0.1
* Use ConfigMe reader whenever possible, minor simplifications
This commit is contained in:
ljacqu
2018-09-09 15:45:00 +02:00
committed by GitHub
parent 6676a39447
commit ee764c0a6e
42 changed files with 516 additions and 555 deletions
@@ -1,7 +1,7 @@
package tools.docs.config;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.resource.YamlFileResource;
import ch.jalu.configme.SettingsManagerBuilder;
import fr.xephi.authme.settings.properties.AuthMeSettingsRetriever;
import fr.xephi.authme.util.FileUtils;
import tools.utils.AutoToolTask;
@@ -31,8 +31,9 @@ public class UpdateConfigPageTask implements AutoToolTask {
try {
// Create empty temporary .yml file and save the config to it
config = File.createTempFile("authme-config-", ".yml");
SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(config), null, AuthMeSettingsRetriever.buildConfigurationData());
SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(config)
.configurationData(AuthMeSettingsRetriever.buildConfigurationData())
.create();
settingsManager.save();
// Get the contents and generate template file
@@ -1,8 +1,8 @@
package tools.docs.translations;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import tools.utils.ToolsConstants;
import java.io.File;
@@ -43,10 +43,10 @@ public class TranslationsGatherer {
}
private void processMessagesFile(String code, File file) {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(file);
PropertyReader reader = new YamlFileReader(file);
int availableMessages = 0;
for (MessageKey key : MessageKey.values()) {
if (configuration.contains(key.getKey())) {
if (reader.contains(key.getKey())) {
++availableMessages;
}
}
@@ -1,7 +1,7 @@
package tools.filegeneration;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.resource.YamlFileResource;
import ch.jalu.configme.SettingsManagerBuilder;
import fr.xephi.authme.settings.commandconfig.CommandConfig;
import fr.xephi.authme.settings.commandconfig.CommandSettingsHolder;
import tools.utils.AutoToolTask;
@@ -24,8 +24,9 @@ public class GenerateCommandsYml implements AutoToolTask {
CommandConfig commandConfig = CommandSettingsHolder.COMMANDS.getDefaultValue();
// Export the value to the file
SettingsManager settingsManager = new SettingsManager(
new YamlFileResource(file), null, CommandSettingsHolder.class);
SettingsManager settingsManager = SettingsManagerBuilder.withYamlFile(file)
.configurationData(CommandSettingsHolder.class)
.create();
settingsManager.setProperty(CommandSettingsHolder.COMMANDS, commandConfig);
settingsManager.save();
@@ -1,10 +1,10 @@
package tools.messages;
import ch.jalu.configme.SettingsManager;
import ch.jalu.configme.configurationdata.ConfigurationData;
import ch.jalu.configme.properties.Property;
import ch.jalu.configme.resource.PropertyResource;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileResource;
import fr.xephi.authme.message.updater.MessageKeyConfigurationData;
import fr.xephi.authme.message.updater.MessageUpdater;
import fr.xephi.authme.message.updater.MigraterYamlFileResource;
import org.bukkit.configuration.file.FileConfiguration;
@@ -47,12 +47,18 @@ public final class MessagesFileWriter {
}
private void performWrite() {
// Initialize ConfigMe classes
MessageKeyConfigurationData configurationData = MessageUpdater.createConfigurationData();
YamlFileResource resource = new MigraterYamlFileResource(file);
PropertyReader reader = resource.createReader();
configurationData.initializeValues(reader);
// Store initial comments so we can add them back later
List<String> initialComments = getInitialUserComments();
List<String> initialComments = getInitialUserComments(configurationData);
// Create property resource with new defaults, save with ConfigMe for proper sections & comments
PropertyResource resource = createPropertyResourceWithCommentEntries();
new SettingsManager(resource, null, MessageUpdater.getConfigurationData()).save();
addMissingMessagesWithCommentMarker(reader, configurationData);
resource.exportProperties(configurationData);
// Go through the newly saved file and replace texts with comment marker to actual YAML comments
// and add initial comments back to the file
@@ -60,11 +66,14 @@ public final class MessagesFileWriter {
}
/**
* Returns the comments at the top which are custom to the file.
*
* @param configurationData the configuration data
* @return any custom comments at the top of the file, for later usage
*/
private List<String> getInitialUserComments() {
private List<String> getInitialUserComments(ConfigurationData configurationData) {
final List<String> initialComments = new ArrayList<>();
final String firstCommentByConfigMe = getFirstCommentByConfigMe();
final String firstCommentByConfigMe = getFirstCommentByConfigMe(configurationData);
for (String line : FileIoUtils.readLinesFromFile(file.toPath())) {
if (line.isEmpty() || line.startsWith("#") && !line.equals(firstCommentByConfigMe)) {
@@ -86,27 +95,30 @@ public final class MessagesFileWriter {
}
/**
* @return the first comment generated by ConfigMe (comment of the first root path)
* Returns the first comment generated by ConfigMe (comment of the first root path).
*
* @param configurationData the configuration data
* @return first comment which is generated by ConfigMe
*/
private static String getFirstCommentByConfigMe() {
ConfigurationData configurationData = MessageUpdater.getConfigurationData();
private static String getFirstCommentByConfigMe(ConfigurationData configurationData) {
String firstRootPath = configurationData.getProperties().get(0).getPath().split("\\.")[0];
return "# " + configurationData.getCommentsForSection(firstRootPath)[0];
return "# " + configurationData.getCommentsForSection(firstRootPath).get(0);
}
/**
* @return generated {@link PropertyResource} with missing entries taken from the default file and marked
* with the {@link #COMMENT_MARKER}
* Adds a text with a {@link #COMMENT_MARKER} for all properties which are not yet present in the reader.
*
* @param reader the property reader
* @param configurationData the configuration data
*/
private PropertyResource createPropertyResourceWithCommentEntries() {
YamlFileResource resource = new MigraterYamlFileResource(file);
for (Property<?> property : MessageUpdater.getConfigurationData().getProperties()) {
String text = resource.getString(property.getPath());
private void addMissingMessagesWithCommentMarker(PropertyReader reader,
MessageKeyConfigurationData configurationData) {
for (Property<String> property : configurationData.getAllMessageProperties()) {
String text = reader.getString(property.getPath());
if (text == null) {
resource.setValue(property.getPath(), COMMENT_MARKER + defaultFile.getString(property.getPath()));
configurationData.setValue(property, COMMENT_MARKER + defaultFile.getString(property.getPath()));
}
}
return resource;
}
/**