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

138 lines
5.3 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;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.intellectualcrafters.plot.object.PlotPlayer;
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-11 20:09:22 +10:00
final ArrayList<String> perms = new ArrayList<String>();
final Pattern p = Pattern.compile("\"([^\"]*)\"");
2015-09-13 14:04:31 +10:00
for (final String line : lines) {
if (line.contains("Permissions.hasPermission(")) {
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-08-26 16:44:50 +10:00
perms.set(perms.size() - 1, perms.get(perms.size() - 1) + perm);
2015-09-13 14:04:31 +10:00
} else if (perm.contains(".")) {
2015-08-26 16:44:50 +10:00
perms.add(perm);
}
}
}
}
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-08-26 16:44:50 +10:00
perms.add("plots.plot.#");
break;
}
}
return perms;
}
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);
}
}