summaryrefslogtreecommitdiff
path: root/build.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-01-23 05:29:28 +0800
committer魏曹先生 <1992414357@qq.com>2026-01-23 05:29:28 +0800
commitfa51df5d748d9d9e3927c56d33e1437a05a8abb1 (patch)
tree4bb8a4c3543ad65d5e261ca4283495cb6cedaece /build.rs
parentcffe4f130d7979df1ebbe84ec77f955b5430e8db (diff)
Update command registration to auto-discover commands
Commands in `./src/cmds/` are now automatically registered. The `[cmd]` section in `.cargo/registry.toml` is now only for overriding or registering commands outside the auto-discovery path.
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/build.rs b/build.rs
index 70e7e28..8ee2245 100644
--- a/build.rs
+++ b/build.rs
@@ -2,6 +2,10 @@ use std::env;
use std::path::PathBuf;
use std::process::Command;
+use string_proc::pascal_case;
+
+const COMMANDS_PATH: &str = "./src/cmds/";
+
const COMPILE_INFO_RS_TEMPLATE: &str = "./templates/compile_info.rs.template";
const COMPILE_INFO_RS: &str = "./src/data/compile_info.rs";
@@ -253,6 +257,7 @@ fn generate_cmd_registry_file(repo_root: &PathBuf) -> Result<(), Box<dyn std::er
let mut commands = Vec::new();
let mut nodes = Vec::new();
+ // First, collect commands from Registry.toml
if let Some(table) = config.as_table() {
if let Some(cmd_table) = table.get("cmd") {
if let Some(cmd_table) = cmd_table.as_table() {
@@ -272,6 +277,48 @@ fn generate_cmd_registry_file(repo_root: &PathBuf) -> Result<(), Box<dyn std::er
}
}
+ // Then, automatically register commands from COMMANDS_PATH
+ let commands_dir = repo_root.join(COMMANDS_PATH);
+ if commands_dir.exists() && commands_dir.is_dir() {
+ for entry in std::fs::read_dir(&commands_dir)? {
+ let entry = entry?;
+ let path = entry.path();
+
+ if !path.is_file() {
+ continue;
+ }
+
+ let extension = match path.extension() {
+ Some(ext) => ext,
+ None => continue,
+ };
+
+ if extension != "rs" {
+ continue;
+ }
+
+ let file_name = match path.file_stem().and_then(|s| s.to_str()) {
+ Some(name) => name,
+ None => continue,
+ };
+
+ // Skip files that start with underscore
+ if file_name.starts_with('_') {
+ continue;
+ }
+
+ // Convert filename to PascalCase
+ let pascal_name = pascal_case!(file_name);
+
+ let key = file_name.to_string();
+ let node = file_name.replace(".", " ");
+ let cmd_type = format!("cmds::{}::JV{}Command", file_name, pascal_name);
+
+ nodes.push(node.clone());
+ commands.push((key, node, cmd_type));
+ }
+ }
+
// Extract the node_if template from the template content
const PROCESS_MARKER: &str = "// PROCESS";
const TEMPLATE_START: &str = "// -- TEMPLATE START --";