From a0b9810c446018552ad1405e063a0f82956afeb5 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Tue, 13 Aug 2019 20:06:15 -0700 Subject: [PATCH] [Doctools] Read source files for command ordering --- worldedit-core/doctools/build.gradle.kts | 1 + .../internal/util/DocumentationPrinter.kt | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/worldedit-core/doctools/build.gradle.kts b/worldedit-core/doctools/build.gradle.kts index 803e1078e..66aa2285f 100644 --- a/worldedit-core/doctools/build.gradle.kts +++ b/worldedit-core/doctools/build.gradle.kts @@ -14,4 +14,5 @@ dependencies { "implementation"(project(":worldedit-libs:core:ap")) "implementation"(project(":worldedit-core")) "implementation"(kotlin("stdlib-jdk8")) + "implementation"(kotlin("reflect")) } diff --git a/worldedit-core/doctools/src/main/kotlin/com/sk89q/worldedit/internal/util/DocumentationPrinter.kt b/worldedit-core/doctools/src/main/kotlin/com/sk89q/worldedit/internal/util/DocumentationPrinter.kt index 768830857..dd06e46a3 100644 --- a/worldedit-core/doctools/src/main/kotlin/com/sk89q/worldedit/internal/util/DocumentationPrinter.kt +++ b/worldedit-core/doctools/src/main/kotlin/com/sk89q/worldedit/internal/util/DocumentationPrinter.kt @@ -48,10 +48,10 @@ import java.nio.file.Files import java.nio.file.Paths import java.util.stream.Stream import kotlin.streams.toList -import org.enginehub.piston.annotation.Command as CommandAnnotation class DocumentationPrinter private constructor() { + private val nameRegex = Regex("name = \"(.+?)\"") private val serializer = PlainComponentSerializer({ "" }, TranslatableComponent::key) private val commands = WorldEdit.getInstance().platformManager.platformCommandManager.commandManager.allCommands .map { it.name to it }.toList().toMap() @@ -59,10 +59,19 @@ class DocumentationPrinter private constructor() { private val permsOutput = StringBuilder() private suspend inline fun SequenceScope.yieldAllCommandsIn() { - for (method in T::class.java.methods) { - val cmdAnno = method.getDeclaredAnnotation(CommandAnnotation::class.java) - if (cmdAnno != null) { - yield(cmdAnno.name) + val sourceFile = Paths.get("worldedit-core/src/main/java/" + T::class.qualifiedName!!.replace('.', '/') + ".java") + require(Files.exists(sourceFile)) { "Source not found for ${T::class.qualifiedName}"} + Files.newBufferedReader(sourceFile).useLines { lines -> + var inCommand = false + for (line in lines) { + if (inCommand) { + when (val match = nameRegex.find(line)) { + null -> if (line.trim() == ")") inCommand = false + else -> yield(match.groupValues[1]) + } + } else if (line.contains("@Command(")) { + inCommand = true + } } } } @@ -219,8 +228,7 @@ Other Permissions private fun cmdsToPerms(cmds: List, prefix: String) { cmds.forEach { c -> permsOutput.append(" ").append(cmdToPerm(prefix, c)).append("\n") - c.parts.filter { p -> p is SubCommandPart } - .map { p -> p as SubCommandPart } + c.parts.filterIsInstance() .forEach { scp -> cmdsToPerms(scp.commands.sortedBy { it.name }, prefix + c.name + " ") } @@ -260,6 +268,7 @@ Other Permissions cmdOutput.appendln() for ((k, v) in entries) { val rstSafe = v.replace("\"", "\\\"").replace("\n", "\n" + " ".repeat(2)) + .lineSequence().map { line -> line.ifBlank { "" } }.joinToString(separator = "\n") cmdOutput.append(" ".repeat(2)) .append(k) .append(",")