Consilidate template libraries
Remove last uses of `string_template` in favor of `minijinja`. Closes #124.
This commit is contained in:
parent
831592edf6
commit
e336240094
5 changed files with 36 additions and 34 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -937,6 +937,7 @@ dependencies = [
|
|||
"futures-util",
|
||||
"glob",
|
||||
"luajit2-sys",
|
||||
"minijinja",
|
||||
"nanorand",
|
||||
"notify",
|
||||
"oodle",
|
||||
|
@ -948,7 +949,6 @@ dependencies = [
|
|||
"serde",
|
||||
"serde_sjson",
|
||||
"shlex",
|
||||
"string_template",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
|
@ -3307,15 +3307,6 @@ dependencies = [
|
|||
"float-cmp",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "string_template"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc6f2c6b2c3fa950895c9aeb0c3cb9271d7eb580662af9af2b711b593f446320"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strip-ansi-escapes"
|
||||
version = "0.2.0"
|
||||
|
|
|
@ -14,6 +14,7 @@ exclude = ["lib/color-eyre"]
|
|||
|
||||
[workspace.dependencies]
|
||||
zip = { version = "2.1.3", default-features = false, features = ["deflate", "bzip2", "zstd", "time"] }
|
||||
minijinja = { version = "2.0.1", default-features = false }
|
||||
|
||||
[patch.crates-io]
|
||||
color-eyre = { path = "lib/color-eyre" }
|
||||
|
|
|
@ -27,7 +27,7 @@ futures = "0.3.25"
|
|||
interprocess = "2.1.0"
|
||||
lazy_static = "1.4.0"
|
||||
luajit2-sys = { path = "../../lib/luajit2-sys", version = "*" }
|
||||
minijinja = { version = "2.0.1", default-features = false }
|
||||
minijinja = { workspace = true }
|
||||
nexusmods = { path = "../../lib/nexusmods", version = "*" }
|
||||
oodle = { path = "../../lib/oodle", version = "*" }
|
||||
open = "5.0.1"
|
||||
|
|
|
@ -20,7 +20,7 @@ promptly = "0.3.1"
|
|||
sdk = { path = "../../lib/sdk", version = "*" }
|
||||
serde_sjson = { path = "../../lib/serde_sjson", version = "*" }
|
||||
serde = { version = "1.0.147", features = ["derive"] }
|
||||
string_template = "0.2.1"
|
||||
minijinja = { workspace = true }
|
||||
tokio-stream = { version = "0.1.11", features = ["fs", "io-util"] }
|
||||
tokio = { version = "1.21.2", features = ["rt-multi-thread", "fs", "process", "macros", "tracing", "io-util", "io-std"] }
|
||||
tracing-error = "0.2.0"
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{Arg, ArgMatches, Command};
|
||||
use color_eyre::eyre::{self, Context, Result};
|
||||
use color_eyre::Help;
|
||||
use futures::{StreamExt, TryStreamExt};
|
||||
use string_template::Template;
|
||||
use minijinja::Environment;
|
||||
use tokio::fs::{self, DirBuilder};
|
||||
|
||||
const TEMPLATES: [(&str, &str); 5] = [
|
||||
|
@ -137,19 +136,23 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()>
|
|||
|
||||
tracing::debug!(root = %root.display(), name, id);
|
||||
|
||||
let mut data = HashMap::new();
|
||||
data.insert("name", name.as_str());
|
||||
data.insert("id", id.as_str());
|
||||
let render_ctx = minijinja::context!(name => name.as_str(), id => id.as_str());
|
||||
let env = Environment::new();
|
||||
|
||||
let templates = TEMPLATES
|
||||
.iter()
|
||||
.map(|(path_tmpl, content_tmpl)| {
|
||||
let path = Template::new(path_tmpl).render(&data);
|
||||
let content = Template::new(content_tmpl).render(&data);
|
||||
|
||||
(root.join(path), content)
|
||||
env.render_str(path_tmpl, &render_ctx)
|
||||
.wrap_err_with(|| format!("Failed to render template: {}", path_tmpl))
|
||||
.and_then(|path| {
|
||||
env.render_named_str(&path, content_tmpl, &render_ctx)
|
||||
.wrap_err_with(|| format!("Failed to render template '{}'", &path))
|
||||
.map(|content| (root.join(path), content))
|
||||
})
|
||||
.map(|(path, content)| async move {
|
||||
})
|
||||
.map(|res| async move {
|
||||
match res {
|
||||
Ok((path, content)) => {
|
||||
let dir = path
|
||||
.parent()
|
||||
.ok_or_else(|| eyre::eyre!("invalid root path"))?;
|
||||
|
@ -158,13 +161,20 @@ pub(crate) async fn run(_ctx: sdk::Context, matches: &ArgMatches) -> Result<()>
|
|||
.recursive(true)
|
||||
.create(&dir)
|
||||
.await
|
||||
.wrap_err_with(|| format!("Failed to create directory {}", dir.display()))?;
|
||||
.wrap_err_with(|| {
|
||||
format!("Failed to create directory {}", dir.display())
|
||||
})?;
|
||||
|
||||
tracing::trace!("Writing file {}", path.display());
|
||||
|
||||
fs::write(&path, content.as_bytes())
|
||||
.await
|
||||
.wrap_err_with(|| format!("Failed to write content to path {}", path.display()))
|
||||
.wrap_err_with(|| {
|
||||
format!("Failed to write content to path {}", path.display())
|
||||
})
|
||||
}
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
});
|
||||
|
||||
futures::stream::iter(templates)
|
||||
|
|
Loading…
Add table
Reference in a new issue