use std::fs;
use std::path::Path;
use tempfile::TempDir;
fn main() {
println!("🧪 测试 Schema 从 target 目录加载\n");
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let workspace_path = temp_dir.path();
println!("📁 创建临时工作空间: {:?}", workspace_path);
let schema_dir = workspace_path.join("target/debug/build/my-app/out");
fs::create_dir_all(&schema_dir).expect("Failed to create schema dir");
let schema_path = schema_dir.join("summer-lsp.schema.json");
let schema_content = serde_json::json!({
"properties": {
"my-service": {
"type": "object",
"properties": {
"endpoint": {
"type": "string",
"description": "Service endpoint URL"
},
"timeout": {
"type": "integer",
"default": 30,
"description": "Connection timeout in seconds"
},
"enable-retry": {
"type": "boolean",
"default": false,
"description": "Enable retry on failure"
}
},
"required": ["endpoint"]
}
}
});
fs::write(
&schema_path,
serde_json::to_string_pretty(&schema_content).unwrap(),
)
.expect("Failed to write schema file");
println!("✅ 创建 Schema 文件: {:?}", schema_path);
println!();
println!("🔍 测试查找 Schema 文件...");
let found = find_schema_in_target(workspace_path);
match found {
Some(path) => {
println!("✅ 找到 Schema 文件: {:?}", path);
println!();
println!("📖 读取 Schema 内容...");
let content = fs::read_to_string(&path).expect("Failed to read schema");
let schema: serde_json::Value =
serde_json::from_str(&content).expect("Failed to parse schema");
if let Some(properties) = schema.get("properties").and_then(|p| p.as_object()) {
println!("✅ Schema 包含 {} 个配置节:", properties.len());
for (key, _) in properties {
println!(" - {}", key);
}
}
println!();
println!("🎉 测试成功!Schema 可以正确从 target 目录加载");
}
None => {
println!("❌ 未找到 Schema 文件");
std::process::exit(1);
}
}
}
fn find_schema_in_target(workspace_path: &Path) -> Option<std::path::PathBuf> {
let target_dir = workspace_path.join("target");
if !target_dir.exists() {
return None;
}
let mut schema_files = Vec::new();
if let Ok(entries) = fs::read_dir(&target_dir) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
let build_dir = path.join("build");
if build_dir.exists() {
if let Ok(build_entries) = fs::read_dir(&build_dir) {
for build_entry in build_entries.flatten() {
let out_dir = build_entry.path().join("out");
let schema_path = out_dir.join("summer-lsp.schema.json");
if schema_path.exists() {
if let Ok(metadata) = fs::metadata(&schema_path) {
if let Ok(modified) = metadata.modified() {
schema_files.push((schema_path, modified));
}
}
}
}
}
}
}
}
}
schema_files.sort_by(|a, b| b.1.cmp(&a.1));
schema_files.first().map(|(path, _)| path.clone())
}