forge_codegen/
compiler.rs1use std::fs;
5use std::path::{Path, PathBuf};
6
7use walkdir::WalkDir;
8
9use crate::lower::{lower, lower_with_target, LowerTarget};
10use crate::parser::tokenize;
11
12pub fn compile_source(source: &str) -> String {
13 let tokens = tokenize(source);
14 lower(&tokens)
15}
16
17pub fn compile_source_runtime(source: &str) -> String {
22 let tokens = tokenize(source);
23 lower_with_target(&tokens, LowerTarget::MiniJinja)
24}
25
26pub fn compile_file(input: &Path, output: &Path) -> std::io::Result<()> {
27 let raw = fs::read_to_string(input)?;
28 let lowered = compile_source(&raw);
29 if let Some(parent) = output.parent() {
30 fs::create_dir_all(parent)?;
31 }
32 fs::write(output, lowered)?;
33 Ok(())
34}
35
36pub fn emit_embedded_registry(input_dir: &Path, output_rs: &Path) -> std::io::Result<()> {
52 if let Some(parent) = output_rs.parent() {
53 fs::create_dir_all(parent)?;
54 }
55
56 if !input_dir.exists() {
57 fs::write(
58 output_rs,
59 "// forge-codegen: input dir not found at build time, no templates embedded.\n",
60 )?;
61 return Ok(());
62 }
63
64 let abs_input = fs::canonicalize(input_dir)?;
65 let mut out =
66 String::from("// Generated by forge_codegen::emit_embedded_registry — do not edit.\n");
67
68 for entry in WalkDir::new(&abs_input).into_iter().filter_map(|e| e.ok()) {
69 if !entry.file_type().is_file() {
70 continue;
71 }
72 let path = entry.path();
73 let Some(file_name) = path.file_name().and_then(|n| n.to_str()) else {
74 continue;
75 };
76 if !file_name.ends_with(".forge.html") && !file_name.ends_with(".forge") {
77 continue;
78 }
79 let rel = path.strip_prefix(&abs_input).unwrap_or(path);
80 let rel_str = rel.to_string_lossy().replace('\\', "/");
81 let view_path = rel_str
82 .trim_end_matches(".forge.html")
83 .trim_end_matches(".forge")
84 .to_string();
85 let abs_str = path
86 .canonicalize()
87 .unwrap_or_else(|_| path.to_path_buf())
88 .to_string_lossy()
89 .to_string();
90 out.push_str(&format!(
91 "::anvilforge::inventory::submit! {{ ::anvilforge::spark::template::EmbeddedTemplate {{ view_path: {view:?}, source: include_str!({src:?}) }} }}\n",
92 view = view_path,
93 src = abs_str,
94 ));
95 }
96
97 fs::write(output_rs, out)?;
98 Ok(())
99}
100
101pub fn compile_dir(input_dir: &Path, output_dir: &Path) -> std::io::Result<Vec<PathBuf>> {
104 let mut written = Vec::new();
105 if !input_dir.exists() {
106 return Ok(written);
107 }
108 for entry in WalkDir::new(input_dir).into_iter().filter_map(|e| e.ok()) {
109 if !entry.file_type().is_file() {
110 continue;
111 }
112 let path = entry.path();
113 let Some(file_name) = path.file_name().and_then(|n| n.to_str()) else {
114 continue;
115 };
116 if !file_name.ends_with(".forge.html") && !file_name.ends_with(".forge") {
117 continue;
118 }
119 let rel = path.strip_prefix(input_dir).unwrap_or(path);
120 let out_name = file_name
121 .replace(".forge.html", ".html")
122 .replace(".forge", ".html");
123 let mut out_path = output_dir.to_path_buf();
124 if let Some(parent) = rel.parent() {
125 out_path.push(parent);
126 }
127 out_path.push(out_name);
128 compile_file(path, &out_path)?;
129 written.push(out_path);
130 }
131 Ok(written)
132}