diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-02-28 12:35:07 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-02-28 12:35:07 +0800 |
| commit | afddded8fdab6925a83f1240999ebac8f76f239d (patch) | |
| tree | 16d692f32e15824863907156ad1d4ed8c7b29576 /gen | |
| parent | a469c6684619c9677dfc1125886acc37f24c81d4 (diff) | |
Replace manual template processing with just_template library
Diffstat (limited to 'gen')
| -rw-r--r-- | gen/gen_commands_file.rs | 99 | ||||
| -rw-r--r-- | gen/gen_compile_info.rs | 32 | ||||
| -rw-r--r-- | gen/gen_iscc_script.rs | 23 | ||||
| -rw-r--r-- | gen/gen_override_renderer.rs | 71 | ||||
| -rw-r--r-- | gen/gen_renderers_file.rs | 58 | ||||
| -rw-r--r-- | gen/gen_specific_renderer.rs | 74 |
6 files changed, 137 insertions, 220 deletions
diff --git a/gen/gen_commands_file.rs b/gen/gen_commands_file.rs index 4131154..b68ac0a 100644 --- a/gen/gen_commands_file.rs +++ b/gen/gen_commands_file.rs @@ -1,33 +1,32 @@ use std::path::PathBuf; use just_fmt::pascal_case; +use just_template::{Template, tmpl, tmpl_param}; -use crate::r#gen::constants::{ - COMMAND_LIST, COMMAND_LIST_TEMPLATE, COMMANDS_PATH, REGISTRY_TOML, TEMPLATE_END, TEMPLATE_START, -}; +use crate::r#gen::constants::{COMMAND_LIST, COMMAND_LIST_TEMPLATE, COMMANDS_PATH, REGISTRY_TOML}; -/// Generate registry file from Registry.toml configuration +/// Generate registry file from Registry.toml configuration using just_template pub async fn generate_commands_file(repo_root: &PathBuf) { let template_path = repo_root.join(COMMAND_LIST_TEMPLATE); let output_path = repo_root.join(COMMAND_LIST); let config_path = repo_root.join(REGISTRY_TOML); // Read the template - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); // Read and parse the TOML configuration let config_content = tokio::fs::read_to_string(&config_path).await.unwrap(); let config: toml::Value = toml::from_str(&config_content).unwrap(); // Collect all command configurations - let mut commands = Vec::new(); - let mut nodes = Vec::new(); + let mut all_commands: Vec<(String, String, String)> = Vec::new(); + let mut all_nodes: Vec<String> = Vec::new(); // Collect commands from registry.toml and COMMANDS_PATH in parallel let (registry_collected, auto_collected) = tokio::join!( async { - let mut commands = Vec::new(); - let mut nodes = Vec::new(); + let mut commands: Vec<(String, String, String)> = Vec::new(); + let mut nodes: Vec<String> = Vec::new(); let Some(table) = config.as_table() else { return (commands, nodes); @@ -70,8 +69,8 @@ pub async fn generate_commands_file(repo_root: &PathBuf) { (commands, nodes) }, async { - let mut commands = Vec::new(); - let mut nodes = Vec::new(); + let mut commands: Vec<(String, String, String)> = Vec::new(); + let mut nodes: Vec<String> = Vec::new(); let commands_dir = repo_root.join(COMMANDS_PATH); if commands_dir.exists() && commands_dir.is_dir() { let mut entries = tokio::fs::read_dir(&commands_dir).await.unwrap(); @@ -120,69 +119,43 @@ pub async fn generate_commands_file(repo_root: &PathBuf) { let (mut registry_commands, mut registry_nodes) = registry_collected; let (mut auto_commands, mut auto_nodes) = auto_collected; - commands.append(&mut registry_commands); - commands.append(&mut auto_commands); - nodes.append(&mut registry_nodes); - nodes.append(&mut auto_nodes); - - // Extract the node_if template from the template content - const PROCESS_MARKER: &str = "// PROCESS"; - const LINE: &str = "<<LINE>>"; - const NODES: &str = "<<NODES>>"; - - let template_start_index = template - .find(TEMPLATE_START) - .ok_or("Template start marker not found") - .unwrap(); - let template_end_index = template - .find(TEMPLATE_END) - .ok_or("Template end marker not found") - .unwrap(); - - let template_slice = &template[template_start_index..template_end_index + TEMPLATE_END.len()]; - let node_if_template = template_slice - .trim_start_matches(TEMPLATE_START) - .trim_end_matches(TEMPLATE_END) - .trim_matches('\n'); - - // Generate the match arms for each command - let match_arms: String = commands - .iter() - .map(|(key, node, cmd_type)| { - node_if_template - .replace("<<KEY>>", key) - .replace("<<NODE_NAME>>", node) - .replace("<<COMMAND_TYPE>>", cmd_type) - .trim_matches('\n') - .to_string() - }) - .collect::<Vec<_>>() - .join("\n"); + all_commands.append(&mut registry_commands); + all_commands.append(&mut auto_commands); + + all_nodes.append(&mut registry_nodes); + all_nodes.append(&mut auto_nodes); + + // Create template + let mut template = Template::from(template_content); + + for (key, node, cmd_type) in &all_commands { + tmpl!(template += { + command_match_arms { + (key = key, node_name = node, cmd_type = cmd_type) + } + }); + } let nodes_str = format!( "[\n {}\n ]", - nodes + all_nodes .iter() .map(|node| format!("\"{}\".to_string()", node)) .collect::<Vec<_>>() .join(", ") ); - // Replace the template section with the generated match arms - let final_content = template - .replace(node_if_template, "") - .replace(TEMPLATE_START, "") - .replace(TEMPLATE_END, "") - .replace(PROCESS_MARKER, &match_arms) - .lines() - .filter(|line| !line.trim().is_empty()) - .collect::<Vec<_>>() - .join("\n") - .replace(LINE, "") - .replace(NODES, nodes_str.as_str()); + // Use insert_param for the NODES parameter + tmpl_param!(template, nodes = nodes_str); + + // Expand the template + let final_content = template.expand().unwrap(); // Write the generated code tokio::fs::write(output_path, final_content).await.unwrap(); - println!("Generated registry file with {} commands", commands.len()); + println!( + "Generated registry file with {} commands using just_template", + all_commands.len() + ); } diff --git a/gen/gen_compile_info.rs b/gen/gen_compile_info.rs index 5af030c..8d68d89 100644 --- a/gen/gen_compile_info.rs +++ b/gen/gen_compile_info.rs @@ -1,17 +1,20 @@ use std::path::PathBuf; +use just_template::{Template, tmpl_param}; + use crate::r#gen::{ constants::{COMPILE_INFO_RS, COMPILE_INFO_RS_TEMPLATE}, env::{get_git_branch, get_git_commit, get_platform, get_toolchain, get_version}, }; -/// Generate compile info +/// Generate compile info using just_template pub async fn generate_compile_info(repo_root: &PathBuf) { // Read the template code let template_code = tokio::fs::read_to_string(repo_root.join(COMPILE_INFO_RS_TEMPLATE)) .await .unwrap(); + // Get all the values needed for the template let date = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(); let target = std::env::var("TARGET").unwrap_or_else(|_| "unknown".to_string()); let platform = get_platform(&target); @@ -20,18 +23,29 @@ pub async fn generate_compile_info(repo_root: &PathBuf) { let branch = get_git_branch().unwrap_or_else(|_| "unknown".to_string()); let commit = get_git_commit().unwrap_or_else(|_| "unknown".to_string()); - let generated_code = template_code - .replace("{date}", &date) - .replace("{target}", &target) - .replace("{platform}", &platform) - .replace("{toolchain}", &toolchain) - .replace("{version}", &version) - .replace("{branch}", &branch) - .replace("{commit}", &commit); + // Create a Template instance + let mut template = Template::from(template_code); + + // Set all parameters + tmpl_param!( + template, + date = date, + target = target, + platform = platform, + toolchain = toolchain, + version = version, + branch = branch, + commit = commit + ); + + // Expand the template + let generated_code = template.expand().unwrap(); // Write the generated code let compile_info_path = repo_root.join(COMPILE_INFO_RS); tokio::fs::write(compile_info_path, generated_code) .await .unwrap(); + + println!("Generated compile_info.rs using just_template"); } diff --git a/gen/gen_iscc_script.rs b/gen/gen_iscc_script.rs index 1eddcca..23328ab 100644 --- a/gen/gen_iscc_script.rs +++ b/gen/gen_iscc_script.rs @@ -1,25 +1,36 @@ use std::path::PathBuf; +use just_template::{Template, tmpl_param}; + use crate::r#gen::{ constants::{SETUP_JV_CLI_ISS, SETUP_JV_CLI_ISS_TEMPLATE}, env::{get_author, get_site, get_version}, }; -/// Generate Inno Setup installer script (Windows only) +/// Generate Inno Setup installer script (Windows only) using just_template pub async fn generate_installer_script(repo_root: &PathBuf) { let template_path = repo_root.join(SETUP_JV_CLI_ISS_TEMPLATE); let output_path = repo_root.join(SETUP_JV_CLI_ISS); - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); + // Read the template + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); + // Get values for the template let author = get_author().unwrap(); let version = get_version(); let site = get_site().unwrap(); - let generated = template - .replace("<<<AUTHOR>>>", &author) - .replace("<<<VERSION>>>", &version) - .replace("<<<SITE>>>", &site); + // Create template + let mut template = Template::from(template_content); + + // Set all parameters + tmpl_param!(template, version = version, author = author, site = site); + // Expand the template + let generated = template.expand().unwrap(); + + // Write the generated script tokio::fs::write(output_path, generated).await.unwrap(); + + println!("Generated Inno Setup script using just_template"); } diff --git a/gen/gen_override_renderer.rs b/gen/gen_override_renderer.rs index dafca91..1e67be4 100644 --- a/gen/gen_override_renderer.rs +++ b/gen/gen_override_renderer.rs @@ -1,13 +1,11 @@ use std::{collections::HashSet, path::PathBuf}; +use just_template::{Template, tmpl}; use regex::Regex; use tokio::fs; use crate::r#gen::{ - constants::{ - COMMANDS_PATH, OVERRIDE_RENDERER_ENTRY, OVERRIDE_RENDERER_ENTRY_TEMPLATE, TEMPLATE_END, - TEMPLATE_START, - }, + constants::{COMMANDS_PATH, OVERRIDE_RENDERER_ENTRY, OVERRIDE_RENDERER_ENTRY_TEMPLATE}, resolve_types::resolve_type_paths, }; @@ -17,53 +15,30 @@ pub async fn generate_override_renderer(repo_root: &PathBuf) { let all_possible_types = collect_all_possible_types(&PathBuf::from(COMMANDS_PATH)).await; // Read the template - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); - - // Extract the template section from the template content - const MATCH_MARKER: &str = "// MATCHING"; - - let template_start_index = template - .find(TEMPLATE_START) - .ok_or("Template start marker not found") - .unwrap(); - let template_end_index = template - .find(TEMPLATE_END) - .ok_or("Template end marker not found") - .unwrap(); - - let template_slice = &template[template_start_index..template_end_index + TEMPLATE_END.len()]; - let renderer_template = template_slice - .trim_start_matches(TEMPLATE_START) - .trim_end_matches(TEMPLATE_END) - .trim_matches('\n'); - - // Generate the match arms for each renderer - let match_arms: String = all_possible_types - .iter() - .map(|type_name| { - let name = type_name.split("::").last().unwrap_or(type_name); - renderer_template - .replace("JVOutputTypeName", name) - .replace("JVOutputType", type_name) - .trim_matches('\n') - .to_string() - }) - .collect::<Vec<String>>() - .join("\n"); - - // Replace the template section with the generated match arms - let final_content = template - .replace(renderer_template, "") - .replace(TEMPLATE_START, "") - .replace(TEMPLATE_END, "") - .replace(MATCH_MARKER, &match_arms) - .lines() - .filter(|line| !line.trim().is_empty()) - .collect::<Vec<_>>() - .join("\n"); + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); + + // Create template + let mut template = Template::from(template_content); + + for type_name in &all_possible_types { + let name = type_name.split("::").last().unwrap_or(type_name); + tmpl!(template += { + type_match_arms { + (jv_output_type_name = name, jv_output_type = type_name) + } + }); + } + + // Expand the template + let final_content = template.expand().unwrap(); // Write the generated code tokio::fs::write(output_path, final_content).await.unwrap(); + + println!( + "Generated override renderer entry with {} types using just_template", + all_possible_types.len() + ); } pub async fn collect_all_possible_types(dir: &PathBuf) -> HashSet<String> { diff --git a/gen/gen_renderers_file.rs b/gen/gen_renderers_file.rs index 497d258..f16c504 100644 --- a/gen/gen_renderers_file.rs +++ b/gen/gen_renderers_file.rs @@ -1,18 +1,19 @@ use std::path::PathBuf; +use just_template::{Template, tmpl}; + use crate::r#gen::constants::{ OVERRIDE_RENDERER_DISPATCHER, OVERRIDE_RENDERER_DISPATCHER_TEMPLATE, REGISTRY_TOML, - TEMPLATE_END, TEMPLATE_START, }; -/// Generate renderer list file from Registry.toml configuration +/// Generate renderer list file from Registry.toml configuration using just_template pub async fn generate_renderers_file(repo_root: &PathBuf) { let template_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER_TEMPLATE); let output_path = repo_root.join(OVERRIDE_RENDERER_DISPATCHER); let config_path = repo_root.join(REGISTRY_TOML); // Read the template - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); // Read and parse the TOML configuration let config_content = tokio::fs::read_to_string(&config_path).await.unwrap(); @@ -45,53 +46,24 @@ pub async fn generate_renderers_file(repo_root: &PathBuf) { renderers.push((name.to_string(), renderer_type.to_string())); } - // Extract the template section from the template content - const MATCH_MARKER: &str = "// MATCH"; - - let template_start_index = template - .find(TEMPLATE_START) - .ok_or("Template start marker not found") - .unwrap(); - let template_end_index = template - .find(TEMPLATE_END) - .ok_or("Template end marker not found") - .unwrap(); + // Create template + let mut template = Template::from(template_content); - let template_slice = &template[template_start_index..template_end_index + TEMPLATE_END.len()]; - let renderer_template = template_slice - .trim_start_matches(TEMPLATE_START) - .trim_end_matches(TEMPLATE_END) - .trim_matches('\n'); - - // Generate the match arms for each renderer - let match_arms: String = renderers - .iter() - .map(|(name, renderer_type)| { - renderer_template - .replace("<<NAME>>", name) - .replace("RendererType", renderer_type) - .trim_matches('\n') - .to_string() - }) - .collect::<Vec<String>>() - .join("\n"); + for (name, renderer_type) in &renderers { + tmpl!(template += { + renderer_match_arms { + (name = name, renderer_type = renderer_type) + } + }); + } - // Replace the template section with the generated match arms - let final_content = template - .replace(renderer_template, "") - .replace(TEMPLATE_START, "") - .replace(TEMPLATE_END, "") - .replace(MATCH_MARKER, &match_arms) - .lines() - .filter(|line| !line.trim().is_empty()) - .collect::<Vec<_>>() - .join("\n"); + let final_content = template.expand().unwrap(); // Write the generated code tokio::fs::write(output_path, final_content).await.unwrap(); println!( - "Generated renderer list file with {} renderers", + "Generated renderer list file with {} renderers using just_template", renderers.len() ); } diff --git a/gen/gen_specific_renderer.rs b/gen/gen_specific_renderer.rs index 0c66631..95e6900 100644 --- a/gen/gen_specific_renderer.rs +++ b/gen/gen_specific_renderer.rs @@ -1,78 +1,50 @@ use std::{collections::HashMap, path::PathBuf}; +use just_template::{Template, tmpl}; use regex::Regex; use crate::r#gen::{ - constants::{ - RENDERERS_PATH, SPECIFIC_RENDERER_MATCHING, SPECIFIC_RENDERER_MATCHING_TEMPLATE, - TEMPLATE_END, TEMPLATE_START, - }, + constants::{RENDERERS_PATH, SPECIFIC_RENDERER_MATCHING, SPECIFIC_RENDERER_MATCHING_TEMPLATE}, resolve_types::resolve_type_paths, }; const RENDERER_TYPE_PREFIX: &str = "crate::"; +/// Generate specific renderer matching file using just_template pub async fn generate_specific_renderer(repo_root: &PathBuf) { - // Matches - // HashMap<RendererTypeFullName, OutputTypeFullName> + // Matches: HashMap<RendererTypeFullName, OutputTypeFullName> let mut renderer_matches: HashMap<String, String> = HashMap::new(); let renderer_path = repo_root.join(RENDERERS_PATH); - collect_renderers(&renderer_path, &mut renderer_matches); let template_path = repo_root.join(SPECIFIC_RENDERER_MATCHING_TEMPLATE); let output_path = repo_root.join(SPECIFIC_RENDERER_MATCHING); // Read the template - let template = tokio::fs::read_to_string(&template_path).await.unwrap(); - - // Extract the template section from the template content - const MATCH_MARKER: &str = "// MATCHING"; - - let template_start_index = template - .find(TEMPLATE_START) - .ok_or("Template start marker not found") - .unwrap(); - let template_end_index = template - .find(TEMPLATE_END) - .ok_or("Template end marker not found") - .unwrap(); - - let template_slice = &template[template_start_index..template_end_index + TEMPLATE_END.len()]; - let renderer_template = template_slice - .trim_start_matches(TEMPLATE_START) - .trim_end_matches(TEMPLATE_END) - .trim_matches('\n'); - - // Generate the match arms for each renderer - let match_arms: String = renderer_matches - .iter() - .map(|(renderer, output)| { - let output_name = output.split("::").last().unwrap_or(output); - renderer_template - .replace("OutputTypeName", output_name) - .replace("OutputType", output) - .replace("RendererType", renderer) - .trim_matches('\n') - .to_string() - }) - .collect::<Vec<String>>() - .join("\n"); + let template_content = tokio::fs::read_to_string(&template_path).await.unwrap(); - // Replace the template section with the generated match arms - let final_content = template - .replace(renderer_template, "") - .replace(TEMPLATE_START, "") - .replace(TEMPLATE_END, "") - .replace(MATCH_MARKER, &match_arms) - .lines() - .filter(|line| !line.trim().is_empty()) - .collect::<Vec<_>>() - .join("\n"); + // Create template + let mut template = Template::from(template_content); + + for (renderer, output) in &renderer_matches { + let output_name = output.split("::").last().unwrap_or(output); + tmpl!(template += { + renderer_match_arms { + (output_type_name = output_name, output_type = output, renderer_type = renderer) + } + }); + } + + let final_content = template.expand().unwrap(); // Write the generated code tokio::fs::write(output_path, final_content).await.unwrap(); + + println!( + "Generated specific renderer matching with {} renderers using just_template", + renderer_matches.len() + ); } fn collect_renderers(dir_path: &PathBuf, matches: &mut HashMap<String, String>) { |
