mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-19 15:17:56 +01:00
Tools - Create message files verifier (work in progress)
- Create utility to test the message files for any unknown or missing message keys
This commit is contained in:
parent
9c86fbd8bf
commit
935e9892c4
78
src/tools/messages/MessageFileVerifier.java
Normal file
78
src/tools/messages/MessageFileVerifier.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package messages;
|
||||||
|
|
||||||
|
import fr.xephi.authme.output.MessageKey;
|
||||||
|
import utils.FileUtils;
|
||||||
|
import utils.ToolsConstants;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies that a message file has all keys as given in {@link MessageKey}.
|
||||||
|
*/
|
||||||
|
public class MessageFileVerifier {
|
||||||
|
|
||||||
|
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
|
||||||
|
private static final String NEW_LINE = "\n";
|
||||||
|
|
||||||
|
private final String messagesFile;
|
||||||
|
private final Map<String, String> defaultMessages;
|
||||||
|
|
||||||
|
public MessageFileVerifier(Map<String, String> defaultMessages, String messagesFile) {
|
||||||
|
this.messagesFile = messagesFile;
|
||||||
|
this.defaultMessages = defaultMessages;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void verify(boolean addMissingKeys) {
|
||||||
|
Set<String> messageKeys = getAllMessageKeys();
|
||||||
|
List<String> fileLines = FileUtils.readLinesFromFile(messagesFile);
|
||||||
|
for (String line : fileLines) {
|
||||||
|
// Skip comments and empty lines
|
||||||
|
if (!line.startsWith("#") && !line.trim().isEmpty()) {
|
||||||
|
handleMessagesLine(line, messageKeys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (messageKeys.isEmpty()) {
|
||||||
|
System.out.println("Found all message keys");
|
||||||
|
} else {
|
||||||
|
handleMissingKeys(messageKeys, addMissingKeys);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMessagesLine(String line, Set<String> messageKeys) {
|
||||||
|
if (line.indexOf(':') == -1) {
|
||||||
|
System.out.println("Skipping line in unknown format: '" + line + "'");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
final String key = line.substring(0, line.indexOf(':'));
|
||||||
|
if (messageKeys.contains(key)) {
|
||||||
|
messageKeys.remove(key);
|
||||||
|
} else {
|
||||||
|
System.out.println("Warning: Unknown key '" + key + "' for line '" + line + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMissingKeys(Set<String> missingKeys, boolean addMissingKeys) {
|
||||||
|
for (String key : missingKeys) {
|
||||||
|
if (addMissingKeys) {
|
||||||
|
String defaultMessage = defaultMessages.get(key);
|
||||||
|
FileUtils.appendToFile(messagesFile, NEW_LINE + key + ":" + defaultMessage);
|
||||||
|
System.out.println("Added missing key '" + key + "' to file");
|
||||||
|
} else {
|
||||||
|
System.out.println("Error: Missing key '" + key + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Set<String> getAllMessageKeys() {
|
||||||
|
Set<String> messageKeys = new HashSet<>(MessageKey.values().length);
|
||||||
|
for (MessageKey key : MessageKey.values()) {
|
||||||
|
messageKeys.add(key.getKey());
|
||||||
|
}
|
||||||
|
return messageKeys;
|
||||||
|
}
|
||||||
|
}
|
70
src/tools/messages/MessagesVerifierRunner.java
Normal file
70
src/tools/messages/MessagesVerifierRunner.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package messages;
|
||||||
|
|
||||||
|
import utils.FileUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TODO: ljacqu write JavaDoc
|
||||||
|
*/
|
||||||
|
public final class MessagesVerifierRunner {
|
||||||
|
|
||||||
|
private MessagesVerifierRunner() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final Map<String, String> defaultMessages = constructDefaultMessages();
|
||||||
|
final List<File> messagesFiles = getMessagesFiles();
|
||||||
|
|
||||||
|
if (messagesFiles.isEmpty()) {
|
||||||
|
throw new RuntimeException("Error getting messages file: list of files is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File file : messagesFiles) {
|
||||||
|
System.out.println("Verifying '" + file.getName() + "'");
|
||||||
|
MessageFileVerifier messageFileVerifier = new MessageFileVerifier(defaultMessages, file.getAbsolutePath());
|
||||||
|
messageFileVerifier.verify(false);
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String, String> constructDefaultMessages() {
|
||||||
|
String defaultMessagesFile = MessageFileVerifier.MESSAGES_FOLDER + "messages_en.yml";
|
||||||
|
List<String> lines = FileUtils.readLinesFromFile(defaultMessagesFile);
|
||||||
|
Map<String, String> messages = new HashMap<>(lines.size());
|
||||||
|
for (String line : lines) {
|
||||||
|
if (line.startsWith("#") || line.trim().isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line.indexOf(':') == -1) {
|
||||||
|
System.out.println("Warning! Unknown format in default messages file for line '" + line + "'");
|
||||||
|
} else {
|
||||||
|
String key = line.substring(0, line.indexOf(':'));
|
||||||
|
messages.put(key, line.substring(line.indexOf(':') + 1)); // fixme: may throw exception
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<File> getMessagesFiles() {
|
||||||
|
final Pattern messageFilePattern = Pattern.compile("messages_[a-z]{2,3}\\.yml");
|
||||||
|
File folder = new File(MessageFileVerifier.MESSAGES_FOLDER);
|
||||||
|
File[] files = folder.listFiles();
|
||||||
|
if (files == null) {
|
||||||
|
throw new RuntimeException("Could not read files from folder '" + folder.getName() + "'");
|
||||||
|
}
|
||||||
|
|
||||||
|
List<File> messageFiles = new ArrayList<>();
|
||||||
|
for (File file : files) {
|
||||||
|
if (messageFilePattern.matcher(file.getName()).matches()) {
|
||||||
|
messageFiles.add(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return messageFiles;
|
||||||
|
}
|
||||||
|
}
|
2
src/tools/messages/README.md
Normal file
2
src/tools/messages/README.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
## Messages
|
||||||
|
Verifies the messages files and adds any missing indices with the English content as default.
|
@ -4,6 +4,8 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,6 +33,14 @@ public final class FileUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void appendToFile(String outputFile, String contents) {
|
||||||
|
try {
|
||||||
|
Files.write(Paths.get(outputFile), contents.getBytes(), StandardOpenOption.APPEND);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Failed to append to file '" + outputFile + "'", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String readFromFile(String file) {
|
public static String readFromFile(String file) {
|
||||||
try {
|
try {
|
||||||
return new String(Files.readAllBytes(Paths.get(file)), CHARSET);
|
return new String(Files.readAllBytes(Paths.get(file)), CHARSET);
|
||||||
@ -39,6 +49,14 @@ public final class FileUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<String> readLinesFromFile(String file) {
|
||||||
|
try {
|
||||||
|
return Files.readAllLines(Paths.get(file), CHARSET);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("Could not read from file '" + file + "'", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static String readFromToolsFile(String file) {
|
public static String readFromToolsFile(String file) {
|
||||||
return readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT + file);
|
return readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT + file);
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ public final class ToolsConstants {
|
|||||||
|
|
||||||
public static final String MAIN_SOURCE_ROOT = "src/main/java/";
|
public static final String MAIN_SOURCE_ROOT = "src/main/java/";
|
||||||
|
|
||||||
|
public static final String MAIN_RESOURCES_ROOT = "src/main/resources/";
|
||||||
|
|
||||||
public static final String TOOLS_SOURCE_ROOT = "src/tools/";
|
public static final String TOOLS_SOURCE_ROOT = "src/tools/";
|
||||||
|
|
||||||
public static final String DOCS_FOLDER = TOOLS_SOURCE_ROOT + "docs/";
|
public static final String DOCS_FOLDER = TOOLS_SOURCE_ROOT + "docs/";
|
||||||
|
Loading…
Reference in New Issue
Block a user