Fix #1493 Extract handling of message file paths to a separate class with constants

This commit is contained in:
ljacqu
2019-06-22 22:37:32 +02:00
parent ff2f43bdc5
commit 4be130b71b
18 changed files with 210 additions and 75 deletions
@@ -2,14 +2,14 @@ package tools.docs.translations;
import ch.jalu.configme.resource.PropertyReader;
import ch.jalu.configme.resource.YamlFileReader;
import fr.xephi.authme.message.MessagePathHelper;
import fr.xephi.authme.message.MessageKey;
import tools.utils.ToolsConstants;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tools.utils.FileIoUtils.listFilesOrThrow;
@@ -18,14 +18,13 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
*/
public class TranslationsGatherer {
private static final Pattern MESSAGES_PATTERN = Pattern.compile("messages_([a-z]{2,4})\\.yml");
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER;
private List<TranslationInfo> translationInfo = new ArrayList<>();
public TranslationsGatherer() {
gatherTranslations();
translationInfo.sort((e1, e2) -> getSortCode(e1).compareTo(getSortCode(e2)));
translationInfo.sort(Comparator.comparing(TranslationsGatherer::getSortCode));
}
public List<TranslationInfo> getTranslationInfo() {
@@ -35,7 +34,7 @@ public class TranslationsGatherer {
private void gatherTranslations() {
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
for (File file : files) {
String code = getLanguageCode(file.getName());
String code = MessagePathHelper.getLanguageIfIsMessagesFile(file.getName());
if (code != null) {
processMessagesFile(code, file);
}
@@ -53,14 +52,6 @@ public class TranslationsGatherer {
translationInfo.add(new TranslationInfo(code, (double) availableMessages / MessageKey.values().length));
}
private String getLanguageCode(String messagesFile) {
Matcher matcher = MESSAGES_PATTERN.matcher(messagesFile);
if (matcher.find()) {
return matcher.group(1);
}
return null;
}
/**
* Returns the language code from the translation info for sorting purposes.
* Returns "a" for "en" language code to sort English on top.
@@ -69,7 +60,7 @@ public class TranslationsGatherer {
* @return the language code for sorting
*/
private static String getSortCode(TranslationInfo info) {
return "en".equals(info.code) ? "a" : info.code;
return MessagePathHelper.DEFAULT_LANGUAGE.equals(info.code) ? "a" : info.code;
}
public static final class TranslationInfo {
@@ -1,5 +1,6 @@
package tools.messages;
import fr.xephi.authme.message.MessagePathHelper;
import fr.xephi.authme.message.MessageKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -15,7 +16,8 @@ import java.util.List;
*/
public class AddJavaDocToMessageEnumTask implements AutoToolTask {
private static final String MESSAGES_FILE = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/messages_en.yml";
private static final String MESSAGES_FILE =
ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.DEFAULT_MESSAGES_FILE;
private FileConfiguration configuration;
@@ -1,6 +1,7 @@
package tools.messages;
import com.google.common.collect.Multimap;
import fr.xephi.authme.message.MessagePathHelper;
import fr.xephi.authme.message.MessageKey;
import fr.xephi.authme.util.StringUtils;
import org.bukkit.configuration.file.FileConfiguration;
@@ -15,8 +16,8 @@ import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;
import static fr.xephi.authme.message.MessagePathHelper.DEFAULT_MESSAGES_FILE;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
@@ -25,11 +26,7 @@ import static tools.utils.FileIoUtils.listFilesOrThrow;
public final class VerifyMessagesTask implements ToolTask {
/** The folder containing the message files. */
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
/** Pattern of the message file names. */
private static final Pattern MESSAGE_FILE_PATTERN = Pattern.compile("messages_[a-z]{2,7}\\.yml");
/** File to get default messages from (assumes that it is complete). */
private static final String DEFAULT_MESSAGES_FILE = MESSAGES_FOLDER + "messages_en.yml";
private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + MessagePathHelper.MESSAGES_FOLDER;
@Override
public String getTaskName() {
@@ -50,7 +47,7 @@ public final class VerifyMessagesTask implements ToolTask {
if (StringUtils.isEmpty(inputFile)) {
messageFiles = getMessagesFiles();
} else {
File customFile = new File(MESSAGES_FOLDER, "messages_" + inputFile + ".yml");
File customFile = new File(ToolsConstants.MAIN_RESOURCES_ROOT, MessagePathHelper.createMessageFilePath(inputFile));
messageFiles = Collections.singletonList(customFile);
}
@@ -118,7 +115,7 @@ public final class VerifyMessagesTask implements ToolTask {
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
List<File> messageFiles = new ArrayList<>();
for (File file : files) {
if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) {
if (MessagePathHelper.isMessagesFile(file.getName())) {
messageFiles.add(file);
}
}