Skip to content

Commit

Permalink
fix: add static files to binary in xbuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosthe19916 committed Aug 11, 2024
1 parent cb37410 commit 2f941e2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions xbuilder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ chrono = { workspace = true, features = ["serde"] }
regex = { workspace = true }
log = { workspace = true }
tera = { workspace = true }
static-files = { workspace = true }
lazy_static = { workspace = true }
serde = { workspace = true, features = ["derive"] }
rust_decimal = { workspace = true, features = ["serde-str", "serde-with-str"] }
Expand All @@ -23,3 +24,6 @@ tokio = { workspace = true, features = ["macros"] }

xsender = { path = "../xsender" }
xsigner = { path = "../xsigner" }

[build-dependencies]
static-files = { workspace = true }
5 changes: 5 additions & 0 deletions xbuilder/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use static_files::resource_dir;

fn main() -> std::io::Result<()> {
resource_dir("./src/templates").build()
}
47 changes: 40 additions & 7 deletions xbuilder/src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

use std::cmp::Ordering;
use std::collections::HashMap;
use std::str::FromStr;
use std::str::{from_utf8, FromStr};

use rust_decimal::Decimal;
use static_files::Resource;
use tera::helpers::tests::{number_args_allowed, value_defined};
use tera::{from_value, to_value, Context, Error, Function, Tera, Value};

Expand Down Expand Up @@ -111,13 +115,42 @@ pub fn credito(value: Option<&Value>, params: &[Value]) -> tera::Result<bool> {

lazy_static::lazy_static! {
pub static ref TEMPLATES: Tera = {
let mut tera = match Tera::new("src/templates/**/*.xml") {
Ok(t) => t,
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
let mut tera = Tera::default();

let resources = generate();
let mut resources_sorted = resources.into_iter()
.filter(|(name, _content)| name.ends_with(".xml"))
.collect::<Vec<(&str, Resource)>>();
// Load the renderer files at the end to avoid dependency errors between templates
resources_sorted.sort_by(|(a,_),(b,_)| {
if a.starts_with("renderer") && b.starts_with("renderer") {
a.partial_cmp(b).unwrap_or(Ordering::Equal)
} else if a.starts_with("renderer") {
Ordering::Greater
} else if b.starts_with("renderer") {
Ordering::Less
} else {
a.partial_cmp(b).unwrap_or(Ordering::Equal)
}
};
});
resources_sorted.iter().for_each(|(name, content)| {
match from_utf8(content.data) {
Ok(template_raw_content) => {
match tera.add_raw_template(name, template_raw_content) {
Ok(_) => {},
Err(e) => {
println!("Adding template error(s): {}", e);
::std::process::exit(1);
}
};
},
Err(e) => {
println!("Parsing error(s): {}", e);
::std::process::exit(1);
}
};
});

tera.register_function("catalog7_taxcategory", catalog7_taxcategory());
tera.register_filter("multiply100", multiply100);
tera.register_filter("round_decimal", round_decimal);
Expand Down

0 comments on commit 2f941e2

Please sign in to comment.