diff options
author | Maarten de Vries <maarten@de-vri.es> | 2019-01-10 20:42:39 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2019-01-11 16:30:51 +0100 |
commit | 07904728f9708de152beea49ce67cafcd7225aa2 (patch) | |
tree | 93b9dcce32ec6a1f2a4d5fa0fd55116aa0a3be7b | |
parent | 7f6263ceddd6586ef2e65a5cbde5ee6555a43c23 (diff) | |
download | askama-07904728f9708de152beea49ce67cafcd7225aa2.tar.gz askama-07904728f9708de152beea49ce67cafcd7225aa2.tar.bz2 askama-07904728f9708de152beea49ce67cafcd7225aa2.zip |
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.
-rw-r--r-- | askama_derive/src/generator.rs | 26 |
1 files changed, 25 insertions, 1 deletions
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. |