fixed changes

master
Edward Shen 2021-09-28 22:01:17 -07:00
parent e06044fc97
commit 4fb22e2194
Signed by: edward
GPG Key ID: 19182661E818369F
1 changed files with 11 additions and 8 deletions

View File

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