Message file verifier: allow to simply enter the language code instead of full path

- To check a single file just enter the language file, e.g. "es"
- Pass File object to MessageFileVerifier instead of String path that will be constructed to a File again...
This commit is contained in:
ljacqu 2016-10-01 11:02:24 +02:00
parent 8d64c0e5bf
commit 113a3f346c
5 changed files with 31 additions and 34 deletions

View File

@ -36,7 +36,7 @@ name_len: '&cTvůj nick je přílíš krátký, nebo přílíš dlouhý'
regex: '&cTvůj nick obsahuje nepovolené znaky. Přípustné znaky jsou: REG_EX'
add_email: '&cPřidej prosím svůj email pomocí : /email add TvůjEmail TvůjEmail'
recovery_email: '&cZapomněl jsi heslo? Napiš: /email recovery <TvůjEmail>'
usage_captcha: '&cPoužij: /captcha <Captcha>'
usage_captcha: '&cPoužij: /captcha <theCaptcha>'
wrong_captcha: '&cŠpatné opsana Captcha, pouzij prosim: /captcha THE_CAPTCHA'
valid_captcha: '&cZadaná captcha je v pořádku!'
kick_forvip: '&cOmlouváme se, ale VIP hráč se připojil na plný server!'
@ -66,3 +66,10 @@ password_error_unsafe: '&cToto heslo není bezpečné, prosím zvol si jiné hes
kick_antibot: 'Bezpečnostní mód AntiBot je zapnut! Musíš počkat několik minut než se budeš moct připojit znovu na server.'
email_exists: '&cNový email byl odeslán! Můžeš ho zahodit a poslat jiný pomocí tohoto příkazu:'
invalid_name_case: 'Měl by jsi použít jméno %valid, ne jméno %invalid.'
# TODO tempban_max_logins: '&cYou have been temporarily banned for failing to log in too many times.'
# TODO accounts_owned_self: 'You own %count accounts:'
# TODO accounts_owned_other: 'The player %name has %count accounts:'
# TODO kicked_admin_registered: 'An admin just registered you; please log in again'
# TODO incomplete_email_settings: 'Error: not all required settings are set for sending emails. Please contact an admin.'
# TODO recovery_code_sent: 'A recovery code to reset your password has been sent to your email.'
# TODO recovery_code_incorrect: 'The recovery code is not correct! Use /email recovery [email] to generate a new one'

View File

@ -1,5 +1,6 @@
package tools.messages;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
@ -11,7 +12,6 @@ import tools.utils.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
@ -24,9 +24,8 @@ import java.util.Set;
*/
public class MessageFileVerifier {
private final String messagesFile;
private final File messagesFile;
private final Set<String> unknownKeys = new HashSet<>();
// Map with the missing key and a boolean indicating whether or not it was added to the file by this object
private final List<MissingKey> missingKeys = new ArrayList<>();
private final Multimap<String, String> missingTags = HashMultimap.create();
@ -35,7 +34,8 @@ public class MessageFileVerifier {
*
* @param messagesFile The messages file to process
*/
public MessageFileVerifier(String messagesFile) {
public MessageFileVerifier(File messagesFile) {
Preconditions.checkArgument(messagesFile.exists(), "Message file '" + messagesFile + "' does not exist");
this.messagesFile = messagesFile;
verifyKeys();
}
@ -70,7 +70,7 @@ public class MessageFileVerifier {
}
private void verifyKeys() {
FileConfiguration configuration = YamlConfiguration.loadConfiguration(new File(messagesFile));
FileConfiguration configuration = YamlConfiguration.loadConfiguration(messagesFile);
// Check known keys (their existence + presence of all tags)
for (MessageKey messageKey : MessageKey.values()) {
@ -104,8 +104,7 @@ public class MessageFileVerifier {
* @param defaultMessages The collection of default messages
*/
public void addMissingKeys(FileConfiguration defaultMessages) {
final List<String> fileLines = new ArrayList<>(
Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n")));
final List<String> fileLines = FileUtils.readLinesFromFile(messagesFile.toPath());
List<MissingKey> keysToAdd = new ArrayList<>();
for (MissingKey entry : missingKeys) {
@ -136,7 +135,7 @@ public class MessageFileVerifier {
addCommentForMissingTags(fileLines, key, entry.getValue());
}
FileUtils.writeToFile(messagesFile, String.join("\n", fileLines));
FileUtils.writeToFile(messagesFile.toPath(), String.join("\n", fileLines));
}
/**
@ -150,12 +149,7 @@ public class MessageFileVerifier {
private void addCommentForMissingTags(List<String> fileLines, final String key, Collection<String> tags) {
int indexForComment = Iterables.indexOf(fileLines, isCommentFor(key));
if (indexForComment == -1) {
indexForComment = Iterables.indexOf(fileLines, new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(key + ": ");
}
});
indexForComment = Iterables.indexOf(fileLines, input -> input.startsWith(key + ": "));
if (indexForComment == -1) {
System.err.println("Error adding comment for key '" + key + "': couldn't find entry in file lines");
return;
@ -174,12 +168,7 @@ public class MessageFileVerifier {
}
private static Predicate<String> isCommentFor(final String key) {
return new Predicate<String>() {
@Override
public boolean apply(String input) {
return input.startsWith(commentForKey(key));
}
};
return input -> input.startsWith(commentForKey(key));
}
private static boolean messageKeyExists(String key) {

View File

@ -17,8 +17,6 @@ import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static java.lang.String.format;
/**
* Task to verify the keys in the messages files.
*/
@ -28,8 +26,6 @@ public final class VerifyMessagesTask implements ToolTask {
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");
/** Tag that is replaced to the messages folder in user input. */
private static final String SOURCES_TAG = "{msgdir}";
/** File to get default messages from (assumes that it is complete). */
private static final String DEFAULT_MESSAGES_FILE = MESSAGES_FOLDER + "messages_en.yml";
@ -41,8 +37,8 @@ public final class VerifyMessagesTask implements ToolTask {
@Override
public void execute(Scanner scanner) {
System.out.println("Check a specific file only?");
System.out.println("Enter the language code for a specific file (e.g. 'es' for messages_es.yml)");
System.out.println("- Empty line will check all files in the resources messages folder (default)");
System.out.println(format("- %s will be replaced to the messages folder %s", SOURCES_TAG, MESSAGES_FOLDER));
String inputFile = scanner.nextLine();
System.out.println("Add any missing keys to files? ['y' = yes]");
@ -52,7 +48,7 @@ public final class VerifyMessagesTask implements ToolTask {
if (StringUtils.isEmpty(inputFile)) {
messageFiles = getMessagesFiles();
} else {
File customFile = new File(inputFile.replace(SOURCES_TAG, MESSAGES_FOLDER));
File customFile = new File(MESSAGES_FOLDER, "messages_" + inputFile + ".yml");
messageFiles = Collections.singletonList(customFile);
}
@ -64,7 +60,7 @@ public final class VerifyMessagesTask implements ToolTask {
// Verify the given files
for (File file : messageFiles) {
System.out.println("Verifying '" + file.getName() + "'");
MessageFileVerifier verifier = new MessageFileVerifier(file.getAbsolutePath());
MessageFileVerifier verifier = new MessageFileVerifier(file);
if (addMissingKeys) {
verifyFileAndAddKeys(verifier, defaultMessages);
} else {

View File

@ -89,7 +89,7 @@ public class ImportMessagesTask implements ToolTask {
throw new IllegalStateException(e);
}
MessageFileVerifier verifier = new MessageFileVerifier(fileName);
MessageFileVerifier verifier = new MessageFileVerifier(file);
VerifyMessagesTask.verifyFileAndAddKeys(verifier, YamlConfiguration.loadConfiguration(
new File(MESSAGES_FOLDER + "messages_en.yml")));
}

View File

@ -3,6 +3,7 @@ package tools.utils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.List;
@ -22,10 +23,14 @@ public final class FileUtils {
}
public static void writeToFile(String outputFile, String contents) {
writeToFile(Paths.get(outputFile), contents);
}
public static void writeToFile(Path path, String contents) {
try {
Files.write(Paths.get(outputFile), contents.getBytes());
Files.write(path, contents.getBytes());
} catch (IOException e) {
throw new UnsupportedOperationException("Failed to write to file '" + outputFile + "'", e);
throw new UnsupportedOperationException("Failed to write to file '" + path + "'", e);
}
}
@ -45,11 +50,11 @@ public final class FileUtils {
}
}
public static List<String> readLinesFromFile(String file) {
public static List<String> readLinesFromFile(Path path) {
try {
return Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
return Files.readAllLines(path, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
throw new UnsupportedOperationException("Could not read from file '" + path + "'", e);
}
}