#336 Create command list generator in tools
- Add new conditional template tags [...] blabla [/...] - Create logic for generating a list of commands
This commit is contained in:
parent
0fdc8d2810
commit
282f777311
@ -203,7 +203,7 @@ public class DataManager {
|
|||||||
} catch (Exception ignored) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ConsoleLogger.info("AutoPurgeDatabase : Removed " + i + " permissions");
|
ConsoleLogger.info("AutoPurgeDatabase : Removed " + i + "permissions");
|
||||||
|
|
||||||
/*int i = 0;
|
/*int i = 0;
|
||||||
for (String name : cleared) {
|
for (String name : cleared) {
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
package fr.xephi.authme.command;
|
package fr.xephi.authme.command;
|
||||||
|
|
||||||
import java.util.List;
|
import com.google.common.collect.Lists;
|
||||||
|
|
||||||
import fr.xephi.authme.util.CollectionUtils;
|
import fr.xephi.authme.util.CollectionUtils;
|
||||||
import fr.xephi.authme.util.StringUtils;
|
import fr.xephi.authme.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public final class CommandUtils {
|
public final class CommandUtils {
|
||||||
|
|
||||||
public static int getMinNumberOfArguments(CommandDescription command) {
|
public static int getMinNumberOfArguments(CommandDescription command) {
|
||||||
@ -32,6 +34,16 @@ public final class CommandUtils {
|
|||||||
return StringUtils.join(" ", labels);
|
return StringUtils.join(" ", labels);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String constructCommandPath(CommandDescription command) {
|
||||||
|
List<String> labels = new ArrayList<>();
|
||||||
|
CommandDescription currentCommand = command;
|
||||||
|
while (currentCommand != null) {
|
||||||
|
labels.add(currentCommand.getLabels().get(0));
|
||||||
|
currentCommand = currentCommand.getParent();
|
||||||
|
}
|
||||||
|
return "/" + labelsToString(Lists.reverse(labels));
|
||||||
|
}
|
||||||
|
|
||||||
public static double getDifference(List<String> labels1, List<String> labels2, boolean fullCompare) {
|
public static double getDifference(List<String> labels1, List<String> labels2, boolean fullCompare) {
|
||||||
// Make sure the other reference is correct
|
// Make sure the other reference is correct
|
||||||
if (labels1 == null || labels2 == null) {
|
if (labels1 == null || labels2 == null) {
|
||||||
|
|||||||
52
src/test/java/fr/xephi/authme/command/CommandUtilsTest.java
Normal file
52
src/test/java/fr/xephi/authme/command/CommandUtilsTest.java
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
package fr.xephi.authme.command;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
|
import static org.junit.Assert.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link CommandUtils}.
|
||||||
|
*/
|
||||||
|
public class CommandUtilsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldPrintLabels() {
|
||||||
|
// given
|
||||||
|
List<String> labels = Arrays.asList("authme", "help", "reload");
|
||||||
|
|
||||||
|
// when
|
||||||
|
String result = CommandUtils.labelsToString(labels);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result, equalTo("authme help reload"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldReturnCommandPath() {
|
||||||
|
// given
|
||||||
|
CommandDescription base = CommandDescription.builder()
|
||||||
|
.labels("authme", "auth")
|
||||||
|
.description("Base")
|
||||||
|
.detailedDescription("Test base command.")
|
||||||
|
.executableCommand(mock(ExecutableCommand.class))
|
||||||
|
.build();
|
||||||
|
CommandDescription command = CommandDescription.builder()
|
||||||
|
.parent(base)
|
||||||
|
.labels("help", "h", "?")
|
||||||
|
.description("Child")
|
||||||
|
.detailedDescription("Test child command.")
|
||||||
|
.executableCommand(mock(ExecutableCommand.class))
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// when
|
||||||
|
String commandPath = CommandUtils.constructCommandPath(command);
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(commandPath, equalTo("/authme help"));
|
||||||
|
}
|
||||||
|
}
|
||||||
70
src/tools/commands/CommandPageCreater.java
Normal file
70
src/tools/commands/CommandPageCreater.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
package commands;
|
||||||
|
|
||||||
|
import fr.xephi.authme.command.CommandArgumentDescription;
|
||||||
|
import fr.xephi.authme.command.CommandDescription;
|
||||||
|
import fr.xephi.authme.command.CommandInitializer;
|
||||||
|
import fr.xephi.authme.command.CommandPermissions;
|
||||||
|
import fr.xephi.authme.command.CommandUtils;
|
||||||
|
import fr.xephi.authme.permission.PermissionNode;
|
||||||
|
import utils.ANewMap;
|
||||||
|
import utils.FileUtils;
|
||||||
|
import utils.TagReplacer;
|
||||||
|
import utils.ToolTask;
|
||||||
|
import utils.ToolsConstants;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Scanner;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class CommandPageCreater implements ToolTask {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTaskName() {
|
||||||
|
return "createCommandPage";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute(Scanner scanner) {
|
||||||
|
final Set<CommandDescription> baseCommands = CommandInitializer.getBaseCommands();
|
||||||
|
final String template = FileUtils.readFromFile(ToolsConstants.TOOLS_SOURCE_ROOT
|
||||||
|
+ "commands/command_entry.tpl.md");
|
||||||
|
|
||||||
|
StringBuilder commandsResult = new StringBuilder();
|
||||||
|
for (CommandDescription command : baseCommands) {
|
||||||
|
Map<String, String> tags = ANewMap
|
||||||
|
.with("command", CommandUtils.constructCommandPath(command))
|
||||||
|
.and("description", command.getDetailedDescription())
|
||||||
|
.and("arguments", formatArguments(command.getArguments()))
|
||||||
|
.and("permissions", formatPermissions(command.getCommandPermissions()))
|
||||||
|
.build();
|
||||||
|
commandsResult.append(TagReplacer.applyReplacements(template, tags));
|
||||||
|
}
|
||||||
|
|
||||||
|
FileUtils.generateFileFromTemplate(
|
||||||
|
ToolsConstants.TOOLS_SOURCE_ROOT + "commands/commands.tpl.md",
|
||||||
|
ToolsConstants.DOCS_FOLDER + "commands.md",
|
||||||
|
ANewMap.with("commands", commandsResult.toString()).build());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatPermissions(CommandPermissions permissions) {
|
||||||
|
if (permissions == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
String result = "";
|
||||||
|
for (PermissionNode node : permissions.getPermissionNodes()) {
|
||||||
|
result += node.getNode() + " ";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String formatArguments(Iterable<CommandArgumentDescription> arguments) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
for (CommandArgumentDescription argument : arguments) {
|
||||||
|
String argumentName = argument.isOptional()
|
||||||
|
? "[" + argument.getDescription() + "]"
|
||||||
|
: "<" + argument.getDescription() + ">";
|
||||||
|
result.append(argumentName).append(" ");
|
||||||
|
}
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
2
src/tools/commands/command_entry.tpl.md
Normal file
2
src/tools/commands/command_entry.tpl.md
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{command}: {description} _{arguments}_
|
||||||
|
[permissions]Permission: {permissions}[/permissions]
|
||||||
4
src/tools/commands/commands.tpl.md
Normal file
4
src/tools/commands/commands.tpl.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
## AuthMe commands
|
||||||
|
You can use the following commands to use the functions of AuthMe:
|
||||||
|
|
||||||
|
{commands}
|
||||||
@ -47,7 +47,7 @@ public class PermissionsListWriter implements ToolTask {
|
|||||||
private static void generateAndWriteFile() {
|
private static void generateAndWriteFile() {
|
||||||
final String permissionsTagValue = generatePermissionsList();
|
final String permissionsTagValue = generatePermissionsList();
|
||||||
|
|
||||||
Map<String, Object> tags = ANewMap.<String, Object>with("permissions", permissionsTagValue).build();
|
Map<String, String> tags = ANewMap.with("permissions", permissionsTagValue).build();
|
||||||
FileUtils.generateFileFromTemplate(
|
FileUtils.generateFileFromTemplate(
|
||||||
ToolsConstants.TOOLS_SOURCE_ROOT + "permissions/permission_nodes.tpl.md", PERMISSIONS_OUTPUT_FILE, tags);
|
ToolsConstants.TOOLS_SOURCE_ROOT + "permissions/permission_nodes.tpl.md", PERMISSIONS_OUTPUT_FILE, tags);
|
||||||
System.out.println("Wrote to '" + PERMISSIONS_OUTPUT_FILE + "'");
|
System.out.println("Wrote to '" + PERMISSIONS_OUTPUT_FILE + "'");
|
||||||
@ -62,8 +62,8 @@ public class PermissionsListWriter implements ToolTask {
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
for (Map.Entry<String, String> entry : permissions.entrySet()) {
|
for (Map.Entry<String, String> entry : permissions.entrySet()) {
|
||||||
Map<String, Object> tags = ANewMap.<String, Object>
|
Map<String, String> tags = ANewMap
|
||||||
with("node", entry.getKey())
|
.with("node", entry.getKey())
|
||||||
.and("description", entry.getValue())
|
.and("description", entry.getValue())
|
||||||
.build();
|
.build();
|
||||||
sb.append(TagReplacer.applyReplacements(template, tags));
|
sb.append(TagReplacer.applyReplacements(template, tags));
|
||||||
|
|||||||
@ -18,10 +18,9 @@ public final class FileUtils {
|
|||||||
private FileUtils() {
|
private FileUtils() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void generateFileFromTemplate(String templateFile, String destinationFile, Map<String, Object> tags) {
|
public static void generateFileFromTemplate(String templateFile, String destinationFile, Map<String, String> tags) {
|
||||||
String template = readFromFile(templateFile);
|
String template = readFromFile(templateFile);
|
||||||
String result = TagReplacer.applyReplacements(template, tags);
|
String result = TagReplacer.applyReplacements(template, tags);
|
||||||
|
|
||||||
writeToFile(destinationFile, result);
|
writeToFile(destinationFile, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package utils;
|
package utils;
|
||||||
|
|
||||||
|
import fr.xephi.authme.util.StringUtils;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -21,10 +23,15 @@ public class TagReplacer {
|
|||||||
* any occurrences of "{foo}" to "bar".
|
* any occurrences of "{foo}" to "bar".
|
||||||
* @return The filled template
|
* @return The filled template
|
||||||
*/
|
*/
|
||||||
public static String applyReplacements(String template, Map<String, Object> tags) {
|
public static String applyReplacements(String template, Map<String, String> tags) {
|
||||||
String result = template;
|
String result = template;
|
||||||
for (Map.Entry<String, Object> tagRule : tags.entrySet()) {
|
for (Map.Entry<String, String> tagRule : tags.entrySet()) {
|
||||||
result = result.replace("{" + tagRule.getKey() + "}", tagRule.getValue().toString());
|
final String name = tagRule.getKey();
|
||||||
|
final String value = tagRule.getValue();
|
||||||
|
|
||||||
|
String replacement = StringUtils.isEmpty(value) ? "" : "\\1";
|
||||||
|
result = result.replaceAll("\\[" + name + "\\](.*?)\\[/" + name + "\\]", replacement);
|
||||||
|
result = result.replace("{" + tagRule.getKey() + "}", tagRule.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
return applyReplacements(result);
|
return applyReplacements(result);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user