From e81bcf9bf7f80fdc6cf8089ed0d34bd924649164 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 5 Dec 2015 14:13:20 +0100 Subject: [PATCH] Create GeneratedFileWriter and basic permission nodes list creater --- src/tools/README.md | 3 + .../PermissionNodesListCreater.java | 26 ++++++++ .../PermissionsListWriter.java | 37 +++++++++++ src/tools/permissionstree/README.md | 2 + src/tools/utils/CommentType.java | 11 ++++ src/tools/utils/GeneratedFileWriter.java | 61 +++++++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 src/tools/README.md create mode 100644 src/tools/permissionstree/PermissionNodesListCreater.java create mode 100644 src/tools/permissionstree/PermissionsListWriter.java create mode 100644 src/tools/permissionstree/README.md create mode 100644 src/tools/utils/CommentType.java create mode 100644 src/tools/utils/GeneratedFileWriter.java diff --git a/src/tools/README.md b/src/tools/README.md new file mode 100644 index 000000000..b61dabead --- /dev/null +++ b/src/tools/README.md @@ -0,0 +1,3 @@ +# About src/tools +This _tools_ folder provides helpers and extended tests useful during the development of AuthMe. +This folder is not included during the build of AuthMe. diff --git a/src/tools/permissionstree/PermissionNodesListCreater.java b/src/tools/permissionstree/PermissionNodesListCreater.java new file mode 100644 index 000000000..bfc1a3b73 --- /dev/null +++ b/src/tools/permissionstree/PermissionNodesListCreater.java @@ -0,0 +1,26 @@ +package permissionstree; + +import fr.xephi.authme.permission.AdminPermission; +import fr.xephi.authme.permission.PermissionNode; +import fr.xephi.authme.permission.PlayerPermission; + +import java.util.Set; +import java.util.TreeSet; + +/** + * Generate all permission nodes like a tree. + */ +public class PermissionNodesListCreater { + + public Set gatherNodes() { + Set nodes = new TreeSet<>(); + for (PermissionNode perm : PlayerPermission.values()) { + nodes.add(perm.getNode()); + } + for (PermissionNode perm : AdminPermission.values()) { + nodes.add(perm.getNode()); + } + return nodes; + } + +} diff --git a/src/tools/permissionstree/PermissionsListWriter.java b/src/tools/permissionstree/PermissionsListWriter.java new file mode 100644 index 000000000..2f3e40f59 --- /dev/null +++ b/src/tools/permissionstree/PermissionsListWriter.java @@ -0,0 +1,37 @@ +package permissionstree; + +import fr.xephi.authme.util.StringUtils; +import utils.CommentType; +import utils.GeneratedFileWriter; + +import java.util.Scanner; +import java.util.Set; + +/** + * Class responsible for formatting a permissions node list and + * for writing it to a file if desired. + */ +public class PermissionsListWriter { + + private static final String PERMISSIONS_TREE_FILE = "gen_permtree.txt"; + + public static void main(String[] args) { + // Ask if result should be written to file + Scanner scanner = new Scanner(System.in); + System.out.println("Write to file? [y = yes]"); + String answer = scanner.next(); + boolean writeToFile = "y".equalsIgnoreCase(answer); + + // Generate connections and output or write + PermissionNodesListCreater creater = new PermissionNodesListCreater(); + Set nodes = creater.gatherNodes(); + String output = StringUtils.join("\n", nodes); + + if (writeToFile) { + GeneratedFileWriter.createGeneratedFile(PERMISSIONS_TREE_FILE, output, CommentType.YML); + } else { + System.out.println(output); + } + } + +} diff --git a/src/tools/permissionstree/README.md b/src/tools/permissionstree/README.md new file mode 100644 index 000000000..5364c8469 --- /dev/null +++ b/src/tools/permissionstree/README.md @@ -0,0 +1,2 @@ +# About +Helper script to generate a list of all permission nodes. diff --git a/src/tools/utils/CommentType.java b/src/tools/utils/CommentType.java new file mode 100644 index 000000000..fbc4c9b89 --- /dev/null +++ b/src/tools/utils/CommentType.java @@ -0,0 +1,11 @@ +package utils; + +/** + * Enum for different comment formats. + */ +public enum CommentType { + + JAVA, + YML + +} diff --git a/src/tools/utils/GeneratedFileWriter.java b/src/tools/utils/GeneratedFileWriter.java new file mode 100644 index 000000000..07f87c43d --- /dev/null +++ b/src/tools/utils/GeneratedFileWriter.java @@ -0,0 +1,61 @@ +package utils; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.util.Date; + +/** + * Utility class for writing a generated file with a timestamp. + */ +public final class GeneratedFileWriter { + + private GeneratedFileWriter() { + } + + public static void createGeneratedFile(File file, String contents, CommentType commentFormat) { + validateFile(file); + + try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file))) { + osw.write(generateComment(commentFormat)); + osw.write(contents); + } catch (IOException e) { + throw new RuntimeException("Could not write to file '" + file.getName() + "'", e); + } + } + + public static void createGeneratedFile(String fileName, String contents, CommentType commentFormat) { + createGeneratedFile(new File(fileName), contents, commentFormat); + } + + private static String generateComment(CommentType commentFormat) { + String comment = "Auto-generated file, generated on " + new Date() + "\n\n"; + switch (commentFormat) { + case JAVA: + return "// " + comment; + case YML: + return "# " + comment; + default: + throw new RuntimeException("Unknown comment format '" + commentFormat + "'"); + } + } + + private static void validateFile(File file) { + if (!file.exists()) { + System.out.println("File '" + file.getName() + "' doesn't exist; attempting to create it"); + try { + boolean success = file.createNewFile(); + if (!success) { + throw new RuntimeException("Failed to create file '" + file.getName() + "'"); + } + } catch (IOException e) { + throw new RuntimeException("Could not create file '" + file.getName() + "'", e); + } + } + if (!file.canWrite()) { + throw new RuntimeException("File '" + file.getName() + "' is not writable"); + } + } + +}