From: Huck Boles Date: Sun, 14 May 2023 22:38:43 +0000 (-0500) Subject: builds directory X-Git-Url: https://git.huck.website/?a=commitdiff_plain;h=7a41966b489a74105393160fce7a57f3df6b1ba1;p=metaforge.git builds directory --- diff --git a/src/builder.rs b/src/builder.rs index ad0d00f..df25a84 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -24,7 +24,7 @@ pub fn build_metafile(file: &MetaFile) -> Result { pub fn write_file(path: &Path, html: String, opts: &Options) -> Result<()> { let dest = find_dest(path, opts)?; // want newline to end file - fs::write(dest, html + "\n")?; + fs::write(dest, html)?; Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 7f342f3..d7443ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,5 +36,5 @@ pub fn build_dir(opts: &Options) -> Result<()> { source.map(&global_init)?; - Ok(()) + source.build_dir() } diff --git a/src/metafile.rs b/src/metafile.rs index a090dc3..ce16a98 100644 --- a/src/metafile.rs +++ b/src/metafile.rs @@ -1,4 +1,4 @@ -use crate::{build_metafile, parse_file, Options}; +use crate::{build_metafile, parse_file, write_file, Options}; use color_eyre::{eyre::eyre, Result}; use std::collections::HashMap; use std::{fs, path::PathBuf}; @@ -108,6 +108,7 @@ pub enum Sub { Pat(String), } +#[derive(Debug, Clone)] pub struct DirNode<'a> { path: PathBuf, opts: &'a Options, @@ -163,23 +164,29 @@ impl<'a> DirNode<'a> { pub fn build_files(&mut self) -> Result<()> { for file in self.files.iter_mut() { file.merge(&self.global); - if let Err(e) = build_metafile(file) { - if self.opts.force { - // print a line to stderr about failure but continue with other files - eprintln!("ignoring {}: {}", file.path.display(), e); - continue; - } else { - return Err(e.wrap_err(eyre!("{}:", file.path.display()))); + match build_metafile(file) { + Ok(str) => { + write_file(&file.path, str, file.opts)?; + } + Err(e) => { + if self.opts.force { + // print a line to stderr about failure but continue with other files + eprintln!("ignoring {}: {}", file.path.display(), e); + continue; + } else { + return Err(e.wrap_err(eyre!("{}:", file.path.display()))); + } } } } Ok(()) } - pub fn build_dir(&mut self) -> Result<()> { + pub fn build_dir(&'a mut self) -> Result<()> { self.build_files()?; for dir in self.dirs.iter_mut() { + dir.map(&self.global)?; dir.build_dir()?; } diff --git a/tests/build_dir.rs b/tests/build_dir.rs index f18d0f6..570ba1e 100644 --- a/tests/build_dir.rs +++ b/tests/build_dir.rs @@ -1,4 +1,26 @@ use color_eyre::Result; #[test] -fn build_dir() -> Result<()> {} +fn build_test_site() -> Result<()> { + let dir = std::path::PathBuf::from("files/site") + .canonicalize() + .unwrap(); + + let mut opts = metaforge::Options::new(); + opts.root = dir.clone(); + opts.source = dir.join("source"); + opts.build = dir.join("build"); + opts.pattern = dir.join("pattern"); + opts.clean = true; + + metaforge::build_dir(&opts)?; + + assert!(opts.build.join("benchmark.html").exists()); + assert!(opts.build.join("dir1/sub_dir1/deep1/deep.html").exists()); + assert_eq!( + std::fs::read_to_string(opts.build.join("root.html"))?, + "

TEST

\n" + ); + + Ok(()) +}