Fix path handling on windows.

Delay turning paths into string. Instead, use methods on Path/PathBuf
to manipulate paths. This way the Rust Path implementation gets to deal with
all werid cross-platform issues.

This should fix #359
This commit is contained in:
Johan Sigfrids 2018-08-04 22:47:45 +03:00
parent f0ba604075
commit 64ec7e1578

View file

@ -219,7 +219,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf
ws_server.listen(&*ws_address).unwrap(); ws_server.listen(&*ws_address).unwrap();
}); });
let pwd = format!("{}", env::current_dir().unwrap().display()); let pwd = env::current_dir().unwrap();
let mut watchers = vec!["content", "templates", "config.toml"]; let mut watchers = vec!["content", "templates", "config.toml"];
if watching_static { if watching_static {
@ -229,7 +229,7 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf
watchers.push("sass"); watchers.push("sass");
} }
println!("Listening for changes in {}/{{{}}}", pwd, watchers.join(", ")); println!("Listening for changes in {}/{{{}}}", pwd.display(), watchers.join(", "));
println!("Press Ctrl+C to stop\n"); println!("Press Ctrl+C to stop\n");
// Delete the output folder on ctrl+C // Delete the output folder on ctrl+C
@ -268,12 +268,12 @@ pub fn serve(interface: &str, port: &str, output_dir: &str, base_url: &str, conf
(ChangeKind::StaticFiles, p) => { (ChangeKind::StaticFiles, p) => {
if path.is_file() { if path.is_file() {
console::info(&format!("-> Static file changes detected {}", path.display())); console::info(&format!("-> Static file changes detected {}", path.display()));
rebuild_done_handling(&broadcaster, copy_file(&path, &site.output_path, &site.static_path), &p); rebuild_done_handling(&broadcaster, copy_file(&path, &site.output_path, &site.static_path), &p.to_string_lossy());
} }
}, },
(ChangeKind::Sass, p) => { (ChangeKind::Sass, p) => {
console::info(&format!("-> Sass file changed {}", path.display())); console::info(&format!("-> Sass file changed {}", path.display()));
rebuild_done_handling(&broadcaster, site.compile_sass(&site.base_path), &p); rebuild_done_handling(&broadcaster, site.compile_sass(&site.base_path), &p.to_string_lossy());
}, },
(ChangeKind::Config, _) => { (ChangeKind::Config, _) => {
console::info(&format!("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible.")); console::info(&format!("-> Config changed. The whole site will be reloaded. The browser needs to be refreshed to make the changes visible."));
@ -320,31 +320,30 @@ fn is_temp_file(path: &Path) -> bool {
/// Detect what changed from the given path so we have an idea what needs /// Detect what changed from the given path so we have an idea what needs
/// to be reloaded /// to be reloaded
fn detect_change_kind(pwd: &str, path: &Path) -> (ChangeKind, String) { fn detect_change_kind(pwd: &Path, path: &Path) -> (ChangeKind, PathBuf) {
let path_str = format!("{}", path.display()) let mut partial_path = PathBuf::from("/");
.replace(pwd, "") partial_path.push(path.strip_prefix(pwd).unwrap());
.replace("\\", "");
let change_kind = if path_str.starts_with("/templates") { let change_kind = if partial_path.starts_with("/templates") {
ChangeKind::Templates ChangeKind::Templates
} else if path_str.starts_with("/content") { } else if partial_path.starts_with("/content") {
ChangeKind::Content ChangeKind::Content
} else if path_str.starts_with("/static") { } else if partial_path.starts_with("/static") {
ChangeKind::StaticFiles ChangeKind::StaticFiles
} else if path_str.starts_with("/sass") { } else if partial_path.starts_with("/sass") {
ChangeKind::Sass ChangeKind::Sass
} else if path_str == "/config.toml" { } else if partial_path == Path::new("/config.toml") {
ChangeKind::Config ChangeKind::Config
} else { } else {
unreachable!("Got a change in an unexpected path: {}", path_str) unreachable!("Got a change in an unexpected path: {}", partial_path.display());
}; };
(change_kind, path_str) (change_kind, partial_path)
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::path::Path; use std::path::{Path, PathBuf};
use super::{is_temp_file, detect_change_kind, ChangeKind}; use super::{is_temp_file, detect_change_kind, ChangeKind};
@ -371,24 +370,24 @@ mod tests {
fn can_detect_kind_of_changes() { fn can_detect_kind_of_changes() {
let test_cases = vec![ let test_cases = vec![
( (
(ChangeKind::Templates, "/templates/hello.html".to_string()), (ChangeKind::Templates, PathBuf::from("/templates/hello.html")),
"/home/vincent/site", Path::new("/home/vincent/site/templates/hello.html") Path::new("/home/vincent/site"), Path::new("/home/vincent/site/templates/hello.html")
), ),
( (
(ChangeKind::StaticFiles, "/static/site.css".to_string()), (ChangeKind::StaticFiles, PathBuf::from("/static/site.css")),
"/home/vincent/site", Path::new("/home/vincent/site/static/site.css") Path::new("/home/vincent/site"), Path::new("/home/vincent/site/static/site.css")
), ),
( (
(ChangeKind::Content, "/content/posts/hello.md".to_string()), (ChangeKind::Content, PathBuf::from("/content/posts/hello.md")),
"/home/vincent/site", Path::new("/home/vincent/site/content/posts/hello.md") Path::new("/home/vincent/site"), Path::new("/home/vincent/site/content/posts/hello.md")
), ),
( (
(ChangeKind::Sass, "/sass/print.scss".to_string()), (ChangeKind::Sass, PathBuf::from("/sass/print.scss")),
"/home/vincent/site", Path::new("/home/vincent/site/sass/print.scss") Path::new("/home/vincent/site"), Path::new("/home/vincent/site/sass/print.scss")
), ),
( (
(ChangeKind::Config, "/config.toml".to_string()), (ChangeKind::Config, PathBuf::from("/config.toml")),
"/home/vincent/site", Path::new("/home/vincent/site/config.toml") Path::new("/home/vincent/site"), Path::new("/home/vincent/site/config.toml")
), ),
]; ];
@ -398,4 +397,12 @@ mod tests {
} }
#[test]
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));
}
} }