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(())
}
-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};
Pat(String),
}
+#[derive(Debug, Clone)]
pub struct DirNode<'a> {
path: PathBuf,
opts: &'a Options,
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()?;
}
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(())
+}