Ugly but working generator for rst commands.

This commit is contained in:
wizjany 2019-06-02 23:39:01 -04:00
parent 1b4ea528ea
commit dc380b1fb3

View File

@ -19,34 +19,21 @@
package com.sk89q.worldedit.internal.util; package com.sk89q.worldedit.internal.util;
import com.sk89q.minecraft.util.commands.Command; import com.google.common.base.Strings;
import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.worldedit.WorldEdit;
import com.sk89q.minecraft.util.commands.NestedCommand; import com.sk89q.worldedit.command.util.PermissionCondition;
import com.sk89q.worldedit.command.BiomeCommands; import com.sk89q.worldedit.extension.platform.PlatformCommandManager;
import com.sk89q.worldedit.command.ChunkCommands; import com.sk89q.worldedit.extension.platform.PlatformManager;
import com.sk89q.worldedit.command.ClipboardCommands; import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.command.GeneralCommands; import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import com.sk89q.worldedit.command.GenerationCommands; import com.sk89q.worldedit.util.formatting.text.serializer.plain.PlainComponentSerializer;
import com.sk89q.worldedit.command.HistoryCommands; import org.enginehub.piston.Command;
import com.sk89q.worldedit.command.NavigationCommands; import org.enginehub.piston.CommandManager;
import com.sk89q.worldedit.command.RegionCommands; import org.enginehub.piston.TextConfig;
import com.sk89q.worldedit.command.ScriptingCommands; import org.enginehub.piston.part.SubCommandPart;
import com.sk89q.worldedit.command.SelectionCommands;
import com.sk89q.worldedit.command.SnapshotUtilCommands;
import com.sk89q.worldedit.command.ToolCommands;
import com.sk89q.worldedit.command.ToolUtilCommands;
import com.sk89q.worldedit.command.UtilityCommands;
import java.io.File; import java.util.stream.Stream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public final class DocumentationPrinter { public final class DocumentationPrinter {
private DocumentationPrinter() { private DocumentationPrinter() {
@ -56,153 +43,49 @@ public final class DocumentationPrinter {
* Generates documentation. * Generates documentation.
* *
* @param args arguments * @param args arguments
* @throws IOException thrown on I/O error
*/ */
public static void main(String[] args) throws IOException { public static void main(String[] args) {
File commandsDir = new File(args[0]); final PlatformManager platformManager = WorldEdit.getInstance().getPlatformManager();
PlatformCommandManager mgr = platformManager.getPlatformCommandManager();
List<Class<?>> commandClasses = getCommandClasses(commandsDir); final CommandManager commandManager = mgr.getCommandManager();
dumpCommands(commandManager);
System.out.println("Writing permissions wiki table...");
writePermissionsWikiTable(commandClasses);
System.out.println("Writing Bukkit plugin.yml...");
writeBukkitYAML();
System.out.println("Done!");
} }
private static List<Class<?>> getCommandClasses(File dir) { private static void dumpCommands(CommandManager commandManager) {
List<Class<?>> classes = new ArrayList<>(); final PlainComponentSerializer serializer = new PlainComponentSerializer(kbc -> "", TranslatableComponent::key);
cmdToUsages(serializer, commandManager.getAllCommands(), TextConfig.getCommandPrefix(), 0);
classes.add(BiomeCommands.class); cmdsToPerms(commandManager.getAllCommands(), TextConfig.getCommandPrefix());
classes.add(ChunkCommands.class);
classes.add(ClipboardCommands.class);
classes.add(GeneralCommands.class);
classes.add(GenerationCommands.class);
classes.add(HistoryCommands.class);
classes.add(NavigationCommands.class);
classes.add(RegionCommands.class);
classes.add(ScriptingCommands.class);
classes.add(SelectionCommands.class);
classes.add(SnapshotUtilCommands.class);
classes.add(ToolUtilCommands.class);
classes.add(ToolCommands.class);
classes.add(UtilityCommands.class);
/*for (File f : dir.listFiles()) {
if (!f.getName().matches("^.*\\.java$")) {
continue;
} }
String className = "com.sk89q.worldedit.commands." private static void cmdsToPerms(Stream<Command> cmds, String prefix) {
+ f.getName().substring(0, f.getName().lastIndexOf(".")); cmds.forEach(c -> {
System.out.println(" " + cmdToPerm(prefix, c));
Class<?> cls; c.getParts().stream().filter(p -> p instanceof SubCommandPart).map(p -> (SubCommandPart) p)
try { .forEach(scp -> cmdsToPerms(scp.getCommands().stream(), prefix + c.getName() + " "));
cls = Class.forName(className, true, });
Thread.currentThread().getContextClassLoader());
} catch (ClassNotFoundException e) {
continue;
} }
classes.add(cls); private static String cmdToPerm(String prefix, Command c) {
}*/ return prefix + c.getName() + ",\"" + (c.getCondition() instanceof PermissionCondition
? String.join(", ", ((PermissionCondition) c.getCondition()).getPermissions()) : "") + "\"";
return classes;
} }
private static void writePermissionsWikiTable(List<Class<?>> commandClasses) private static void cmdToUsages(PlainComponentSerializer serializer, Stream<Command> cmds, String prefix, int indent) {
throws IOException { cmds.forEach(c -> {
try (FileOutputStream stream = new FileOutputStream("wiki_permissions.txt")) { System.out.println(Strings.repeat("\t", indent) + cmdToString(serializer, prefix, c, indent));
PrintStream print = new PrintStream(stream); c.getParts().stream().filter(p -> p instanceof SubCommandPart).map(p -> (SubCommandPart) p)
writePermissionsWikiTable(print, commandClasses, "/"); .forEach(scp -> cmdToUsages(serializer, scp.getCommands().stream(), prefix + c.getName() + " ", indent + 1));
} System.out.println();
});
} }
private static void writePermissionsWikiTable(PrintStream stream, private static String cmdToString(PlainComponentSerializer serializer, String prefix, Command c, int indent) {
List<Class<?>> commandClasses, String prefix) { return serializer.serialize(TextComponent.of(prefix + c.getName()).append(TextComponent.newline())
.append(TextComponent.of(c.getCondition() instanceof PermissionCondition
for (Class<?> cls : commandClasses) { ? "Permissions: " + (String.join(", ", ((PermissionCondition) c.getCondition()).getPermissions())) + "\n"
for (Method method : cls.getMethods()) { : ""))
if (!method.isAnnotationPresent(Command.class)) { .append(c.getFullHelp())).replace("\n", "\n" + Strings.repeat("\t", indent));
continue;
}
Command cmd = method.getAnnotation(Command.class);
stream.println("|-");
stream.print("| " + prefix + cmd.aliases()[0]);
stream.print(" || ");
if (method.isAnnotationPresent(CommandPermissions.class)) {
CommandPermissions perms =
method.getAnnotation(CommandPermissions.class);
String[] permKeys = perms.value();
for (int i = 0; i < permKeys.length; ++i) {
if (i > 0) {
stream.print(", ");
}
stream.print(permKeys[i]);
}
}
stream.print(" || ");
boolean firstAlias = true;
if (cmd.aliases().length != 0) {
for (String alias : cmd.aliases()) {
if (!firstAlias) stream.print("<br />");
stream.print(prefix + alias);
firstAlias = false;
}
}
stream.print(" || ");
if (cmd.flags() != null && !cmd.flags().isEmpty()) {
stream.print(cmd.flags());
}
stream.print(" || ");
if (cmd.desc() != null && !cmd.desc().isEmpty()) {
stream.print(cmd.desc());
}
stream.println();
if (method.isAnnotationPresent(NestedCommand.class)) {
NestedCommand nested =
method.getAnnotation(NestedCommand.class);
Class<?>[] nestedClasses = nested.value();
writePermissionsWikiTable(stream,
Arrays.asList(nestedClasses),
prefix + cmd.aliases()[0] + " ");
}
}
}
}
private static void writeBukkitYAML()
throws IOException {
try (FileOutputStream stream = new FileOutputStream("plugin.yml")) {
PrintStream print = new PrintStream(stream);
writeBukkitYAML(print);
}
}
private static void writeBukkitYAML(PrintStream stream) {
stream.println("name: WorldEdit");
stream.println("main: com.sk89q.worldedit.bukkit.WorldEditPlugin");
stream.println("version: ${project.version}");
stream.println("softdepend: [Spout] #hack to fix trove errors");
stream.println();
stream.println();
stream.println("# Permissions aren't here. Read http://wiki.sk89q.com/wiki/WEPIF/DinnerPerms");
stream.println("# for how WorldEdit permissions actually work.");
} }
} }