TF-PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/GenerateDocs.java

188 lines
7.4 KiB
Java
Raw Normal View History

2015-08-26 16:44:50 +10:00
package com.intellectualcrafters.plot.commands;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
2015-09-29 16:29:28 +10:00
import java.util.Arrays;
import java.util.HashSet;
2015-08-26 16:44:50 +10:00
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2015-09-29 16:29:28 +10:00
import com.intellectualcrafters.plot.config.C;
2015-08-26 16:44:50 +10:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-09-29 16:29:28 +10:00
import com.intellectualcrafters.plot.util.Permissions;
2015-08-26 16:44:50 +10:00
import com.intellectualcrafters.plot.util.StringMan;
import com.plotsquared.general.commands.Command;
2015-09-13 14:04:31 +10:00
public class GenerateDocs {
public static void main(final String[] args) {
2015-08-26 16:44:50 +10:00
MainCommand.getInstance().addCommand(new WE_Anywhere());
MainCommand.getInstance().addCommand(new Cluster());
2015-09-11 20:09:22 +10:00
final ArrayList<Command<PlotPlayer>> commands = MainCommand.getInstance().getCommands();
2015-08-26 16:44:50 +10:00
log("### Want to document some commands?");
log(" - This page is automatically generated");
log(" - Fork the project and add a javadoc comment to one of the command classes");
log(" - Then do a pull request and it will be added to this page");
log("");
log("# Contents");
2015-09-13 14:04:31 +10:00
for (final CommandCategory category : CommandCategory.values()) {
2015-08-26 16:44:50 +10:00
log("###### " + category.name());
2015-09-13 14:04:31 +10:00
for (final Command<PlotPlayer> command : MainCommand.getCommands(category, null)) {
2015-09-11 20:09:22 +10:00
log(" - [/plot " + command.getCommand() + "](https://github.com/IntellectualSites/PlotSquared/wiki/Commands#" + command.getCommand() + ") ");
2015-08-26 16:44:50 +10:00
}
log("");
}
log("# Commands");
2015-09-13 14:04:31 +10:00
for (final Command<PlotPlayer> command : commands) {
2015-08-26 16:44:50 +10:00
printCommand(command);
}
}
2015-09-13 14:04:31 +10:00
public static void printCommand(final Command<PlotPlayer> command) {
try {
2015-09-11 20:09:22 +10:00
final String clazz = command.getClass().getSimpleName();
final String name = command.getCommand();
2015-09-13 14:04:31 +10:00
2015-08-26 16:44:50 +10:00
// Header
2015-09-11 20:09:22 +10:00
final String source = "https://github.com/IntellectualSites/PlotSquared/tree/master/src/main/java/com/intellectualcrafters/plot/commands/" + clazz + ".java";
2015-08-26 16:44:50 +10:00
log("## [" + name.toUpperCase() + "](" + source + ") ");
2015-09-13 14:04:31 +10:00
2015-09-11 20:09:22 +10:00
final File file = new File("src/main/java/com/intellectualcrafters/plot/commands/" + clazz + ".java");
final List<String> lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8);
final List<String> perms = getPerms(name, lines);
final String comment = getComments(lines);
2015-09-13 14:04:31 +10:00
2015-08-26 16:44:50 +10:00
log("#### Description");
log("`" + command.getDescription() + "`");
2015-09-13 14:04:31 +10:00
if (comment.length() > 0) {
2015-08-26 16:44:50 +10:00
log("##### Comments");
log("``` java");
log(comment);
log("```");
}
2015-09-13 14:04:31 +10:00
2015-08-26 16:44:50 +10:00
log("#### Usage");
log("`" + command.getUsage().replaceAll("\\{label\\}", "plot") + "`");
2015-09-13 14:04:31 +10:00
if (command.getRequiredType() != RequiredType.NONE) {
2015-08-26 16:44:50 +10:00
log("#### Required callers");
log("`" + command.getRequiredType().name() + "`");
}
2015-09-13 14:04:31 +10:00
2015-09-11 20:09:22 +10:00
final Set<String> aliases = command.getAliases();
2015-09-13 14:04:31 +10:00
if (aliases.size() > 0) {
2015-08-26 16:44:50 +10:00
log("#### Aliases");
log("`" + StringMan.getString(command.getAliases()) + "`");
}
2015-09-13 14:04:31 +10:00
2015-08-26 16:44:50 +10:00
log("#### Permissions");
log("##### Primary");
log(" - `" + command.getPermission() + "` ");
2015-09-13 14:04:31 +10:00
if (perms.size() > 0) {
2015-08-26 16:44:50 +10:00
log("");
log("##### Other");
log(" - `" + StringMan.join(perms, "`\n - `") + "`");
}
log("");
log("***");
log("");
2015-09-13 14:04:31 +10:00
} catch (final Exception e) {
2015-08-26 16:44:50 +10:00
e.printStackTrace();
}
}
2015-09-13 14:04:31 +10:00
public static List<String> getPerms(final String cmd, final List<String> lines) {
2015-09-29 16:29:28 +10:00
final HashSet<String> perms = new HashSet<String>();
2015-09-11 20:09:22 +10:00
final Pattern p = Pattern.compile("\"([^\"]*)\"");
2015-09-29 16:29:28 +10:00
final Pattern p2 = Pattern.compile("C.PERMISSION_\\s*(\\w+)");
String last = null;
2015-09-13 14:04:31 +10:00
for (final String line : lines) {
2015-09-29 16:29:28 +10:00
Matcher m2 = p2.matcher(line);
while (m2.find()) {
perms.add(C.valueOf("PERMISSION_" + m2.group(1)).s());
}
2015-09-13 14:04:31 +10:00
if (line.contains("Permissions.hasPermission(")) {
2015-09-29 16:29:28 +10:00
String[] split = line.split("Permissions.hasPermission");
split = Arrays.copyOfRange(split, 1, split.length);
for (String method : split) {
String perm = method.split("[,|)]")[1].trim();
if (!perm.toLowerCase().equals(perm)) {
if (perm.startsWith("C.")) {
perm = C.valueOf(perm.split("\\.")[1]).s();
}
else {
continue;
}
}
else {
perm = perm.substring(1, perm.length() - 1);
}
perms.add(perm);
}
2015-09-11 20:09:22 +10:00
final Matcher m = p.matcher(line);
2015-09-13 14:04:31 +10:00
while (m.find()) {
2015-08-26 16:44:50 +10:00
String perm = m.group(1);
2015-09-13 14:04:31 +10:00
if (perm.endsWith(".")) {
2015-08-26 16:44:50 +10:00
perm += "<arg>";
}
2015-09-13 14:04:31 +10:00
if (perm.startsWith(".")) {
2015-09-29 16:29:28 +10:00
perms.remove(last);
perms.add(last + perm);
2015-09-13 14:04:31 +10:00
} else if (perm.contains(".")) {
2015-09-29 16:29:28 +10:00
last = perm;
2015-08-26 16:44:50 +10:00
perms.add(perm);
}
}
}
2015-09-29 16:29:28 +10:00
else if (line.contains("Permissions.hasPermissionRange")) {
String[] split = line.split("Permissions.hasPermissionRange");
split = Arrays.copyOfRange(split, 1, split.length);
for (String method : split) {
String perm = method.split("[,|)]")[1].trim();
if (!perm.toLowerCase().equals(perm)) {
if (perm.startsWith("C.")) {
perm = C.valueOf(perm.split("\\.")[1]).s();
}
else {
continue;
}
}
else {
perm = perm.substring(1, perm.length() - 1);
}
perms.add(perm + ".<#>");
}
}
2015-08-26 16:44:50 +10:00
}
2015-09-13 14:04:31 +10:00
switch (cmd.toLowerCase()) {
2015-08-26 16:44:50 +10:00
case "auto":
2015-09-13 14:04:31 +10:00
case "claim": {
2015-09-29 16:29:28 +10:00
perms.add("plots.plot.<#>");
2015-08-26 16:44:50 +10:00
break;
}
}
2015-09-29 16:29:28 +10:00
return new ArrayList<>(perms);
2015-08-26 16:44:50 +10:00
}
2015-09-13 14:04:31 +10:00
public static String getComments(final List<String> lines) {
2015-09-11 20:09:22 +10:00
final StringBuilder result = new StringBuilder();
2015-09-13 14:04:31 +10:00
for (String line : lines) {
2015-08-26 16:44:50 +10:00
line = line.trim();
2015-09-13 14:04:31 +10:00
if (line.startsWith("/** ") || line.startsWith("*/ ") || line.startsWith("* ")) {
2015-08-26 16:44:50 +10:00
line = (line.replaceAll("/[*][*] ", "").replaceAll("[*]/ ", "").replaceAll("[*] ", "")).trim();
result.append(line + "\n");
}
}
return result.toString().trim();
}
2015-09-13 14:04:31 +10:00
public static void log(final String s) {
2015-08-26 16:44:50 +10:00
System.out.println(s);
}
}