2017-03-06 10:35:56 +00:00
|
|
|
use std::env;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::mpsc::channel;
|
2017-03-08 04:21:45 +00:00
|
|
|
use std::time::{Instant, Duration};
|
2017-03-06 10:35:56 +00:00
|
|
|
use std::thread;
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
use chrono::prelude::*;
|
2017-03-06 10:35:56 +00:00
|
|
|
use iron::{Iron, Request, IronResult, Response, status};
|
|
|
|
use mount::Mount;
|
|
|
|
use staticfile::Static;
|
|
|
|
use notify::{Watcher, RecursiveMode, watcher};
|
2017-03-10 11:39:58 +00:00
|
|
|
use ws::{WebSocket, Sender};
|
2017-03-08 00:13:50 +00:00
|
|
|
use gutenberg::Site;
|
2017-05-01 09:11:18 +00:00
|
|
|
use gutenberg::errors::{Result, ResultExt};
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2017-03-08 04:21:45 +00:00
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
use ::{report_elapsed_time, unravel_errors};
|
|
|
|
use console;
|
2017-03-08 04:21:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum ChangeKind {
|
|
|
|
Content,
|
|
|
|
Templates,
|
|
|
|
StaticFiles,
|
|
|
|
}
|
|
|
|
|
|
|
|
const LIVE_RELOAD: &'static str = include_str!("livereload.js");
|
2017-03-06 10:35:56 +00:00
|
|
|
|
|
|
|
|
2017-03-06 11:58:31 +00:00
|
|
|
fn livereload_handler(_: &mut Request) -> IronResult<Response> {
|
2017-03-08 04:21:45 +00:00
|
|
|
Ok(Response::with((status::Ok, LIVE_RELOAD.to_string())))
|
2017-03-06 10:35:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
fn rebuild_done_handling(broadcaster: &Sender, res: Result<()>, reload_path: &str) {
|
|
|
|
match res {
|
|
|
|
Ok(_) => {
|
|
|
|
broadcaster.send(format!(r#"
|
|
|
|
{{
|
|
|
|
"command": "reload",
|
|
|
|
"path": "{}",
|
|
|
|
"originalPath": "",
|
|
|
|
"liveCSS": true,
|
|
|
|
"liveImg": true,
|
|
|
|
"protocol": ["http://livereload.com/protocols/official-7"]
|
|
|
|
}}"#, reload_path)
|
|
|
|
).unwrap();
|
|
|
|
},
|
2017-03-25 06:59:12 +00:00
|
|
|
Err(e) => unravel_errors("Failed to build the site", &e, false)
|
2017-03-10 11:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-06 10:35:56 +00:00
|
|
|
// Most of it taken from mdbook
|
2017-03-25 04:18:15 +00:00
|
|
|
pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
|
2017-03-10 11:39:58 +00:00
|
|
|
let start = Instant::now();
|
2017-03-25 04:18:15 +00:00
|
|
|
let mut site = Site::new(env::current_dir().unwrap(), config_file)?;
|
2017-03-25 07:12:58 +00:00
|
|
|
|
2017-03-20 10:00:00 +00:00
|
|
|
let address = format!("{}:{}", interface, port);
|
|
|
|
// Override the base url so links work in localhost
|
|
|
|
site.config.base_url = if site.config.base_url.ends_with('/') {
|
|
|
|
format!("http://{}/", address)
|
|
|
|
} else {
|
|
|
|
format!("http://{}", address)
|
|
|
|
};
|
|
|
|
|
2017-03-21 07:57:00 +00:00
|
|
|
site.load()?;
|
2017-03-14 12:25:45 +00:00
|
|
|
site.enable_live_reload();
|
2017-05-09 12:12:10 +00:00
|
|
|
super::notify_site_size(&site);
|
|
|
|
super::warn_about_ignored_pages(&site);
|
2017-03-06 10:35:56 +00:00
|
|
|
site.build()?;
|
2017-03-10 11:39:58 +00:00
|
|
|
report_elapsed_time(start);
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2017-05-01 09:11:18 +00:00
|
|
|
// Setup watchers
|
|
|
|
let (tx, rx) = channel();
|
|
|
|
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
|
|
|
|
watcher.watch("content/", RecursiveMode::Recursive)
|
2017-05-03 08:52:49 +00:00
|
|
|
.chain_err(|| "Can't watch the `content` folder. Does it exist?")?;
|
2017-05-01 09:11:18 +00:00
|
|
|
watcher.watch("static/", RecursiveMode::Recursive)
|
2017-05-03 08:52:49 +00:00
|
|
|
.chain_err(|| "Can't watch the `static` folder. Does it exist?")?;
|
2017-05-01 09:11:18 +00:00
|
|
|
watcher.watch("templates/", RecursiveMode::Recursive)
|
2017-05-03 08:52:49 +00:00
|
|
|
.chain_err(|| "Can't watch the `templates` folder. Does it exist?")?;
|
2017-05-01 09:11:18 +00:00
|
|
|
|
2017-03-06 10:35:56 +00:00
|
|
|
let ws_address = format!("{}:{}", interface, "1112");
|
|
|
|
|
|
|
|
// Start a webserver that serves the `public` directory
|
|
|
|
let mut mount = Mount::new();
|
|
|
|
mount.mount("/", Static::new(Path::new("public/")));
|
|
|
|
mount.mount("/livereload.js", livereload_handler);
|
2017-03-07 12:34:31 +00:00
|
|
|
// Starts with a _ to not trigger the unused lint
|
|
|
|
// we need to assign to a variable otherwise it will block
|
2017-03-10 13:24:56 +00:00
|
|
|
let _iron = Iron::new(mount).http(address.as_str()).unwrap();
|
2017-03-06 10:35:56 +00:00
|
|
|
|
|
|
|
// The websocket for livereload
|
|
|
|
let ws_server = WebSocket::new(|_| {
|
|
|
|
|_| {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}).unwrap();
|
|
|
|
let broadcaster = ws_server.broadcaster();
|
|
|
|
thread::spawn(move || {
|
|
|
|
ws_server.listen(&*ws_address).unwrap();
|
|
|
|
});
|
|
|
|
|
2017-03-08 04:21:45 +00:00
|
|
|
let pwd = format!("{}", env::current_dir().unwrap().display());
|
2017-03-25 06:52:51 +00:00
|
|
|
|
2017-03-08 04:21:45 +00:00
|
|
|
println!("Listening for changes in {}/{{content, static, templates}}", pwd);
|
2017-03-25 06:52:51 +00:00
|
|
|
println!("Web server is available at http://{}", address);
|
|
|
|
println!("Press Ctrl+C to stop\n");
|
2017-03-06 10:35:56 +00:00
|
|
|
|
|
|
|
use notify::DebouncedEvent::*;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// See https://github.com/spf13/hugo/blob/master/commands/hugo.go
|
|
|
|
// for a more complete version of that
|
|
|
|
match rx.recv() {
|
2017-03-08 04:21:45 +00:00
|
|
|
Ok(event) => {
|
|
|
|
match event {
|
|
|
|
Create(path) |
|
|
|
|
Write(path) |
|
|
|
|
Remove(path) |
|
|
|
|
Rename(_, path) => {
|
|
|
|
if is_temp_file(&path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
println!("Change detected @ {}", Local::now().format("%Y-%m-%d %H:%M:%S").to_string());
|
|
|
|
let start = Instant::now();
|
|
|
|
match detect_change_kind(&pwd, &path) {
|
|
|
|
(ChangeKind::Content, _) => {
|
2017-03-25 06:52:51 +00:00
|
|
|
console::info(&format!("-> Content changed {}", path.display()));
|
2017-03-10 12:36:43 +00:00
|
|
|
// Force refresh
|
2017-03-21 07:57:00 +00:00
|
|
|
rebuild_done_handling(&broadcaster, site.rebuild_after_content_change(&path), "/x.js");
|
2017-03-10 11:39:58 +00:00
|
|
|
},
|
|
|
|
(ChangeKind::Templates, _) => {
|
2017-03-25 06:52:51 +00:00
|
|
|
console::info(&format!("-> Template changed {}", path.display()));
|
2017-03-10 12:36:43 +00:00
|
|
|
// Force refresh
|
2017-04-28 07:18:18 +00:00
|
|
|
rebuild_done_handling(&broadcaster, site.rebuild_after_template_change(&path), "/x.js");
|
2017-03-10 11:39:58 +00:00
|
|
|
},
|
2017-03-09 07:34:12 +00:00
|
|
|
(ChangeKind::StaticFiles, p) => {
|
2017-04-18 05:07:02 +00:00
|
|
|
if path.is_file() {
|
|
|
|
console::info(&format!("-> Static file changes detected {}", path.display()));
|
|
|
|
rebuild_done_handling(&broadcaster, site.copy_static_file(&path), &p);
|
|
|
|
}
|
2017-03-09 07:34:12 +00:00
|
|
|
},
|
2017-03-08 04:21:45 +00:00
|
|
|
};
|
2017-03-10 11:39:58 +00:00
|
|
|
report_elapsed_time(start);
|
2017-03-06 10:35:56 +00:00
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
_ => {}
|
2017-03-06 10:35:56 +00:00
|
|
|
}
|
|
|
|
},
|
2017-03-25 06:52:51 +00:00
|
|
|
Err(e) => console::error(&format!("Watch error: {:?}", e)),
|
2017-03-06 10:35:56 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
/// Returns whether the path we received corresponds to a temp file created
|
|
|
|
/// by an editor or the OS
|
2017-03-06 10:35:56 +00:00
|
|
|
fn is_temp_file(path: &Path) -> bool {
|
|
|
|
let ext = path.extension();
|
|
|
|
match ext {
|
|
|
|
Some(ex) => match ex.to_str().unwrap() {
|
|
|
|
"swp" | "swx" | "tmp" | ".DS_STORE" => true,
|
|
|
|
// jetbrains IDE
|
|
|
|
x if x.ends_with("jb_old___") => true,
|
|
|
|
x if x.ends_with("jb_tmp___") => true,
|
|
|
|
x if x.ends_with("jb_bak___") => true,
|
2017-03-08 04:21:45 +00:00
|
|
|
// vim
|
2017-03-10 13:19:36 +00:00
|
|
|
x if x.ends_with('~') => true,
|
2017-03-06 10:35:56 +00:00
|
|
|
_ => {
|
|
|
|
if let Some(filename) = path.file_stem() {
|
|
|
|
// emacs
|
2017-03-10 13:19:36 +00:00
|
|
|
filename.to_str().unwrap().starts_with('#')
|
2017-03-06 10:35:56 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2017-03-08 04:21:45 +00:00
|
|
|
None => {
|
2017-03-10 13:19:36 +00:00
|
|
|
path.ends_with(".DS_STORE")
|
2017-03-08 04:21:45 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Detect what changed from the given path so we have an idea what needs
|
|
|
|
/// to be reloaded
|
2017-03-09 07:34:12 +00:00
|
|
|
fn detect_change_kind(pwd: &str, path: &Path) -> (ChangeKind, String) {
|
2017-03-08 04:21:45 +00:00
|
|
|
let path_str = format!("{}", path.display())
|
|
|
|
.replace(pwd, "")
|
|
|
|
.replace("\\", "/");
|
|
|
|
let change_kind = if path_str.starts_with("/templates") {
|
|
|
|
ChangeKind::Templates
|
|
|
|
} else if path_str.starts_with("/content") {
|
|
|
|
ChangeKind::Content
|
|
|
|
} else if path_str.starts_with("/static") {
|
|
|
|
ChangeKind::StaticFiles
|
|
|
|
} else {
|
2017-03-25 06:52:51 +00:00
|
|
|
unreachable!("Got a change in an unexpected path: {}", path_str);
|
2017-03-08 04:21:45 +00:00
|
|
|
};
|
|
|
|
|
2017-03-09 07:34:12 +00:00
|
|
|
(change_kind, path_str)
|
2017-03-08 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use super::{is_temp_file, detect_change_kind, ChangeKind};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_recognize_temp_files() {
|
|
|
|
let testcases = vec![
|
|
|
|
Path::new("hello.swp"),
|
|
|
|
Path::new("hello.swx"),
|
|
|
|
Path::new(".DS_STORE"),
|
|
|
|
Path::new("hello.tmp"),
|
|
|
|
Path::new("hello.html.__jb_old___"),
|
|
|
|
Path::new("hello.html.__jb_tmp___"),
|
|
|
|
Path::new("hello.html.__jb_bak___"),
|
|
|
|
Path::new("hello.html~"),
|
|
|
|
Path::new("#hello.html"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for t in testcases {
|
|
|
|
assert!(is_temp_file(&t));
|
|
|
|
}
|
2017-03-06 10:35:56 +00:00
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_can_detect_kind_of_changes() {
|
|
|
|
let testcases = vec![
|
2017-03-09 07:34:12 +00:00
|
|
|
(
|
|
|
|
(ChangeKind::Templates, "/templates/hello.html".to_string()),
|
|
|
|
"/home/vincent/site", Path::new("/home/vincent/site/templates/hello.html")
|
|
|
|
),
|
|
|
|
(
|
|
|
|
(ChangeKind::StaticFiles, "/static/site.css".to_string()),
|
|
|
|
"/home/vincent/site", Path::new("/home/vincent/site/static/site.css")
|
|
|
|
),
|
|
|
|
(
|
|
|
|
(ChangeKind::Content, "/content/posts/hello.md".to_string()),
|
|
|
|
"/home/vincent/site", Path::new("/home/vincent/site/content/posts/hello.md")
|
|
|
|
),
|
2017-03-08 04:21:45 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (expected, pwd, path) in testcases {
|
|
|
|
assert_eq!(expected, detect_change_kind(&pwd, &path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|