fixed changes

This commit is contained in:
Edward Shen 2021-09-28 22:01:17 -07:00
parent e06044fc97
commit 4fb22e2194
Signed by: edward
GPG key ID: 19182661E818369F

View file

@ -8,6 +8,7 @@ use serde_json::Value;
use std::env;
use std::error::Error;
use std::fs::{create_dir_all, read_dir, read_to_string, write};
use std::path::{Path, PathBuf};
use std::process;
use tera::{Context, Error as TeraError, Tera};
@ -44,10 +45,12 @@ fn run() -> Result<(), Box<dyn Error>> {
.map(String::from)
.collect();
let template_dir = PathBuf::from(template_dir);
output(&tera, &context, &output_dir, &outputs, &template_dir)?;
if matches.is_present("watch") {
watch_mode(tera, context, output_dir, outputs, template_dir)?;
watch_mode(tera, context, output_dir, outputs, &template_dir)?;
}
Ok(())
@ -72,16 +75,16 @@ fn get_args() -> clap::ArgMatches<'static> {
/// Usually never returns, unless there was an error initializing the watcher.
/// Handles watching for file changes, and reloads tera if there's a change.
fn watch_mode<'a>(
fn watch_mode(
mut engine: Tera,
context: Context,
dir: String,
outputs: Vec<String>,
template_dir: String,
template_dir: &Path,
) -> Result<(), Box<dyn Error>> {
let (tx, rx) = unbounded();
let mut watcher: RecommendedWatcher = Watcher::new_immediate(move |res| tx.send(res).unwrap())?;
watcher.watch(&template_dir, RecursiveMode::Recursive)?;
let mut watcher: RecommendedWatcher = Watcher::new(move |res| tx.send(res).unwrap())?;
watcher.watch(template_dir, RecursiveMode::Recursive)?;
for res in rx {
match res {
@ -89,7 +92,7 @@ fn watch_mode<'a>(
Ok(event) => {
println!("got event {:?}", event);
engine.full_reload().expect("Failed to perform full reload");
output(&engine, &context, &dir, &outputs, &template_dir)
output(&engine, &context, &dir, &outputs, template_dir)
.expect("Failed to call output");
}
}
@ -100,12 +103,12 @@ fn watch_mode<'a>(
/// Parses the output values and generates a file for each format specified or
/// found, if told to generate all outputs.
fn output<'a>(
fn output(
engine: &Tera,
context: &Context,
dir: &str,
outputs: &[String],
template_dir: &'a str,
template_dir: &Path,
) -> Result<(), Box<dyn Error>> {
if outputs.contains(&String::from("all")) {
for output in read_dir(template_dir)? {