feat: 标准化占位符, 增加模板

This commit is contained in:
2025-11-29 23:38:03 +08:00
parent d647e84db0
commit 8da042ccd3
13 changed files with 402 additions and 103 deletions

View File

@@ -1,6 +1,7 @@
use crate::{
config::Config,
entity::project::ProjectInfo,
template::TemplateEngine,
utils::{file, http::HttpClient},
};
use std::{fs, path::PathBuf};
@@ -26,9 +27,9 @@ fn create_project(name: &str, target: Option<&str>, temp_dir: &PathBuf) -> Resul
let local_dir = PathBuf::from(format!("./{}", name));
fs::create_dir(&local_dir)?;
clone_and_copy_template(target, temp_dir, &local_dir)?;
let template_dir = clone_and_copy_template(target, temp_dir, &local_dir)?;
initialize_project(&local_dir, name)?;
initialize_project_with_template(&template_dir, &local_dir, name)?;
Ok(())
}
@@ -57,7 +58,11 @@ fn check_example_exists(target: &str) -> Result<()> {
Ok(())
}
fn clone_and_copy_template(target: &str, temp_dir: &PathBuf, local_dir: &PathBuf) -> Result<()> {
fn clone_and_copy_template(
target: &str,
temp_dir: &PathBuf,
local_dir: &PathBuf,
) -> Result<PathBuf> {
let _ = fs::remove_dir_all(format!("{}/tmp", temp_dir.display()));
let config = Config::load();
@@ -67,10 +72,14 @@ fn clone_and_copy_template(target: &str, temp_dir: &PathBuf, local_dir: &PathBuf
let target_dir = PathBuf::from(format!("{}/tmp/examples/{}", temp_dir.display(), target));
file::copy_folder(&target_dir, local_dir)?;
Ok(())
Ok(target_dir)
}
fn initialize_project(local_dir: &PathBuf, name: &str) -> Result<()> {
fn initialize_project_with_template(
template_dir: &PathBuf,
local_dir: &PathBuf,
name: &str,
) -> Result<()> {
let lower_name = format!(
"{}{}",
name.chars().next().unwrap().to_lowercase(),
@@ -80,14 +89,41 @@ fn initialize_project(local_dir: &PathBuf, name: &str) -> Result<()> {
println!("项目名称: {}", name);
println!("标识名称: {}", lower_name);
let scripts_dir = local_dir.join(format!("behavior_pack/{}Scripts", lower_name));
fs::rename(local_dir.join("behavior_pack/exampleScripts"), &scripts_dir)?;
let project_info = generate_project_info(name, &lower_name);
apply_project_info(local_dir, &scripts_dir, &project_info)?;
let mut engine = TemplateEngine::load(template_dir)?;
rename_pack_folders(local_dir, &project_info)?;
engine.set_variable("mod_name".to_string(), project_info.name.clone());
engine.set_variable(
"mod_name_lower".to_string(),
project_info.lower_name.clone(),
);
engine.set_variable(
"behavior_pack_uuid".to_string(),
project_info.behavior_pack_uuid.clone(),
);
engine.set_variable(
"resource_pack_uuid".to_string(),
project_info.resource_pack_uuid.clone(),
);
engine.set_variable(
"behavior_module_uuid".to_string(),
project_info.behavior_module_uuid.clone(),
);
engine.set_variable(
"resource_module_uuid".to_string(),
project_info.resource_module_uuid.clone(),
);
engine.set_variable(
"behavior_pack_uuid_short".to_string(),
project_info.behavior_pack_uuid.chars().take(8).collect(),
);
engine.set_variable(
"resource_pack_uuid_short".to_string(),
project_info.resource_pack_uuid.chars().take(8).collect(),
);
engine.process_directory(local_dir)?;
Ok(())
}
@@ -102,82 +138,3 @@ fn generate_project_info(name: &str, lower_name: &str) -> ProjectInfo {
resource_module_uuid: Uuid::new_v4().to_string(),
}
}
fn apply_project_info(
local_dir: &PathBuf,
scripts_dir: &PathBuf,
info: &ProjectInfo,
) -> Result<()> {
let manifest_files = vec![
local_dir.join("world_behavior_packs.json"),
local_dir.join("world_resource_packs.json"),
local_dir.join("behavior_pack/pack_manifest.json"),
local_dir.join("resource_pack/pack_manifest.json"),
];
for path in manifest_files {
apply_info_to_json(&path, info)?;
}
process_python_files(scripts_dir, info)?;
Ok(())
}
fn apply_info_to_json(path: &PathBuf, info: &ProjectInfo) -> Result<()> {
println!(" - 修改文件: {}", path.display());
file::update_json_file(path, |json| {
let content = serde_json::to_string(json)?;
let updated = content
.replace("{behavior_pack_uuid}", &info.behavior_pack_uuid)
.replace("{resource_pack_uuid}", &info.resource_pack_uuid)
.replace("{behavior_module_uuid}", &info.behavior_module_uuid)
.replace("{resource_module_uuid}", &info.resource_module_uuid)
.replace("__mod_name__", &info.name)
.replace("__mod_name_lower__", &info.lower_name);
*json = serde_json::from_str(&updated)?;
Ok(())
})
}
fn process_python_files(dir: &PathBuf, info: &ProjectInfo) -> Result<()> {
if !dir.exists() {
return Ok(());
}
if dir.is_dir() {
for entry in fs::read_dir(dir)? {
let entry = entry?;
process_python_files(&entry.path(), info)?;
}
} else if dir.extension().and_then(|s| s.to_str()) == Some("py") {
apply_info_to_python(dir, info)?;
}
Ok(())
}
fn apply_info_to_python(path: &PathBuf, info: &ProjectInfo) -> Result<()> {
let content = fs::read_to_string(path)?;
let updated = content
.replace("__mod_name__", &info.name)
.replace("__mod_name_lower__", &info.lower_name);
fs::write(path, updated)?;
Ok(())
}
fn rename_pack_folders(local_dir: &PathBuf, info: &ProjectInfo) -> Result<()> {
let behavior_suffix: String = info.behavior_pack_uuid.chars().take(8).collect();
let resource_suffix: String = info.resource_pack_uuid.chars().take(8).collect();
fs::rename(
local_dir.join("behavior_pack"),
local_dir.join(format!("behavior_pack_{}", behavior_suffix)),
)?;
fs::rename(
local_dir.join("resource_pack"),
local_dir.join(format!("resource_pack_{}", resource_suffix)),
)?;
Ok(())
}