]> git.huck.website - metaforge.git/commitdiff
builds directory
authorHuck Boles <huck@huck.website>
Sun, 14 May 2023 22:38:43 +0000 (17:38 -0500)
committerHuck Boles <huck@huck.website>
Sun, 14 May 2023 22:38:43 +0000 (17:38 -0500)
src/builder.rs
src/lib.rs
src/metafile.rs
tests/build_dir.rs

index ad0d00f79a915d4466a72e8ba6ddceb982fa81d5..df25a846adf0532add2ff08c02ca7f35c4ac2af1 100644 (file)
@@ -24,7 +24,7 @@ pub fn build_metafile(file: &MetaFile) -> Result<String> {
 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(())
 }
 
index 7f342f3725204bc7dea0c8c22e5f242cd547c9ee..d7443ce1765fb29ccba126a7a534c4540b395b22 100644 (file)
@@ -36,5 +36,5 @@ pub fn build_dir(opts: &Options) -> Result<()> {
 
     source.map(&global_init)?;
 
-    Ok(())
+    source.build_dir()
 }
index a090dc3ecd61501c53223d68726f40e146b0fa8e..ce16a9883bce106191f43c943a1aa72202143985 100644 (file)
@@ -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()?;
         }
 
index f18d0f600c54ecabc9400cd99aa7c08d58ae019a..570ba1eaf9f3213c43c3a7bae8d6b01215acc30f 100644 (file)
@@ -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"))?,
+        "<p>TEST</p>\n"
+    );
+
+    Ok(())
+}