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.AdminPermission;
import fr.xephi.authme.permission.PermissionNode; import fr.xephi.authme.permission.PermissionNode;
import fr.xephi.authme.permission.PlayerPermission; 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.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -23,10 +20,6 @@ import java.util.regex.Pattern;
*/ */
public class PermissionNodesGatherer { 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> * 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; * 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 * @return Source code of the file
*/ */
private static <T extends Enum<T> & PermissionNode> String getSourceForClass(Class<T> clazz) { private static <T extends Enum<T> & PermissionNode> String getSourceForClass(Class<T> clazz) {
String classFile = PERMISSION_NODE_SOURCE_FOLDER + clazz.getSimpleName() + ".java"; String classFile = ToolsConstants.MAIN_SOURCE_ROOT + clazz.getName().replace(".", "/") + ".java";
Charset cs = Charset.forName("utf-8"); return FileUtils.readFromFile(classFile);
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() + "'");
}
} }
} }

View File

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

View File

@ -7,13 +7,13 @@ import java.nio.file.Paths;
import java.util.Map; 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 final static Charset CHARSET = Charset.forName("utf-8");
private GeneratedFileWriter() { private FileUtils() {
} }
public static void generateFileFromTemplate(String templateFile, String destinationFile, Map<String, Object> tags) { 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); return readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT + file);
} }
} }