mirror of
https://github.com/AuthMe/AuthMeReloaded.git
synced 2024-12-25 01:57:48 +01:00
Create GeneratedFileWriter and basic permission nodes list creater
This commit is contained in:
parent
a8b3fe2648
commit
e81bcf9bf7
3
src/tools/README.md
Normal file
3
src/tools/README.md
Normal file
@ -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.
|
26
src/tools/permissionstree/PermissionNodesListCreater.java
Normal file
26
src/tools/permissionstree/PermissionNodesListCreater.java
Normal file
@ -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<String> gatherNodes() {
|
||||
Set<String> nodes = new TreeSet<>();
|
||||
for (PermissionNode perm : PlayerPermission.values()) {
|
||||
nodes.add(perm.getNode());
|
||||
}
|
||||
for (PermissionNode perm : AdminPermission.values()) {
|
||||
nodes.add(perm.getNode());
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
37
src/tools/permissionstree/PermissionsListWriter.java
Normal file
37
src/tools/permissionstree/PermissionsListWriter.java
Normal file
@ -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<String> nodes = creater.gatherNodes();
|
||||
String output = StringUtils.join("\n", nodes);
|
||||
|
||||
if (writeToFile) {
|
||||
GeneratedFileWriter.createGeneratedFile(PERMISSIONS_TREE_FILE, output, CommentType.YML);
|
||||
} else {
|
||||
System.out.println(output);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
2
src/tools/permissionstree/README.md
Normal file
2
src/tools/permissionstree/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# About
|
||||
Helper script to generate a list of all permission nodes.
|
11
src/tools/utils/CommentType.java
Normal file
11
src/tools/utils/CommentType.java
Normal file
@ -0,0 +1,11 @@
|
||||
package utils;
|
||||
|
||||
/**
|
||||
* Enum for different comment formats.
|
||||
*/
|
||||
public enum CommentType {
|
||||
|
||||
JAVA,
|
||||
YML
|
||||
|
||||
}
|
61
src/tools/utils/GeneratedFileWriter.java
Normal file
61
src/tools/utils/GeneratedFileWriter.java
Normal file
@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user