Tools: Remove I/O without user confirmation, minor refactoring

- Permissions: Do not write to generated file without explicit consent by user (default behavior of runners should be _not_ to do any I/O)
- Fix wrong check in messages; minor output adjustment
- Remove hacky check to see if last char of a file is a new line
This commit is contained in:
ljacqu 2015-12-10 20:24:20 +01:00
parent 3253b43a63
commit 52b61b10f6
3 changed files with 14 additions and 17 deletions

View File

@ -16,7 +16,7 @@ import java.util.Set;
*/ */
public class MessageFileVerifier { public class MessageFileVerifier {
private static final char NEW_LINE = '\n'; private static final String NEW_LINE = "\n";
private final String messagesFile; private final String messagesFile;
private final Set<String> unknownKeys = new HashSet<>(); private final Set<String> unknownKeys = new HashSet<>();
@ -96,20 +96,13 @@ public class MessageFileVerifier {
} }
} }
// Very ugly way of verifying if the last char in the file is a new line, in which case we won't start by // We know that all keys in keysToAdd are safe to retrieve and write
// adding a new line to the file. It's grossly inefficient but with the scale of the messages file it's fine StringBuilder sb = new StringBuilder(NEW_LINE);
String fileContents = FileUtils.readFromFile(messagesFile);
String contentsToAdd = "";
if (fileContents.charAt(fileContents.length() - 1) == NEW_LINE) {
contentsToAdd += NEW_LINE;
}
// We know that all keys in keysToAdd are safe to retrieve and add
for (String keyToAdd : keysToAdd) { for (String keyToAdd : keysToAdd) {
contentsToAdd += keyToAdd + ":" + defaultMessages.get(keyToAdd) + NEW_LINE; sb.append(keyToAdd).append(":").append(defaultMessages.get(keyToAdd)).append(NEW_LINE);
missingKeys.put(keyToAdd, true); missingKeys.put(keyToAdd, true);
} }
FileUtils.appendToFile(messagesFile, contentsToAdd); FileUtils.appendToFile(messagesFile, sb.toString());
} }
private static Set<String> getAllMessageKeys() { private static Set<String> getAllMessageKeys() {

View File

@ -21,7 +21,7 @@ import static java.lang.String.format;
*/ */
public final class MessagesVerifierRunner { public final class MessagesVerifierRunner {
public static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/"; private static final String MESSAGES_FOLDER = ToolsConstants.MAIN_RESOURCES_ROOT + "messages/";
private static final String SOURCES_TAG = "{msgdir}"; private static final String SOURCES_TAG = "{msgdir}";
private MessagesVerifierRunner() { private MessagesVerifierRunner() {
@ -63,11 +63,15 @@ public final class MessagesVerifierRunner {
verifyFile(verifier); verifyFile(verifier);
} }
} }
if (messageFiles.size() > 1) {
System.out.println("Checked " + messageFiles.size() + " files");
}
} }
private static void verifyFile(MessageFileVerifier verifier) { private static void verifyFile(MessageFileVerifier verifier) {
Map<String, Boolean> missingKeys = verifier.getMissingKeys(); Map<String, Boolean> missingKeys = verifier.getMissingKeys();
if (missingKeys.isEmpty()) { if (!missingKeys.isEmpty()) {
System.out.println(" Missing keys: " + missingKeys.keySet()); System.out.println(" Missing keys: " + missingKeys.keySet());
} }
@ -83,7 +87,7 @@ public final class MessagesVerifierRunner {
verifier.addMissingKeys(defaultMessages); verifier.addMissingKeys(defaultMessages);
missingKeys = verifier.getMissingKeys(); missingKeys = verifier.getMissingKeys();
List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys); List<String> addedKeys = getKeysWithValue(Boolean.TRUE, missingKeys);
System.out.println("Could add missing keys " + addedKeys); System.out.println(" Added missing keys " + addedKeys);
List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys); List<String> unsuccessfulKeys = getKeysWithValue(Boolean.FALSE, missingKeys);
if (!unsuccessfulKeys.isEmpty()) { if (!unsuccessfulKeys.isEmpty()) {

View File

@ -25,8 +25,8 @@ public class PermissionsListWriter {
boolean writeToFile = false; boolean writeToFile = false;
if (includeDescription) { if (includeDescription) {
System.out.println("Write to file? [Enter 'n' for console output]"); System.out.println("Write to file? [Enter 'y' for yes]");
writeToFile = !matches("n", scanner); writeToFile = matches("y", scanner);
} }
scanner.close(); scanner.close();