ADD: reload when themes change (#771)

* ADD: reload when themes change

close #713

* MOD: test can_detect_kind_of_changes
This commit is contained in:
Ever 2019-08-25 01:05:02 +08:00 committed by Vincent Prouillet
parent 6a7e955ab0
commit 4fff7508ba

View file

@ -52,6 +52,7 @@ use rebuild;
enum ChangeKind { enum ChangeKind {
Content, Content,
Templates, Templates,
Themes,
StaticFiles, StaticFiles,
Sass, Sass,
Config, Config,
@ -155,6 +156,7 @@ pub fn serve(
// Setup watchers // Setup watchers
let mut watching_static = false; let mut watching_static = false;
let mut watching_templates = false; let mut watching_templates = false;
let mut watching_themes = false;
let (tx, rx) = channel(); let (tx, rx) = channel();
let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap(); let mut watcher = watcher(tx, Duration::from_secs(1)).unwrap();
watcher watcher
@ -178,6 +180,13 @@ pub fn serve(
.map_err(|e| ZolaError::chain("Can't watch the `templates` folder.", e))?; .map_err(|e| ZolaError::chain("Can't watch the `templates` folder.", e))?;
} }
if Path::new("themes").exists() {
watching_themes = true;
watcher
.watch("themes/", RecursiveMode::Recursive)
.map_err(|e| ZolaError::chain("Can't watch the `themes` folder.", e))?;
}
// Sass support is optional so don't make it an error to no have a sass folder // Sass support is optional so don't make it an error to no have a sass folder
let _ = watcher.watch("sass/", RecursiveMode::Recursive); let _ = watcher.watch("sass/", RecursiveMode::Recursive);
@ -248,6 +257,9 @@ pub fn serve(
if watching_templates { if watching_templates {
watchers.push("templates"); watchers.push("templates");
} }
if watching_themes {
watchers.push("themes");
}
if site.config.compile_sass { if site.config.compile_sass {
watchers.push("sass"); watchers.push("sass");
} }
@ -362,6 +374,19 @@ pub fn serve(
ChangeKind::Templates => reload_templates(&mut site, &path), ChangeKind::Templates => reload_templates(&mut site, &path),
ChangeKind::StaticFiles => copy_static(&site, &path, &partial_path), ChangeKind::StaticFiles => copy_static(&site, &path, &partial_path),
ChangeKind::Sass => reload_sass(&site, &path, &partial_path), ChangeKind::Sass => reload_sass(&site, &path, &partial_path),
ChangeKind::Themes => {
console::info("-> Themes changed. The whole site will be reloaded.");
site = create_new_site(
interface,
port,
output_dir,
base_url,
config_file,
)
.unwrap()
.0;
rebuild_done_handling(&broadcaster, Ok(()), "/x.js");
},
ChangeKind::Config => { ChangeKind::Config => {
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.");
site = create_new_site( site = create_new_site(
@ -406,6 +431,19 @@ pub fn serve(
(ChangeKind::Templates, _) => reload_templates(&mut site, &path), (ChangeKind::Templates, _) => reload_templates(&mut site, &path),
(ChangeKind::StaticFiles, p) => copy_static(&site, &path, &p), (ChangeKind::StaticFiles, p) => copy_static(&site, &path, &p),
(ChangeKind::Sass, p) => reload_sass(&site, &path, &p), (ChangeKind::Sass, p) => reload_sass(&site, &path, &p),
(ChangeKind::Themes, _) => {
console::info("-> Themes changed. The whole site will be reloaded.");
site = create_new_site(
interface,
port,
output_dir,
base_url,
config_file,
)
.unwrap()
.0;
rebuild_done_handling(&broadcaster, Ok(()), "/x.js");
},
(ChangeKind::Config, _) => { (ChangeKind::Config, _) => {
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."); console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.");
site = create_new_site( site = create_new_site(
@ -471,6 +509,8 @@ fn detect_change_kind(pwd: &Path, path: &Path) -> (ChangeKind, PathBuf) {
let change_kind = if partial_path.starts_with("/templates") { let change_kind = if partial_path.starts_with("/templates") {
ChangeKind::Templates ChangeKind::Templates
} else if partial_path.starts_with("/themes") {
ChangeKind::Themes
} else if partial_path.starts_with("/content") { } else if partial_path.starts_with("/content") {
ChangeKind::Content ChangeKind::Content
} else if partial_path.starts_with("/static") { } else if partial_path.starts_with("/static") {
@ -527,6 +567,11 @@ mod tests {
Path::new("/home/vincent/site"), Path::new("/home/vincent/site"),
Path::new("/home/vincent/site/templates/hello.html"), Path::new("/home/vincent/site/templates/hello.html"),
), ),
(
(ChangeKind::Themes, PathBuf::from("/themes/hello.html")),
Path::new("/home/vincent/site"),
Path::new("/home/vincent/site/themes/hello.html"),
),
( (
(ChangeKind::StaticFiles, PathBuf::from("/static/site.css")), (ChangeKind::StaticFiles, PathBuf::from("/static/site.css")),
Path::new("/home/vincent/site"), Path::new("/home/vincent/site"),