From 3dd3e5d5ea842399a6075da148a799510da4c3b7 Mon Sep 17 00:00:00 2001
From: Ben Wallis <atomyc@gmail.com>
Date: Fri, 20 Aug 2021 20:07:22 +0100
Subject: [PATCH] Added cargo cmd-doc-gen to auto-generate the table of
 commands in the new players/commands.md page in the book

---
 .cargo/config                   |  1 +
 .gitlab/scripts/code-quality.sh |  2 +-
 common/Cargo.toml               |  5 +++++
 common/src/bin/cmd_doc_gen.rs   | 31 +++++++++++++++++++++++++++++++
 4 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 common/src/bin/cmd_doc_gen.rs

diff --git a/.cargo/config b/.cargo/config
index ba8c095c46..686f74a4b0 100644
--- a/.cargo/config
+++ b/.cargo/config
@@ -4,6 +4,7 @@ rustflags = [
 ]
 
 [alias]
+cmd-doc-gen = "run --features=bin_cmd_doc_gen --bin cmd_doc_gen"
 csv-export = "run --manifest-path common/Cargo.toml --features=bin_csv --bin csv_export"
 csv-import = "run --manifest-path common/Cargo.toml --features=bin_csv --bin csv_import"
 test-server = "run --bin veloren-server-cli --no-default-features"
diff --git a/.gitlab/scripts/code-quality.sh b/.gitlab/scripts/code-quality.sh
index 3a07d71057..3d90661c9f 100755
--- a/.gitlab/scripts/code-quality.sh
+++ b/.gitlab/scripts/code-quality.sh
@@ -1,6 +1,6 @@
 #!/bin/bash
 rm -r target/debug/incremental/* || echo "all good" # TMP FIX FOR 2021-03-22-nightly
-time cargo clippy --all-targets --locked --features="bin_compression,bin_csv,bin_graphviz,bin_bot,asset_tweak" -- -D warnings &&
+time cargo clippy --all-targets --locked --features="bin_cmd_doc_gen,bin_compression,bin_csv,bin_graphviz,bin_bot,asset_tweak" -- -D warnings &&
 # Ensure that the veloren-voxygen default-publish feature builds as it excludes some default features
 time cargo clippy -p veloren-voxygen --locked --no-default-features --features="default-publish" -- -D warnings &&
 time cargo fmt --all -- --check
\ No newline at end of file
diff --git a/common/Cargo.toml b/common/Cargo.toml
index c332b5f9eb..9384818e12 100644
--- a/common/Cargo.toml
+++ b/common/Cargo.toml
@@ -10,6 +10,7 @@ hot-reloading = ["common-assets/hot-reloading"]
 simd = ["vek/platform_intrinsics"]
 bin_csv = ["ron", "csv", "structopt"]
 bin_graphviz = ["petgraph"]
+bin_cmd_doc_gen = []
 
 default = ["simd"]
 
@@ -98,3 +99,7 @@ required-features = ["bin_csv"]
 [[bin]]
 name = "recipe_graphviz"
 required-features = ["bin_graphviz"]
+
+[[bin]]
+name = "cmd_doc_gen"
+required-features = ["bin_cmd_doc_gen"]
\ No newline at end of file
diff --git a/common/src/bin/cmd_doc_gen.rs b/common/src/bin/cmd_doc_gen.rs
new file mode 100644
index 0000000000..24ecb39fc5
--- /dev/null
+++ b/common/src/bin/cmd_doc_gen.rs
@@ -0,0 +1,31 @@
+use veloren_common::cmd::ChatCommand;
+
+/// This binary generates the markdown table used for the `players/commands.md`
+/// page in the Veloren Book. It can be run with `cargo cmd-doc-gen`.
+fn main() {
+    println!("|Command|Description|Requires|Arguments|");
+    println!("|-|-|-|-|");
+    for cmd in ChatCommand::iter() {
+        let args = cmd
+            .data()
+            .args
+            .iter()
+            .map(|arg| arg.usage_string())
+            .collect::<Vec<String>>()
+            .join(" ");
+
+        println!(
+            "|/{}|{}|{}|{}|",
+            cmd.keyword(),
+            cmd.data().description,
+            cmd.data()
+                .needs_role
+                .map_or("".to_string(), |role| format!("{:?}", role)),
+            if !args.is_empty() {
+                format!("`{}`", args)
+            } else {
+                "".to_owned()
+            }
+        );
+    }
+}