Handle changes to static files and site config

This commit is contained in:
Zoey 2021-08-18 17:12:24 -07:00
parent 58f252ed48
commit c67fcd19a7
2 changed files with 51 additions and 25 deletions

View file

@ -12,7 +12,7 @@ enum Mode {
#[cfg(feature = "serve")] #[cfg(feature = "serve")]
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let site = Site::new(Path::new("site"))?; let site = Site::new(&Path::new("site").canonicalize()?)?;
let mut mode = Mode::Build; let mut mode = Mode::Build;
for arg in std::env::args() { for arg in std::env::args() {
@ -34,7 +34,7 @@ async fn main() -> anyhow::Result<()> {
#[cfg(not(feature = "serve"))] #[cfg(not(feature = "serve"))]
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let site = Site::new(Path::new("site"))?; let site = Site::new(&Path::new("site").canonicalize()?)?;
site.build_once()?; site.build_once()?;
println!("Build complete!"); println!("Build complete!");

View file

@ -16,7 +16,7 @@ use warp::{
Filter, Filter,
}; };
use crate::{Site, SiteBuilder, PAGES_PATH, TEMPLATES_PATH}; use crate::{Site, SiteBuilder, PAGES_PATH, STATIC_PATH, TEMPLATES_PATH};
fn with_build_path( fn with_build_path(
build_path: PathBuf, build_path: PathBuf,
@ -59,8 +59,12 @@ fn create(
if build { if build {
builder.site.build_all_pages(builder)?; builder.site.build_all_pages(builder)?;
} }
} else { } else if let Ok(_static_path) = relative_path.strip_prefix(STATIC_PATH) {
anyhow::anyhow!("ahh"); std::fs::copy(path, builder.build_path.join(relative_path))?;
} else if relative_path.display().to_string() == "config.yaml" {
let new_config = serde_yaml::from_str(&std::fs::read_to_string(path)?)?;
builder.site.config = new_config;
builder.site.build_all_pages(builder)?;
} }
Ok(()) Ok(())
@ -78,12 +82,18 @@ fn remove(builder: &mut SiteBuilder, _path: &Path, relative_path: &Path) -> anyh
builder.site.template_index.remove(&template_name_str); builder.site.template_index.remove(&template_name_str);
builder.reg.unregister_template(&template_name_str); builder.reg.unregister_template(&template_name_str);
builder.site.build_all_pages(builder)?; builder.site.build_all_pages(builder)?;
} else { } else if let Ok(_static_path) = relative_path.strip_prefix(STATIC_PATH) {
anyhow::anyhow!("ahh"); std::fs::remove_file(builder.build_path.join(relative_path))?;
} }
Ok(()) Ok(())
} }
/// Decides whether to skip a path in the watcher.
fn skip_path(builder: &SiteBuilder, path: &Path) -> bool {
path.strip_prefix(&builder.build_path).is_ok()
}
impl Site { impl Site {
/// Serves the site for development. Don't use this in production. /// Serves the site for development. Don't use this in production.
pub async fn serve(self) -> anyhow::Result<()> { pub async fn serve(self) -> anyhow::Result<()> {
@ -114,30 +124,46 @@ impl Site {
match (|| match event { match (|| match event {
Event::Write(path) => { Event::Write(path) => {
let rel = rel(&path, &site_path)?; if skip_path(&builder, &path) {
println!("CHANGED - {:?}", rel); Ok(false)
create(&mut builder, &path, &rel, true)?; } else {
Ok::<_, anyhow::Error>(true) let rel = rel(&path, &site_path)?;
println!("CHANGED - {:?}", rel);
create(&mut builder, &path, &rel, true)?;
Ok::<_, anyhow::Error>(true)
}
} }
Event::Create(path) => { Event::Create(path) => {
let rel = rel(&path, &site_path)?; if skip_path(&builder, &path) {
println!("CREATED - {:?}", rel); Ok(false)
create(&mut builder, &path, &rel, true)?; } else {
Ok(true) let rel = rel(&path, &site_path)?;
println!("CREATED - {:?}", rel);
create(&mut builder, &path, &rel, true)?;
Ok(true)
}
} }
Event::Remove(path) => { Event::Remove(path) => {
let rel = rel(&path, &site_path)?; if skip_path(&builder, &path) {
println!("REMOVED - {:?}", rel); Ok(false)
remove(&mut builder, &path, &rel)?; } else {
Ok(true) let rel = rel(&path, &site_path)?;
println!("REMOVED - {:?}", rel);
remove(&mut builder, &path, &rel)?;
Ok(true)
}
} }
Event::Rename(old, new) => { Event::Rename(old, new) => {
let old_rel = rel(&old, &site_path)?; if skip_path(&builder, &old) && skip_path(&builder, &new) {
let new_rel = rel(&new, &site_path)?; Ok(false)
println!("RENAMED - {:?} -> {:?}", old_rel, new_rel); } else {
create(&mut builder, &new, &new_rel, false)?; let old_rel = rel(&old, &site_path)?;
remove(&mut builder, &old, &old_rel)?; let new_rel = rel(&new, &site_path)?;
Ok(true) println!("RENAMED - {:?} -> {:?}", old_rel, new_rel);
create(&mut builder, &new, &new_rel, false)?;
remove(&mut builder, &old, &old_rel)?;
Ok(true)
}
} }
_ => Ok(false), _ => Ok(false),
})() { })() {