From 07904728f9708de152beea49ce67cafcd7225aa2 Mon Sep 17 00:00:00 2001 From: Maarten de Vries Date: Thu, 10 Jan 2019 20:42:39 +0100 Subject: Inject template file into generated source as bytes. This is meant to allow the compiler to understand the dependency between the generated code and the template source. It removes the need for a build script. --- askama_derive/src/generator.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'askama_derive/src') diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs index 288ddae..16a08bb 100644 --- a/askama_derive/src/generator.rs +++ b/askama_derive/src/generator.rs @@ -1,5 +1,5 @@ use super::{get_template_source, Context, Heritage}; -use crate::input::TemplateInput; +use crate::input::{Source, TemplateInput}; use crate::parser::{ Cond, Expr, MatchParameter, MatchParameters, MatchVariant, Node, Target, When, WS, }; @@ -108,6 +108,21 @@ impl<'a> Generator<'a> { ::askama::Result<()> {", ); + // Make sure the compiler understands that the generated code depends on the template files. + for (path, _) in self.contexts { + // Skip the fake path of templates defined in rust source. + let path_is_valid = match self.input.source { + Source::Path(_) => true, + Source::Source(_) => *path != &self.input.path, + }; + if path_is_valid { + let path = path.to_str().unwrap(); + buf.writeln("e! { + include_bytes!(#path); + }.to_string()); + } + } + if let Some(heritage) = self.heritage { self.handle(heritage.root, heritage.root.nodes, buf, AstLevel::Top); } else { @@ -559,6 +574,15 @@ impl<'a> Generator<'a> { .find_template(path, Some(&self.input.path)); let src = get_template_source(&path); let nodes = parse(&src, self.input.syntax); + + // Make sure the compiler understands that the generated code depends on the template file. + { + let path = path.to_str().unwrap(); + buf.writeln("e! { + include_bytes!(#path); + }.to_string()); + } + { // Since nodes must not outlive the Generator, we instantiate // a nested Generator here to handle the include's nodes. -- cgit