diff options
author | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2020-06-20 10:58:18 +0000 |
---|---|---|
committer | Daniel Silverstone <dsilvers+gitlab@digital-scurf.org> | 2020-06-20 10:58:18 +0000 |
commit | f9e5f30e17f22240dee3a6b99fafea7ec0a0a066 (patch) | |
tree | 272715801099083825d51f7d14234215ce8068e0 | |
parent | 613eaaf82fbbbc96b05b19cc0c8bdbfec3a892ee (diff) | |
parent | 86a9f568c410bed2f4503a2e8d43e2dc2a0d70b8 (diff) | |
download | subplot-f9e5f30e17f22240dee3a6b99fafea7ec0a0a066.tar.gz |
Merge branch 'funcsrc' into 'master'
feat: supply source file name for functions files
Closes #64
See merge request larswirzenius/subplot!51
-rw-r--r-- | src/codegen.rs | 22 | ||||
-rw-r--r-- | templates/bash/template.sh | 7 | ||||
-rw-r--r-- | templates/python/template.py | 7 |
3 files changed, 31 insertions, 5 deletions
diff --git a/src/codegen.rs b/src/codegen.rs index 9895783..c1da2f8 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -2,10 +2,11 @@ use crate::{Document, SubplotError, TemplateSpec}; use std::collections::HashMap; use std::fs::File; use std::io::prelude::{Read, Write}; -use std::path::Path; +use std::path::{Path, PathBuf}; use base64::encode; +use serde::Serialize; use tera::{Context, Tera, Value}; use anyhow::Result; @@ -42,11 +43,11 @@ fn context(doc: &mut Document) -> Result<Context> { context.insert("files", doc.files()); let funcs_filenames = doc.meta().functions_filenames(); - let mut funcs = String::new(); + let mut funcs = vec![]; for filename in funcs_filenames { let content = cat(filename).map_err(|e| SubplotError::FunctionsFileNotFound(filename.into(), e))?; - funcs.push_str(&content); + funcs.push(Func::new(filename, content)); } context.insert("functions", &funcs); @@ -83,3 +84,18 @@ fn base64(v: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> { )), } } + +#[derive(Debug, Serialize)] +pub struct Func { + pub source: PathBuf, + pub code: String, +} + +impl Func { + pub fn new(source: &Path, code: String) -> Func { + Func { + source: source.to_path_buf(), + code, + } + } +} diff --git a/templates/bash/template.sh b/templates/bash/template.sh index 89958d4..1da4f5b 100644 --- a/templates/bash/template.sh +++ b/templates/bash/template.sh @@ -5,7 +5,12 @@ set -eu -o pipefail ############################################################################# # Functions that implement steps. -{{ functions }} +{% for func in functions %} +#---------------------------------------------------------------------------- +# This code comes from: {{ func.source }} + +{{ func.code }} +{% endfor %} ############################################################################# diff --git a/templates/python/template.py b/templates/python/template.py index 4ceeb51..af5b36e 100644 --- a/templates/python/template.py +++ b/templates/python/template.py @@ -1,7 +1,12 @@ ############################################################################# # Functions that implement steps. -{{ functions }} +{% for func in functions %} +#---------------------------------------------------------------------------- +# This code comes from: {{ func.source }} + +{{ func.code }} +{% endfor %} ############################################################################# |