Copy only modified files in static directory (#27)
Copy only static files that triggered the change notification
This commit is contained in:
parent
c708295865
commit
9acb70442f
|
@ -134,8 +134,10 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
|
||||||
rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), "/x.js");
|
rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(), "/x.js");
|
||||||
},
|
},
|
||||||
(ChangeKind::StaticFiles, p) => {
|
(ChangeKind::StaticFiles, p) => {
|
||||||
console::info(&format!("-> Static file changes detected {}", path.display()));
|
if path.is_file() {
|
||||||
rebuild_done_handling(&broadcaster, site.copy_static_directory(), &p);
|
console::info(&format!("-> Static file changes detected {}", path.display()));
|
||||||
|
rebuild_done_handling(&broadcaster, site.copy_static_file(&path), &p);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
report_elapsed_time(start);
|
report_elapsed_time(start);
|
||||||
|
|
45
src/site.rs
45
src/site.rs
|
@ -1,6 +1,6 @@
|
||||||
use std::collections::{BTreeMap, HashMap};
|
use std::collections::{BTreeMap, HashMap};
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::fs::{remove_dir_all, copy, remove_file};
|
use std::fs::{remove_dir_all, copy, create_dir_all};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use glob::glob;
|
use glob::glob;
|
||||||
|
@ -67,6 +67,7 @@ pub struct Site {
|
||||||
pub tera: Tera,
|
pub tera: Tera,
|
||||||
live_reload: bool,
|
live_reload: bool,
|
||||||
output_path: PathBuf,
|
output_path: PathBuf,
|
||||||
|
static_path: PathBuf,
|
||||||
pub tags: HashMap<String, Vec<PathBuf>>,
|
pub tags: HashMap<String, Vec<PathBuf>>,
|
||||||
pub categories: HashMap<String, Vec<PathBuf>>,
|
pub categories: HashMap<String, Vec<PathBuf>>,
|
||||||
}
|
}
|
||||||
|
@ -91,7 +92,8 @@ impl Site {
|
||||||
sections: BTreeMap::new(),
|
sections: BTreeMap::new(),
|
||||||
tera: tera,
|
tera: tera,
|
||||||
live_reload: false,
|
live_reload: false,
|
||||||
output_path: PathBuf::from("public"),
|
output_path: path.join("public"),
|
||||||
|
static_path: path.join("static"),
|
||||||
tags: HashMap::new(),
|
tags: HashMap::new(),
|
||||||
categories: HashMap::new(),
|
categories: HashMap::new(),
|
||||||
};
|
};
|
||||||
|
@ -223,31 +225,30 @@ impl Site {
|
||||||
html
|
html
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Copy the content of the `static` folder into the `public` folder
|
/// Copy static file to public directory.
|
||||||
///
|
pub fn copy_static_file<P: AsRef<Path>>(&self, path: P) -> Result<()> {
|
||||||
/// TODO: only copy one file if possible because that would be a waste
|
let relative_path = path.as_ref().strip_prefix(&self.static_path).unwrap();
|
||||||
/// to do re-copy the whole thing. Benchmark first to see if it's a big difference
|
let target_path = self.output_path.join(relative_path);
|
||||||
pub fn copy_static_directory(&self) -> Result<()> {
|
if let Some(parent_directory) = target_path.parent() {
|
||||||
let from = Path::new("static");
|
create_dir_all(parent_directory)?;
|
||||||
let target = Path::new("public");
|
}
|
||||||
|
copy(path.as_ref(), &target_path)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
for entry in WalkDir::new(from).into_iter().filter_map(|e| e.ok()) {
|
/// Copy the content of the `static` folder into the `public` folder
|
||||||
let relative_path = entry.path().strip_prefix(&from).unwrap();
|
pub fn copy_static_directory(&self) -> Result<()> {
|
||||||
let target_path = {
|
for entry in WalkDir::new(&self.static_path).into_iter().filter_map(|e| e.ok()) {
|
||||||
let mut target_path = target.to_path_buf();
|
let relative_path = entry.path().strip_prefix(&self.static_path).unwrap();
|
||||||
target_path.push(relative_path);
|
let target_path = self.output_path.join(relative_path);
|
||||||
target_path
|
|
||||||
};
|
|
||||||
|
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir() {
|
||||||
if !target_path.exists() {
|
if !target_path.exists() {
|
||||||
create_directory(&target_path)?;
|
create_directory(&target_path)?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if target_path.exists() {
|
let entry_fullpath = self.base_path.join(entry.path());
|
||||||
remove_file(&target_path)?;
|
self.copy_static_file(entry_fullpath)?;
|
||||||
}
|
|
||||||
copy(entry.path(), &target_path)?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -255,9 +256,9 @@ impl Site {
|
||||||
|
|
||||||
/// Deletes the `public` directory if it exists
|
/// Deletes the `public` directory if it exists
|
||||||
pub fn clean(&self) -> Result<()> {
|
pub fn clean(&self) -> Result<()> {
|
||||||
if Path::new("public").exists() {
|
if self.output_path.exists() {
|
||||||
// Delete current `public` directory so we can start fresh
|
// Delete current `public` directory so we can start fresh
|
||||||
remove_dir_all("public").chain_err(|| "Couldn't delete `public` directory")?;
|
remove_dir_all(&self.output_path).chain_err(|| "Couldn't delete `public` directory")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -58,7 +58,6 @@ pub fn find_content_components<P: AsRef<Path>>(path: P) -> Vec<String> {
|
||||||
components
|
components
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{find_content_components};
|
use super::{find_content_components};
|
||||||
|
|
Loading…
Reference in a new issue