diff options
author | Qrius <[email protected]> | 2024-09-26 00:11:05 +0200 |
---|---|---|
committer | Qrius <[email protected]> | 2024-09-26 00:11:05 +0200 |
commit | f26d572d52d822ca2d96d20124fe085c560cd604 (patch) | |
tree | 8b66c8c7d64f3ffff21c58ccd947f66a746379b3 /src | |
parent | ee7facd1db22adb32b5f9077591e4cf2468e32c2 (diff) | |
download | skaldpress-f26d572d52d822ca2d96d20124fe085c560cd604.tar.gz skaldpress-f26d572d52d822ca2d96d20124fe085c560cd604.zip |
Track macro invokations, only re-compile nesecarry files
Diffstat (limited to 'src')
-rw-r--r-- | src/macro_processor/macro_processor.rs | 11 | ||||
-rw-r--r-- | src/skaldpress/main.rs | 36 |
2 files changed, 39 insertions, 8 deletions
diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 93a86af..2e8675f 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -145,7 +145,7 @@ fn smp_builtin_include( } /// Include a new file verbatim, don't do ANY additional processing -fn smp_builtin_include_verbatim ( +fn smp_builtin_include_verbatim( smp: &mut MacroProcessor, macro_name: &str, args: &mut [String], @@ -264,12 +264,15 @@ enum ParserState { pub struct MacroProcessor { /// All currently defined macros in this MacroProcessor pub macros: HashMap<String, MacroType>, + /// All macro invocations that has happened + pub macro_invocations: Vec<(String, Vec<String>)>, } impl MacroProcessor { pub fn new() -> Self { let mut smp = Self { macros: HashMap::new(), + macro_invocations: Vec::new(), }; smp.define_builtins(); smp @@ -373,6 +376,12 @@ impl MacroProcessor { return Ok(out); }; + // Log macro invokation + // The fact that we are here, does not ensure that the macro is actually expanded into + // something useful, just that it exists, and was invoked + self.macro_invocations + .push((macro_name.to_string(), args.to_vec())); + match macro_body { MacroType::String(body) => { let mut expanded = body.clone(); diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs index 5e44b8d..a4d40ee 100644 --- a/src/skaldpress/main.rs +++ b/src/skaldpress/main.rs @@ -31,6 +31,7 @@ struct CompiledFile { /// or the file itself if it didn't have a extension extension: String, source_path: String, + needs_recompilation: bool, } /// SMP Macro for processing input with a template @@ -186,6 +187,15 @@ fn wrap_template( ) } +fn needs_recompilation(macro_processor: &MacroProcessor) -> bool { + for (macro_name, _) in ¯o_processor.macro_invocations { + if macro_name == "all_tagged_by" { + return true; + } + } + false +} + /// Will attempt to compile a specific file, potentially storing some state about the file fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, SkaldpressError> { let extension = file_path.extension().unwrap_or(std::ffi::OsStr::new("")); @@ -230,6 +240,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, Skaldpres metadata: map, extension: String::from(extension.to_str().unwrap_or("")), source_path: String::from(file_path.to_str().unwrap_or("")), + needs_recompilation: false, }); } } @@ -246,6 +257,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, Skaldpres metadata: map, extension: String::from(extension.to_str().unwrap_or("")), source_path: String::from(file_path.to_str().unwrap_or("")), + needs_recompilation: needs_recompilation(¯o_processor), }); }; @@ -263,6 +275,7 @@ fn compile_file(file_path: &Path, opts: &Opts) -> Result<CompiledFile, Skaldpres metadata: map, extension: String::from(template_extension), source_path: String::from(file_path.to_str().unwrap_or("")), + needs_recompilation: needs_recompilation(¯o_processor), }) } @@ -393,10 +406,20 @@ fn compile_files_in_directory(directory: &Path, opts: &Opts) -> Result<(), Skald } }; - let should_compile = (opts.filter.len() == 0) - || opts - .filter - .contains(&path.as_path().to_str().unwrap_or("").to_string()); + let mut needs_recompilation = false; + if let Some(cfile_i) = + cached_file_id_by_path(&path.as_path().to_str().unwrap_or("").to_string()) + { + unsafe { + needs_recompilation = COMPILED_FILES[cfile_i].needs_recompilation; + } + } + + let should_compile = (opts.first_run || needs_recompilation) + && ((opts.filter.len() == 0) + || opts + .filter + .contains(&path.as_path().to_str().unwrap_or("").to_string())); if metadata.is_file() && should_compile { println!("< Compiling {:#?}", path.as_path()); if let Err(e) = compile_file_and_write(path.as_path(), opts) { @@ -492,9 +515,8 @@ fn main() { let _ = std::fs::remove_dir_all(Path::new(&opts.build_dir)); println!("Copying static content"); let _ = copy_files_in_directory(Path::new(&opts.static_dir), &opts); - // Running twice, we should make macro_processor emit list of macros which was executed, such - // that we can re-compile only the files we need to - // We should also make some kind of file-list, and only re-compile files which has changed. + // Running compilation twice, needed for some macros which depends on compiled content + // We should make some kind of file-list, and only re-compile files which has changed. println!("Compiling content"); let _ = compile_files_in_directory(Path::new(&opts.content_dir), &opts); println!("Rerun compilation"); |