2017-06-07 09:25:36 +00:00
|
|
|
// Contains an embedded version of livereload-js
|
|
|
|
//
|
|
|
|
// Copyright (c) 2010-2012 Andrey Tarantsov
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
// a copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
// permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
// the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be
|
|
|
|
// included in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2017-03-06 10:35:56 +00:00
|
|
|
use std::env;
|
2018-11-10 21:23:37 +00:00
|
|
|
use std::fs::{remove_dir_all, File, read_dir};
|
2018-06-24 19:08:33 +00:00
|
|
|
use std::io::{self, Read};
|
2018-10-02 09:31:18 +00:00
|
|
|
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
|
2017-03-06 10:35:56 +00:00
|
|
|
use std::sync::mpsc::channel;
|
|
|
|
use std::thread;
|
2018-10-31 07:18:57 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2017-03-03 08:12:40 +00:00
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
use actix_web::middleware::{Middleware, Response, Started};
|
2018-06-24 19:08:33 +00:00
|
|
|
use actix_web::{self, fs, http, server, App, HttpRequest, HttpResponse, Responder};
|
2018-10-31 07:18:57 +00:00
|
|
|
use chrono::prelude::*;
|
2018-01-22 17:11:25 +00:00
|
|
|
use ctrlc;
|
2018-10-31 07:18:57 +00:00
|
|
|
use notify::{watcher, RecursiveMode, Watcher};
|
|
|
|
use ws::{Message, Sender, WebSocket};
|
2018-01-22 17:11:25 +00:00
|
|
|
|
2017-07-01 07:47:41 +00:00
|
|
|
use errors::{Result, ResultExt};
|
2018-10-31 07:18:57 +00:00
|
|
|
use site::Site;
|
2018-03-14 21:03:06 +00:00
|
|
|
use utils::fs::copy_file;
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
use console;
|
2017-05-13 13:37:01 +00:00
|
|
|
use rebuild;
|
2017-03-08 04:21:45 +00:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
enum ChangeKind {
|
|
|
|
Content,
|
|
|
|
Templates,
|
|
|
|
StaticFiles,
|
2017-07-06 13:19:15 +00:00
|
|
|
Sass,
|
2018-01-12 10:50:29 +00:00
|
|
|
Config,
|
2017-03-08 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 08:11:14 +00:00
|
|
|
// Uglified using uglifyjs
|
|
|
|
// Also, commenting out the lines 330-340 (containing `e instanceof ProtocolError`) was needed
|
|
|
|
// as it seems their build didn't work well and didn't include ProtocolError so it would error on
|
|
|
|
// errors
|
2018-09-30 19:15:09 +00:00
|
|
|
const LIVE_RELOAD: &str = include_str!("livereload.js");
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2018-06-26 06:24:57 +00:00
|
|
|
struct NotFoundHandler {
|
|
|
|
rendered_template: PathBuf,
|
|
|
|
}
|
2018-04-26 21:14:37 +00:00
|
|
|
|
2018-06-26 06:24:57 +00:00
|
|
|
impl<S> Middleware<S> for NotFoundHandler {
|
2018-07-22 10:14:16 +00:00
|
|
|
fn start(&self, _req: &HttpRequest<S>) -> actix_web::Result<Started> {
|
2018-06-24 19:08:33 +00:00
|
|
|
Ok(Started::Done)
|
|
|
|
}
|
2018-04-26 21:14:37 +00:00
|
|
|
|
2018-06-24 19:08:33 +00:00
|
|
|
fn response(
|
|
|
|
&self,
|
2018-07-22 10:14:16 +00:00
|
|
|
_req: &HttpRequest<S>,
|
2018-06-24 19:08:33 +00:00
|
|
|
mut resp: HttpResponse,
|
|
|
|
) -> actix_web::Result<Response> {
|
|
|
|
if http::StatusCode::NOT_FOUND == resp.status() {
|
2018-06-26 06:24:57 +00:00
|
|
|
let mut fh = File::open(&self.rendered_template)?;
|
|
|
|
let mut buf: Vec<u8> = vec![];
|
|
|
|
let _ = fh.read_to_end(&mut buf)?;
|
|
|
|
resp.replace_body(buf);
|
|
|
|
resp.headers_mut().insert(
|
|
|
|
http::header::CONTENT_TYPE,
|
|
|
|
http::header::HeaderValue::from_static("text/html"),
|
|
|
|
);
|
2018-04-26 21:14:37 +00:00
|
|
|
}
|
2018-06-24 19:08:33 +00:00
|
|
|
Ok(Response::Done(resp))
|
2018-04-26 21:14:37 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2018-07-22 10:14:16 +00:00
|
|
|
fn livereload_handler(_: &HttpRequest) -> &'static str {
|
2018-05-27 05:19:01 +00:00
|
|
|
LIVE_RELOAD
|
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(_) => {
|
2018-10-31 07:18:57 +00:00
|
|
|
broadcaster
|
|
|
|
.send(format!(
|
|
|
|
r#"
|
2017-03-10 11:39:58 +00:00
|
|
|
{{
|
|
|
|
"command": "reload",
|
|
|
|
"path": "{}",
|
|
|
|
"originalPath": "",
|
|
|
|
"liveCSS": true,
|
|
|
|
"liveImg": true,
|
|
|
|
"protocol": ["http://livereload.com/protocols/official-7"]
|
2018-10-31 07:18:57 +00:00
|
|
|
}}"#,
|
|
|
|
reload_path
|
|
|
|
))
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
Err(e) => console::unravel_errors("Failed to build the site", &e),
|
2017-03-10 11:39:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
fn create_new_site(
|
|
|
|
interface: &str,
|
|
|
|
port: u16,
|
|
|
|
output_dir: &str,
|
|
|
|
base_url: &str,
|
|
|
|
config_file: &str,
|
|
|
|
) -> Result<(Site, String)> {
|
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
|
|
|
|
2018-02-02 16:18:07 +00:00
|
|
|
let base_address = format!("{}:{}", base_url, port);
|
2017-03-20 10:00:00 +00:00
|
|
|
let address = format!("{}:{}", interface, port);
|
2018-02-02 20:35:04 +00:00
|
|
|
let base_url = if site.config.base_url.ends_with('/') {
|
2018-02-02 16:18:07 +00:00
|
|
|
format!("http://{}/", base_address)
|
2017-03-20 10:00:00 +00:00
|
|
|
} else {
|
2018-02-02 16:18:07 +00:00
|
|
|
format!("http://{}", base_address)
|
2017-03-20 10:00:00 +00:00
|
|
|
};
|
2018-02-02 16:18:07 +00:00
|
|
|
|
2018-02-02 20:35:04 +00:00
|
|
|
site.set_base_url(base_url);
|
2017-12-29 18:25:06 +00:00
|
|
|
site.set_output_path(output_dir);
|
2017-03-21 07:57:00 +00:00
|
|
|
site.load()?;
|
2018-10-19 14:33:11 +00:00
|
|
|
site.enable_live_reload(port);
|
2017-05-12 14:10:21 +00:00
|
|
|
console::notify_site_size(&site);
|
|
|
|
console::warn_about_ignored_pages(&site);
|
2017-03-06 10:35:56 +00:00
|
|
|
site.build()?;
|
2018-01-12 10:50:29 +00:00
|
|
|
Ok((site, address))
|
|
|
|
}
|
|
|
|
|
2018-05-27 05:19:01 +00:00
|
|
|
/// Attempt to render `index.html` when a directory is requested.
|
|
|
|
///
|
|
|
|
/// The default "batteries included" mechanisms for actix to handle directory
|
|
|
|
/// listings rely on redirection which behaves oddly (the location headers
|
|
|
|
/// seem to use relative paths for some reason).
|
|
|
|
/// They also mean that the address in the browser will include the
|
|
|
|
/// `index.html` on a successful redirect (rare), which is unsightly.
|
|
|
|
///
|
|
|
|
/// Rather than deal with all of that, we can hijack a hook for presenting a
|
|
|
|
/// custom directory listing response and serve it up using their
|
|
|
|
/// `NamedFile` responder.
|
2018-10-31 07:18:57 +00:00
|
|
|
fn handle_directory<'a, 'b>(
|
|
|
|
dir: &'a fs::Directory,
|
|
|
|
req: &'b HttpRequest,
|
|
|
|
) -> io::Result<HttpResponse> {
|
2018-05-27 05:19:01 +00:00
|
|
|
let mut path = PathBuf::from(&dir.base);
|
|
|
|
path.push(&dir.path);
|
|
|
|
path.push("index.html");
|
2018-05-31 18:39:43 +00:00
|
|
|
fs::NamedFile::open(path)?.respond_to(req)
|
2018-05-27 05:19:01 +00:00
|
|
|
}
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
pub fn serve(
|
|
|
|
interface: &str,
|
|
|
|
port: u16,
|
|
|
|
output_dir: &str,
|
|
|
|
base_url: &str,
|
|
|
|
config_file: &str,
|
2018-11-01 22:20:35 +00:00
|
|
|
watch_only: bool,
|
2018-10-31 07:18:57 +00:00
|
|
|
) -> Result<()> {
|
2018-01-12 10:50:29 +00:00
|
|
|
let start = Instant::now();
|
2018-02-02 16:18:07 +00:00
|
|
|
let (mut site, address) = create_new_site(interface, port, output_dir, base_url, config_file)?;
|
2017-05-12 14:10:21 +00:00
|
|
|
console::report_elapsed_time(start);
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2017-05-01 09:11:18 +00:00
|
|
|
// Setup watchers
|
2018-01-22 17:11:25 +00:00
|
|
|
let mut watching_static = false;
|
2018-10-11 18:52:41 +00:00
|
|
|
let mut watching_templates = false;
|
2017-05-01 09:11:18 +00:00
|
|
|
let (tx, rx) = channel();
|
|
|
|
let mut watcher = watcher(tx, Duration::from_secs(2)).unwrap();
|
2018-10-31 07:18:57 +00:00
|
|
|
watcher
|
|
|
|
.watch("content/", RecursiveMode::Recursive)
|
2017-05-03 08:52:49 +00:00
|
|
|
.chain_err(|| "Can't watch the `content` folder. Does it exist?")?;
|
2018-10-31 07:18:57 +00:00
|
|
|
watcher
|
|
|
|
.watch(config_file, RecursiveMode::Recursive)
|
2018-04-26 08:58:59 +00:00
|
|
|
.chain_err(|| "Can't watch the `config` file. Does it exist?")?;
|
2017-05-01 09:11:18 +00:00
|
|
|
|
2017-10-25 12:49:54 +00:00
|
|
|
if Path::new("static").exists() {
|
|
|
|
watching_static = true;
|
2018-10-31 07:18:57 +00:00
|
|
|
watcher
|
|
|
|
.watch("static/", RecursiveMode::Recursive)
|
2018-10-11 18:52:41 +00:00
|
|
|
.chain_err(|| "Can't watch the `static` folder.")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
if Path::new("templates").exists() {
|
|
|
|
watching_templates = true;
|
2018-10-31 07:18:57 +00:00
|
|
|
watcher
|
|
|
|
.watch("templates/", RecursiveMode::Recursive)
|
2018-10-11 18:52:41 +00:00
|
|
|
.chain_err(|| "Can't watch the `templates` folder.")?;
|
2017-10-25 12:49:54 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 13:19:15 +00:00
|
|
|
// Sass support is optional so don't make it an error to no have a sass folder
|
|
|
|
let _ = watcher.watch("sass/", RecursiveMode::Recursive);
|
|
|
|
|
2018-05-11 11:54:16 +00:00
|
|
|
let ws_address = format!("{}:{}", interface, site.live_reload.unwrap());
|
2018-05-27 05:19:01 +00:00
|
|
|
let output_path = Path::new(output_dir).to_path_buf();
|
2017-03-06 10:35:56 +00:00
|
|
|
|
2018-05-27 05:19:01 +00:00
|
|
|
// output path is going to need to be moved later on, so clone it for the
|
|
|
|
// http closure to avoid contention.
|
|
|
|
let static_root = output_path.clone();
|
2018-11-01 22:20:35 +00:00
|
|
|
let broadcaster = if !watch_only {
|
|
|
|
thread::spawn(move || {
|
|
|
|
let s = server::new(move || {
|
|
|
|
App::new()
|
|
|
|
.middleware(NotFoundHandler { rendered_template: static_root.join("404.html") })
|
|
|
|
.resource(r"/livereload.js", |r| r.f(livereload_handler))
|
|
|
|
// Start a webserver that serves the `output_dir` directory
|
|
|
|
.handler(
|
|
|
|
r"/",
|
|
|
|
fs::StaticFiles::new(&static_root)
|
|
|
|
.unwrap()
|
|
|
|
.show_files_listing()
|
|
|
|
.files_listing_renderer(handle_directory),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.bind(&address)
|
|
|
|
.expect("Can't start the webserver")
|
|
|
|
.shutdown_timeout(20);
|
2018-11-10 21:23:37 +00:00
|
|
|
println!("Web server is available at http://{}\n", &address);
|
2018-11-01 22:20:35 +00:00
|
|
|
s.run();
|
|
|
|
});
|
|
|
|
// The websocket for livereload
|
|
|
|
let ws_server = WebSocket::new(|output: Sender| {
|
|
|
|
move |msg: Message| {
|
|
|
|
if msg.into_text().unwrap().contains("\"hello\"") {
|
|
|
|
return output.send(Message::text(
|
|
|
|
r#"
|
|
|
|
{
|
|
|
|
"command": "hello",
|
|
|
|
"protocols": [ "http://livereload.com/protocols/official-7" ],
|
|
|
|
"serverName": "Zola"
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
Ok(())
|
2017-05-12 12:15:50 +00:00
|
|
|
}
|
2018-11-01 22:20:35 +00:00
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
let broadcaster = ws_server.broadcaster();
|
|
|
|
thread::spawn(move || {
|
|
|
|
ws_server.listen(&*ws_address).unwrap();
|
|
|
|
});
|
|
|
|
Some(broadcaster)
|
|
|
|
} else {
|
|
|
|
println!("Watching in watch only mode, no web server will be started");
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2018-08-04 19:47:45 +00:00
|
|
|
let pwd = env::current_dir().unwrap();
|
2017-03-25 06:52:51 +00:00
|
|
|
|
2018-10-11 18:52:41 +00:00
|
|
|
let mut watchers = vec!["content", "config.toml"];
|
2017-10-25 12:49:54 +00:00
|
|
|
if watching_static {
|
|
|
|
watchers.push("static");
|
|
|
|
}
|
2018-10-11 18:52:41 +00:00
|
|
|
if watching_templates {
|
|
|
|
watchers.push("templates");
|
|
|
|
}
|
2018-03-12 19:11:03 +00:00
|
|
|
if site.config.compile_sass {
|
2017-10-25 12:49:54 +00:00
|
|
|
watchers.push("sass");
|
2017-07-06 13:19:15 +00:00
|
|
|
}
|
2017-10-25 12:49:54 +00:00
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
println!(
|
|
|
|
"Listening for changes in {}{}{{{}}}",
|
|
|
|
pwd.display(),
|
|
|
|
MAIN_SEPARATOR,
|
|
|
|
watchers.join(", ")
|
|
|
|
);
|
2018-05-27 05:19:01 +00:00
|
|
|
|
2017-03-25 06:52:51 +00:00
|
|
|
println!("Press Ctrl+C to stop\n");
|
2018-01-22 17:11:25 +00:00
|
|
|
// Delete the output folder on ctrl+C
|
|
|
|
ctrlc::set_handler(move || {
|
|
|
|
remove_dir_all(&output_path).expect("Failed to delete output directory");
|
|
|
|
::std::process::exit(0);
|
2018-10-31 07:18:57 +00:00
|
|
|
})
|
|
|
|
.expect("Error setting Ctrl-C handler");
|
2017-03-06 10:35:56 +00:00
|
|
|
|
|
|
|
use notify::DebouncedEvent::*;
|
|
|
|
|
2018-11-10 21:23:37 +00:00
|
|
|
let reload_templates = |site: &mut Site, path: &Path| {
|
|
|
|
let msg = if path.is_dir() {
|
|
|
|
format!("-> Directory in `templates` folder changed {}", path.display())
|
|
|
|
} else {
|
|
|
|
format!("-> Template changed {}", path.display())
|
|
|
|
};
|
|
|
|
console::info(&msg);
|
|
|
|
if let Some(ref broadcaster) = broadcaster {
|
|
|
|
// Force refresh
|
|
|
|
rebuild_done_handling(
|
|
|
|
broadcaster,
|
|
|
|
rebuild::after_template_change(site, &path),
|
|
|
|
"/x.js",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let reload_sass = |site: &Site, path: &Path, partial_path: &Path| {
|
|
|
|
let msg = if path.is_dir() {
|
|
|
|
format!("-> Directory in `sass` folder changed {}", path.display())
|
|
|
|
} else {
|
|
|
|
format!("-> Sass file changed {}", path.display())
|
|
|
|
};
|
|
|
|
console::info(&msg);
|
|
|
|
if let Some(ref broadcaster) = broadcaster {
|
|
|
|
rebuild_done_handling(
|
|
|
|
&broadcaster,
|
|
|
|
site.compile_sass(&site.base_path),
|
|
|
|
&partial_path.to_string_lossy(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let copy_static = |site: &Site, path: &Path, partial_path: &Path| {
|
|
|
|
// Do nothing if the file/dir was deleted
|
|
|
|
if !path.exists() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let msg = if path.is_dir() {
|
|
|
|
format!("-> Directory in `static` folder changed {}", path.display())
|
|
|
|
} else {
|
|
|
|
format!("-> Static file changed {}", path.display())
|
|
|
|
};
|
|
|
|
|
|
|
|
console::info(&msg);
|
|
|
|
if let Some(ref broadcaster) = broadcaster {
|
|
|
|
if path.is_dir() {
|
|
|
|
rebuild_done_handling(
|
|
|
|
broadcaster,
|
|
|
|
site.copy_static_directories(),
|
|
|
|
&path.to_string_lossy(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
rebuild_done_handling(
|
|
|
|
broadcaster,
|
|
|
|
copy_file(&path, &site.output_path, &site.static_path),
|
|
|
|
&partial_path.to_string_lossy(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-06 10:35:56 +00:00
|
|
|
loop {
|
|
|
|
match rx.recv() {
|
2017-03-08 04:21:45 +00:00
|
|
|
Ok(event) => {
|
|
|
|
match event {
|
2018-11-10 21:23:37 +00:00
|
|
|
Rename(old_path, path) => {
|
|
|
|
if path.is_file() && is_temp_file(&path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let (change_kind, partial_path) = detect_change_kind(&pwd, &path);
|
|
|
|
|
|
|
|
// We only care about changes in non-empty folders
|
|
|
|
if path.is_dir() && is_folder_empty(&path) {
|
2017-03-08 04:21:45 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
println!(
|
|
|
|
"Change detected @ {}",
|
|
|
|
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
|
|
|
|
);
|
2018-11-10 21:23:37 +00:00
|
|
|
|
2017-03-10 11:39:58 +00:00
|
|
|
let start = Instant::now();
|
2018-11-10 21:23:37 +00:00
|
|
|
match change_kind {
|
|
|
|
ChangeKind::Content => {
|
|
|
|
console::info(&format!("-> Content renamed {}", path.display()));
|
2018-11-01 22:20:35 +00:00
|
|
|
if let Some(ref broadcaster) = broadcaster {
|
|
|
|
// Force refresh
|
|
|
|
rebuild_done_handling(
|
|
|
|
broadcaster,
|
2018-11-10 21:23:37 +00:00
|
|
|
rebuild::after_content_rename(&mut site, &old_path, &path),
|
2018-11-01 22:20:35 +00:00
|
|
|
"/x.js",
|
|
|
|
);
|
|
|
|
}
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2018-11-10 21:23:37 +00:00
|
|
|
ChangeKind::Templates => reload_templates(&mut site, &path),
|
|
|
|
ChangeKind::StaticFiles => copy_static(&site, &path, &partial_path),
|
|
|
|
ChangeKind::Sass => reload_sass(&site, &path, &partial_path),
|
|
|
|
ChangeKind::Config => {
|
|
|
|
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(
|
|
|
|
interface,
|
|
|
|
port,
|
|
|
|
output_dir,
|
|
|
|
base_url,
|
|
|
|
config_file,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console::report_elapsed_time(start);
|
|
|
|
|
|
|
|
}
|
|
|
|
Create(path) | Write(path) | Remove(path) => {
|
|
|
|
if is_temp_file(&path) || path.is_dir() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
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, _) => {
|
|
|
|
console::info(&format!("-> Content changed {}", path.display()));
|
2018-11-01 22:20:35 +00:00
|
|
|
if let Some(ref broadcaster) = broadcaster {
|
|
|
|
// Force refresh
|
|
|
|
rebuild_done_handling(
|
|
|
|
broadcaster,
|
2018-11-10 21:23:37 +00:00
|
|
|
rebuild::after_content_change(&mut site, &path),
|
2018-11-01 22:20:35 +00:00
|
|
|
"/x.js",
|
|
|
|
);
|
|
|
|
}
|
2018-10-31 07:18:57 +00:00
|
|
|
}
|
2018-11-10 21:23:37 +00:00
|
|
|
(ChangeKind::Templates, _) => reload_templates(&mut site, &path),
|
|
|
|
(ChangeKind::StaticFiles, p) => copy_static(&site, &path, &p),
|
|
|
|
(ChangeKind::Sass, p) => reload_sass(&site, &path, &p),
|
2018-01-12 10:50:29 +00:00
|
|
|
(ChangeKind::Config, _) => {
|
2018-09-30 19:15:09 +00:00
|
|
|
console::info("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.");
|
2018-10-31 07:18:57 +00:00
|
|
|
site = create_new_site(
|
|
|
|
interface,
|
|
|
|
port,
|
|
|
|
output_dir,
|
|
|
|
base_url,
|
|
|
|
config_file,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.0;
|
2018-01-12 10:50:29 +00:00
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
};
|
2017-05-12 14:10:21 +00:00
|
|
|
console::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
|
|
|
}
|
2018-10-31 07:18:57 +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
|
2018-09-13 14:57:38 +00:00
|
|
|
let name = filename.to_str().unwrap();
|
|
|
|
name.starts_with('#') || name.starts_with(".#")
|
2017-03-06 10:35:56 +00:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-10-31 07:18:57 +00:00
|
|
|
None => true,
|
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
|
2018-08-04 19:47:45 +00:00
|
|
|
fn detect_change_kind(pwd: &Path, path: &Path) -> (ChangeKind, PathBuf) {
|
|
|
|
let mut partial_path = PathBuf::from("/");
|
2018-08-05 05:59:56 +00:00
|
|
|
partial_path.push(path.strip_prefix(pwd).unwrap_or(path));
|
2018-06-20 15:43:24 +00:00
|
|
|
|
2018-08-04 19:47:45 +00:00
|
|
|
let change_kind = if partial_path.starts_with("/templates") {
|
2017-03-08 04:21:45 +00:00
|
|
|
ChangeKind::Templates
|
2018-08-04 19:47:45 +00:00
|
|
|
} else if partial_path.starts_with("/content") {
|
2017-03-08 04:21:45 +00:00
|
|
|
ChangeKind::Content
|
2018-08-04 19:47:45 +00:00
|
|
|
} else if partial_path.starts_with("/static") {
|
2017-03-08 04:21:45 +00:00
|
|
|
ChangeKind::StaticFiles
|
2018-08-04 19:47:45 +00:00
|
|
|
} else if partial_path.starts_with("/sass") {
|
2017-07-06 13:19:15 +00:00
|
|
|
ChangeKind::Sass
|
2018-08-04 19:47:45 +00:00
|
|
|
} else if partial_path == Path::new("/config.toml") {
|
2018-01-12 10:50:29 +00:00
|
|
|
ChangeKind::Config
|
2017-03-08 04:21:45 +00:00
|
|
|
} else {
|
2018-08-04 19:47:45 +00:00
|
|
|
unreachable!("Got a change in an unexpected path: {}", partial_path.display());
|
2017-03-08 04:21:45 +00:00
|
|
|
};
|
|
|
|
|
2018-08-04 19:47:45 +00:00
|
|
|
(change_kind, partial_path)
|
2017-03-08 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-10 21:23:37 +00:00
|
|
|
/// Check if the directory at path contains any file
|
|
|
|
fn is_folder_empty(dir: &Path) -> bool {
|
|
|
|
// Can panic if we don't have the rights I guess?
|
|
|
|
for _ in read_dir(dir).expect("Failed to read a directory to see if it was empty") {
|
|
|
|
// If we get there, that means we have a file
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-08 04:21:45 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-08-04 19:47:45 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-03-08 04:21:45 +00:00
|
|
|
|
2018-10-31 07:18:57 +00:00
|
|
|
use super::{detect_change_kind, is_temp_file, ChangeKind};
|
2017-03-08 04:21:45 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-05-20 15:00:41 +00:00
|
|
|
fn can_recognize_temp_files() {
|
|
|
|
let test_cases = vec![
|
2017-03-08 04:21:45 +00:00
|
|
|
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"),
|
|
|
|
];
|
|
|
|
|
2017-05-20 15:00:41 +00:00
|
|
|
for t in test_cases {
|
2017-03-08 04:21:45 +00:00
|
|
|
assert!(is_temp_file(&t));
|
|
|
|
}
|
2017-03-06 10:35:56 +00:00
|
|
|
}
|
2017-03-08 04:21:45 +00:00
|
|
|
|
|
|
|
#[test]
|
2017-05-20 15:00:41 +00:00
|
|
|
fn can_detect_kind_of_changes() {
|
|
|
|
let test_cases = vec![
|
2017-03-09 07:34:12 +00:00
|
|
|
(
|
2018-08-04 19:47:45 +00:00
|
|
|
(ChangeKind::Templates, PathBuf::from("/templates/hello.html")),
|
2018-10-31 07:18:57 +00:00
|
|
|
Path::new("/home/vincent/site"),
|
|
|
|
Path::new("/home/vincent/site/templates/hello.html"),
|
2017-03-09 07:34:12 +00:00
|
|
|
),
|
|
|
|
(
|
2018-08-04 19:47:45 +00:00
|
|
|
(ChangeKind::StaticFiles, PathBuf::from("/static/site.css")),
|
2018-10-31 07:18:57 +00:00
|
|
|
Path::new("/home/vincent/site"),
|
|
|
|
Path::new("/home/vincent/site/static/site.css"),
|
2017-03-09 07:34:12 +00:00
|
|
|
),
|
|
|
|
(
|
2018-08-04 19:47:45 +00:00
|
|
|
(ChangeKind::Content, PathBuf::from("/content/posts/hello.md")),
|
2018-10-31 07:18:57 +00:00
|
|
|
Path::new("/home/vincent/site"),
|
|
|
|
Path::new("/home/vincent/site/content/posts/hello.md"),
|
2017-03-09 07:34:12 +00:00
|
|
|
),
|
2017-11-20 23:05:37 +00:00
|
|
|
(
|
2018-08-04 19:47:45 +00:00
|
|
|
(ChangeKind::Sass, PathBuf::from("/sass/print.scss")),
|
2018-10-31 07:18:57 +00:00
|
|
|
Path::new("/home/vincent/site"),
|
|
|
|
Path::new("/home/vincent/site/sass/print.scss"),
|
2018-01-12 10:50:29 +00:00
|
|
|
),
|
|
|
|
(
|
2018-08-04 19:47:45 +00:00
|
|
|
(ChangeKind::Config, PathBuf::from("/config.toml")),
|
2018-10-31 07:18:57 +00:00
|
|
|
Path::new("/home/vincent/site"),
|
|
|
|
Path::new("/home/vincent/site/config.toml"),
|
2018-01-12 10:50:29 +00:00
|
|
|
),
|
2017-03-08 04:21:45 +00:00
|
|
|
];
|
|
|
|
|
2017-05-20 15:00:41 +00:00
|
|
|
for (expected, pwd, path) in test_cases {
|
2017-03-08 04:21:45 +00:00
|
|
|
assert_eq!(expected, detect_change_kind(&pwd, &path));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-04 19:47:45 +00:00
|
|
|
#[test]
|
2018-08-04 20:28:39 +00:00
|
|
|
#[cfg(windows)]
|
2018-08-04 19:47:45 +00:00
|
|
|
fn windows_path_handling() {
|
|
|
|
let expected = (ChangeKind::Templates, PathBuf::from("/templates/hello.html"));
|
|
|
|
let pwd = Path::new(r#"C:\\Users\johan\site"#);
|
|
|
|
let path = Path::new(r#"C:\\Users\johan\site\templates\hello.html"#);
|
|
|
|
assert_eq!(expected, detect_change_kind(pwd, path));
|
|
|
|
}
|
|
|
|
|
2018-08-05 05:59:56 +00:00
|
|
|
#[test]
|
|
|
|
fn relative_path() {
|
|
|
|
let expected = (ChangeKind::Templates, PathBuf::from("/templates/hello.html"));
|
|
|
|
let pwd = Path::new("/home/johan/site");
|
|
|
|
let path = Path::new("templates/hello.html");
|
|
|
|
assert_eq!(expected, detect_change_kind(pwd, path));
|
|
|
|
}
|
2017-03-03 08:12:40 +00:00
|
|
|
}
|