Change message verification task to add todo comments in same order
- Make verification task add todo comments to YML files in the same order as the MessageKey enum - Use DefaultCharsets everywhere instead of Guava's Charsets class (thanks to DNx5)
This commit is contained in:
@@ -5,7 +5,6 @@ import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Multimap;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
import fr.xephi.authme.util.StringUtils;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import tools.utils.FileUtils;
|
||||
@@ -14,7 +13,6 @@ import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -29,7 +27,7 @@ public class MessageFileVerifier {
|
||||
private final String 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 Map<String, Boolean> missingKeys = new HashMap<>();
|
||||
private final List<MissingKey> missingKeys = new ArrayList<>();
|
||||
private final Multimap<String, String> missingTags = HashMultimap.create();
|
||||
|
||||
/**
|
||||
@@ -58,7 +56,7 @@ public class MessageFileVerifier {
|
||||
*
|
||||
* @return The list of missing keys in the file
|
||||
*/
|
||||
public Map<String, Boolean> getMissingKeys() {
|
||||
public List<MissingKey> getMissingKeys() {
|
||||
return missingKeys;
|
||||
}
|
||||
|
||||
@@ -80,7 +78,7 @@ public class MessageFileVerifier {
|
||||
if (configuration.isString(key)) {
|
||||
checkTagsInMessage(messageKey, configuration.getString(key));
|
||||
} else {
|
||||
missingKeys.put(key, false);
|
||||
missingKeys.add(new MissingKey(key));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,26 +107,27 @@ public class MessageFileVerifier {
|
||||
final List<String> fileLines = new ArrayList<>(
|
||||
Arrays.asList(FileUtils.readFromFile(messagesFile).split("\\n")));
|
||||
|
||||
List<String> keysToAdd = new ArrayList<>();
|
||||
for (Map.Entry<String, Boolean> entry : missingKeys.entrySet()) {
|
||||
List<MissingKey> keysToAdd = new ArrayList<>();
|
||||
for (MissingKey entry : missingKeys) {
|
||||
final String key = entry.getKey();
|
||||
|
||||
if (Boolean.FALSE.equals(entry.getValue()) && defaultMessages.get(key) != null) {
|
||||
keysToAdd.add(key);
|
||||
if (!entry.getWasAdded() && defaultMessages.get(key) != null) {
|
||||
keysToAdd.add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
// Add missing keys as comments to the bottom of the file
|
||||
for (String keyToAdd : keysToAdd) {
|
||||
int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(keyToAdd));
|
||||
for (MissingKey keyToAdd : keysToAdd) {
|
||||
final String key = keyToAdd.getKey();
|
||||
int indexOfComment = Iterables.indexOf(fileLines, isCommentFor(key));
|
||||
if (indexOfComment != -1) {
|
||||
// Comment for keyToAdd already exists, so remove it since we're going to add it
|
||||
fileLines.remove(indexOfComment);
|
||||
}
|
||||
String comment = commentForKey(keyToAdd) + "'" +
|
||||
defaultMessages.getString(keyToAdd).replace("'", "''") + "'";
|
||||
String comment = commentForKey(key) + "'" +
|
||||
defaultMessages.getString(key).replace("'", "''") + "'";
|
||||
fileLines.add(comment);
|
||||
missingKeys.put(keyToAdd, Boolean.TRUE);
|
||||
keyToAdd.setWasAdded(true);
|
||||
}
|
||||
|
||||
// Add a comment above messages missing a tag
|
||||
@@ -137,7 +136,7 @@ public class MessageFileVerifier {
|
||||
addCommentForMissingTags(fileLines, key, entry.getValue());
|
||||
}
|
||||
|
||||
FileUtils.writeToFile(messagesFile, StringUtils.join("\n", fileLines));
|
||||
FileUtils.writeToFile(messagesFile, String.join("\n", fileLines));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -167,7 +166,7 @@ public class MessageFileVerifier {
|
||||
|
||||
String tagWord = tags.size() > 1 ? "tags" : "tag";
|
||||
fileLines.add(indexForComment, commentForKey(key)
|
||||
+ String.format("Missing %s %s", tagWord, StringUtils.join(", ", tags)));
|
||||
+ String.format("Missing %s %s", tagWord, String.join(", ", tags)));
|
||||
}
|
||||
|
||||
private static String commentForKey(String key) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package tools.messages;
|
||||
|
||||
/**
|
||||
* Missing message key in a file.
|
||||
*/
|
||||
public class MissingKey {
|
||||
|
||||
private final String key;
|
||||
private boolean wasAdded;
|
||||
|
||||
public MissingKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the message key that is missing
|
||||
*/
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setWasAdded(boolean wasAdded) {
|
||||
this.wasAdded = wasAdded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if a comment was added to the file, false otherwise
|
||||
*/
|
||||
public boolean getWasAdded() {
|
||||
return wasAdded;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import java.util.Map;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
@@ -77,9 +78,9 @@ public final class VerifyMessagesTask implements ToolTask {
|
||||
}
|
||||
|
||||
private static void verifyFile(MessageFileVerifier verifier) {
|
||||
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
|
||||
List<MissingKey> missingKeys = verifier.getMissingKeys();
|
||||
if (!missingKeys.isEmpty()) {
|
||||
System.out.println(" Missing keys: " + missingKeys.keySet());
|
||||
System.out.println(" Missing keys: " + missingKeys);
|
||||
}
|
||||
|
||||
Set<String> unknownKeys = verifier.getUnknownKeys();
|
||||
@@ -94,13 +95,13 @@ public final class VerifyMessagesTask implements ToolTask {
|
||||
}
|
||||
|
||||
public static void verifyFileAndAddKeys(MessageFileVerifier verifier, FileConfiguration defaultMessages) {
|
||||
Map<String, Boolean> missingKeys = verifier.getMissingKeys();
|
||||
List<MissingKey> missingKeys = verifier.getMissingKeys();
|
||||
if (!missingKeys.isEmpty() || !verifier.getMissingTags().isEmpty()) {
|
||||
verifier.addMissingKeys(defaultMessages);
|
||||
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys);
|
||||
List<String> addedKeys = getMissingKeysWithAdded(missingKeys, true);
|
||||
System.out.println(" Added missing keys " + addedKeys);
|
||||
|
||||
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys);
|
||||
List<String> unsuccessfulKeys = getMissingKeysWithAdded(missingKeys, false);
|
||||
if (!unsuccessfulKeys.isEmpty()) {
|
||||
System.err.println(" Warning! Could not add all missing keys (problem with loading " +
|
||||
"default messages?)");
|
||||
@@ -119,14 +120,11 @@ public final class VerifyMessagesTask implements ToolTask {
|
||||
}
|
||||
}
|
||||
|
||||
private static <K, V> List<K> getKeysWithValue(V value, Map<K, V> map) {
|
||||
List<K> result = new ArrayList<>();
|
||||
for (Map.Entry<K, V> entry : map.entrySet()) {
|
||||
if (value.equals(entry.getValue())) {
|
||||
result.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return result;
|
||||
private static List<String> getMissingKeysWithAdded(List<MissingKey> missingKeys, boolean wasAdded) {
|
||||
return missingKeys.stream()
|
||||
.filter(e -> e.getWasAdded() == wasAdded)
|
||||
.map(MissingKey::getKey)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<File> getMessagesFiles() {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package tools.messages.translation;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Resources;
|
||||
import com.google.gson.Gson;
|
||||
import fr.xephi.authme.output.MessageKey;
|
||||
@@ -15,6 +14,7 @@ import tools.utils.ToolsConstants;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashSet;
|
||||
import java.util.Scanner;
|
||||
import java.util.Set;
|
||||
@@ -56,7 +56,7 @@ public class ImportMessagesTask implements ToolTask {
|
||||
private LanguageExport getLanguageExportFromUrl(String location) {
|
||||
try {
|
||||
URL url = new URL(location);
|
||||
String json = Resources.toString(url, Charsets.UTF_8);
|
||||
String json = Resources.toString(url, StandardCharsets.UTF_8);
|
||||
return gson.fromJson(json, LanguageExport.class);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package tools.utils;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
@@ -40,7 +39,7 @@ public final class FileUtils {
|
||||
|
||||
public static String readFromFile(String file) {
|
||||
try {
|
||||
return new String(Files.readAllBytes(Paths.get(file)), Charsets.UTF_8);
|
||||
return new String(Files.readAllBytes(Paths.get(file)), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
|
||||
}
|
||||
@@ -48,7 +47,7 @@ public final class FileUtils {
|
||||
|
||||
public static List<String> readLinesFromFile(String file) {
|
||||
try {
|
||||
return Files.readAllLines(Paths.get(file), Charsets.UTF_8);
|
||||
return Files.readAllLines(Paths.get(file), StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user