Minor refactoring for tool tasks

- Don't scan for translations on initialization in TranslationPageGenerator in order to speed up startup time of ToolsRunner
- Extract checking for null / empty array of File#listFiles into a separate method
- Move single method of RuntimeUtils into Utils class
This commit is contained in:
ljacqu
2016-10-30 10:43:59 +01:00
parent 195e409efd
commit 46af922fba
10 changed files with 45 additions and 58 deletions
@@ -34,8 +34,6 @@ public class TranslationPageGenerator implements AutoToolTask {
private static final int[] COLOR_1 = {12, 9, 0};
private static final int[] COLOR_2 = { 6, 15, 6};
private final TranslationsGatherer gatherer = new TranslationsGatherer();
@Override
public String getTaskName() {
return "updateTranslations";
@@ -43,6 +41,7 @@ public class TranslationPageGenerator implements AutoToolTask {
@Override
public void executeDefault() {
TranslationsGatherer gatherer = new TranslationsGatherer();
NestedTagValue translationValuesHolder = new NestedTagValue();
for (TranslationInfo translation : gatherer.getTranslationInfo()) {
@@ -11,6 +11,8 @@ import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Gathers all available translations of AuthMe.
*/
@@ -31,10 +33,7 @@ public class TranslationsGatherer {
}
private void gatherTranslations() {
File[] files = new File(MESSAGES_FOLDER).listFiles();
if (files == null) {
throw new IllegalStateException("Cannot read files of '" + MESSAGES_FOLDER + "'");
}
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
for (File file : files) {
String code = getLanguageCode(file.getName());
if (code != null) {
@@ -10,6 +10,8 @@ import java.util.Scanner;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Verifies the help translations for validity and completeness.
*/
@@ -57,10 +59,7 @@ public class VerifyHelpTranslations implements ToolTask {
}
private static List<File> getHelpTranslations() {
File[] files = new File(FOLDER).listFiles();
if (files == null) {
throw new IllegalStateException("Could not get files from '" + FOLDER + "'");
}
File[] files = listFilesOrThrow(new File(FOLDER));
List<File> helpFiles = Arrays.stream(files)
.filter(file -> HELP_MESSAGE_PATTERN.matcher(file.getName()).matches())
.collect(Collectors.toList());
@@ -17,6 +17,8 @@ import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Task to verify the keys in the messages files.
*/
@@ -124,12 +126,7 @@ public final class VerifyMessagesTask implements ToolTask {
}
private static List<File> getMessagesFiles() {
File folder = new File(MESSAGES_FOLDER);
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Could not read files from folder '" + folder.getName() + "'");
}
File[] files = listFilesOrThrow(new File(MESSAGES_FOLDER));
List<File> messageFiles = new ArrayList<>();
for (File file : files) {
if (MESSAGE_FILE_PATTERN.matcher(file.getName()).matches()) {
@@ -8,6 +8,8 @@ import tools.utils.ToolsConstants;
import java.io.File;
import java.util.Scanner;
import static tools.utils.FileIoUtils.listFilesOrThrow;
/**
* Task which exports all messages to a local folder.
*/
@@ -22,11 +24,7 @@ public class WriteAllExportsTask extends ExportMessagesTask {
@Override
public void execute(Scanner scanner) {
File[] messageFiles = new File(MESSAGES_FOLDER).listFiles();
if (messageFiles == null || messageFiles.length == 0) {
throw new IllegalStateException("Could not read messages folder");
}
final File[] messageFiles = listFilesOrThrow(new File(MESSAGES_FOLDER));
final FileConfiguration defaultMessages = loadDefaultMessages();
for (File file : messageFiles) {
String code = file.getName().substring("messages_".length(), file.getName().length() - ".yml".length());
@@ -1,5 +1,6 @@
package tools.utils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -62,4 +63,19 @@ public final class FileIoUtils {
}
}
/**
* Returns a folder's files or throws an exception if the folder could not be read or if it is empty.
*
* @param folder the folder to read
* @return the files in the folder
*/
public static File[] listFilesOrThrow(File folder) {
File[] files = folder.listFiles();
if (files == null) {
throw new IllegalStateException("Could not read folder '" + folder + "'");
} else if (files.length == 0) {
throw new IllegalStateException("Folder '" + folder + "' is empty");
}
return files;
}
}