Close Scanner in tools/permissions; make use of more generic services

- Close scanner for all cases
- Replace read from file with the one from the File service
- Rename file service class to FileUtils to better reflect its purpose
This commit is contained in:
ljacqu 2015-12-08 22:08:22 +01:00
parent 595879b95e
commit 6a503772e2
3 changed files with 17 additions and 32 deletions

View File

@ -3,12 +3,9 @@ package permissions;
import fr.xephi.authme.permission.AdminPermission;
import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PlayerPermission;
import fr.xephi.authme.util.StringUtils;
import utils.FileUtils;
import utils.ToolsConstants;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
@ -23,10 +20,6 @@ import java.util.regex.Pattern;
*/
public class PermissionNodesGatherer {
/** The folder in which the implementations of {@link PermissionNode} reside. */
private static final String PERMISSION_NODE_SOURCE_FOLDER =
"src/main/java/fr/xephi/authme/permission/";
/**
* Regular expression that should match the JavaDoc comment above an enum, <i>including</i>
* the name of the enum value. The first group (i.e. {@code \\1}) should be the JavaDoc description;
@ -100,14 +93,8 @@ public class PermissionNodesGatherer {
* @return Source code of the file
*/
private static <T extends Enum<T> & PermissionNode> String getSourceForClass(Class<T> clazz) {
String classFile = PERMISSION_NODE_SOURCE_FOLDER + clazz.getSimpleName() + ".java";
Charset cs = Charset.forName("utf-8");
try {
return StringUtils.join("\n",
Files.readAllLines(Paths.get(classFile), cs));
} catch (IOException e) {
throw new RuntimeException("Failed to get the source for class '" + clazz.getSimpleName() + "'");
}
String classFile = ToolsConstants.MAIN_SOURCE_ROOT + clazz.getName().replace(".", "/") + ".java";
return FileUtils.readFromFile(classFile);
}
}

View File

@ -1,7 +1,7 @@
package permissions;
import utils.ANewMap;
import utils.GeneratedFileWriter;
import utils.FileUtils;
import utils.TagReplacer;
import utils.ToolsConstants;
@ -23,17 +23,16 @@ public class PermissionsListWriter {
System.out.println("Include description? [Enter 'n' for no]");
boolean includeDescription = !matches("n", scanner);
if (!includeDescription) {
outputSimpleList();
return;
boolean writeToFile = false;
if (includeDescription) {
System.out.println("Write to file? [Enter 'n' for console output]");
writeToFile = !matches("n", scanner);
}
System.out.println("Write to file? [Enter 'n' for console output]");
boolean writeToFile = !matches("n", scanner);
scanner.close();
if (writeToFile) {
if (!includeDescription) {
outputSimpleList();
} else if (writeToFile) {
generateAndWriteFile();
} else {
System.out.println(generatePermissionsList());
@ -45,7 +44,7 @@ public class PermissionsListWriter {
final String permissionsTagValue = generatePermissionsList();
Map<String, Object> tags = ANewMap.<String, Object>with("permissions", permissionsTagValue).build();
GeneratedFileWriter.generateFileFromTemplate(
FileUtils.generateFileFromTemplate(
ToolsConstants.TOOLS_SOURCE_ROOT + "permissions/permission_nodes.tpl.md", PERMISSIONS_OUTPUT_FILE, tags);
System.out.println("Wrote to '" + PERMISSIONS_OUTPUT_FILE + "'");
System.out.println("Before committing, please verify the output!");
@ -55,7 +54,7 @@ public class PermissionsListWriter {
PermissionNodesGatherer gatherer = new PermissionNodesGatherer();
Map<String, String> permissions = gatherer.gatherNodesWithJavaDoc();
final String template = GeneratedFileWriter.readFromToolsFile("permissions/permission_node_entry.tpl.md");
final String template = FileUtils.readFromToolsFile("permissions/permission_node_entry.tpl.md");
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : permissions.entrySet()) {

View File

@ -7,13 +7,13 @@ import java.nio.file.Paths;
import java.util.Map;
/**
* Utility class for writing a generated file with a timestamp.
* Utility class for reading from and writing to files.
*/
public final class GeneratedFileWriter {
public final class FileUtils {
private final static Charset CHARSET = Charset.forName("utf-8");
private GeneratedFileWriter() {
private FileUtils() {
}
public static void generateFileFromTemplate(String templateFile, String destinationFile, Map<String, Object> tags) {
@ -43,5 +43,4 @@ public final class GeneratedFileWriter {
return readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT + file);
}
}