use std::fs;
use std::io::Write;
use std::path::Path;
const SPECIAL_ROUTES: &[&str] = &[
"daily_signin",
"fm_trash",
"personal_fm",
"personal_fm_mode",
];
const EXCLUDED_MODULES: &[&str] = &[
"avatar_upload",
"voice_upload",
"eapi_decrypt",
"inner_version",
"cloud", "cloud_upload_token", "cloud_upload_complete", ];
fn method_to_route(method: &str) -> String {
if SPECIAL_ROUTES.contains(&method) {
format!("/{}", method)
} else {
format!("/{}", method.replace('_', "/"))
}
}
fn main() {
println!("cargo:rerun-if-changed=src/api/mod.rs");
let mod_rs = fs::read_to_string("src/api/mod.rs").expect("Failed to read src/api/mod.rs");
let mut methods: Vec<String> = Vec::new();
for line in mod_rs.lines() {
let trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix("mod ") {
if let Some(name) = rest.strip_suffix(';') {
let name = name.trim();
if !EXCLUDED_MODULES.contains(&name) {
methods.push(name.to_string());
}
}
}
}
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("api_routes_generated.rs");
let mut f = fs::File::create(&dest_path).expect("Failed to create generated file");
writeln!(f, "// 自动生成的路由注册代码 - 请勿手动修改").unwrap();
writeln!(f, "// 由 build.rs 从 src/api/mod.rs 扫描生成").unwrap();
writeln!(f, "//").unwrap();
writeln!(f, "// 共 {} 个标准路由", methods.len()).unwrap();
writeln!(f).unwrap();
writeln!(f, "api_routes!(router,").unwrap();
for method in methods.iter() {
let route = method_to_route(method);
writeln!(f, " {} => \"{}\",", method, route).unwrap();
}
writeln!(f, ")").unwrap();
}