diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 77b93cdc..7b8e0cde 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -78,7 +78,8 @@ fn init(args: &ArgMatches) -> Result<(), Box> { // Read answer from user and exit if it's not 'yes' if !confirm() { - println!("\nexiting...\n"); + println!("\nSkipping...\n"); + println!("All done, no errors..."); ::std::process::exit(0); } } @@ -87,9 +88,10 @@ fn init(args: &ArgMatches) -> Result<(), Box> { try!(book.copy_theme()); println!("\nTheme copied."); - println!(""); } + println!("\nAll done, no errors..."); + Ok(()) } diff --git a/src/book/mdbook.rs b/src/book/mdbook.rs index 06d273f3..618c211e 100644 --- a/src/book/mdbook.rs +++ b/src/book/mdbook.rs @@ -1,14 +1,12 @@ use std::path::Path; -use std::fs::{self, File, metadata}; +use std::fs::{self, File}; use std::io::Write; use std::error::Error; -use {BookConfig, BookItem}; +use {BookConfig, BookItem, theme, parse}; use book::BookItems; -use parse; -use theme; -use renderer::Renderer; -use renderer::HtmlHandlebars; +use renderer::{Renderer, HtmlHandlebars}; +use utils::{PathExt, create_path}; pub struct MDBook { config: BookConfig, @@ -27,14 +25,8 @@ impl MDBook { pub fn new(root: &Path) -> MDBook { - // Hacky way to check if the path exists... Until PathExt moves to stable - match metadata(root) { - Err(_) => panic!("Directory does not exist"), - Ok(f) => { - if !f.is_dir() { - panic!("Is not a directory"); - } - } + if !root.exists() || !root.is_dir() { + output!("{:?} No directory with that name", root); } MDBook { @@ -95,46 +87,33 @@ impl MDBook { debug!("[fn]: init"); + if !self.config.get_root().exists() { + create_path(self.config.get_root()).unwrap(); + output!("{:?} created", self.config.get_root()); + } + let dest = self.config.get_dest(); let src = self.config.get_src(); - // Hacky way to check if the directory exists... Until PathExt moves to stable - match metadata(&dest) { - Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - debug!("[*]: {:?} does not exist, trying to create directory", dest); - try!(fs::create_dir(&dest)); - }, - Ok(_) => { /* If there is no error, the directory / file does exist */ } + if !dest.exists() { + debug!("[*]: {:?} does not exist, trying to create directory", dest); + try!(fs::create_dir(&dest)); } - // Hacky way to check if the directory exists... Until PathExt moves to stable - match metadata(&src) { - Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - debug!("[*]: {:?} does not exist, trying to create directory", src); - try!(fs::create_dir(&src)); - }, - Ok(_) => { /* If there is no error, the directory / file does exist */ } + if !src.exists() { + debug!("[*]: {:?} does not exist, trying to create directory", src); + try!(fs::create_dir(&src)); } - // Hacky way to check if the directory exists... Until PathExt moves to stable - let summary = match metadata(&src.join("SUMMARY.md")) { - Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md")); - Ok(try!(File::create(&src.join("SUMMARY.md")))) - }, - Ok(_) => { - /* If there is no error, the directory / file does exist */ - Err("SUMMARY.md does already exist") - } - }; + let summary = src.join("SUMMARY.md"); + + if !summary.exists() { + + // Summary does not exist, create it and populate it + + debug!("[*]: {:?} does not exist, trying to create SUMMARY.md", src.join("SUMMARY.md")); + let mut f = try!(File::create(&src.join("SUMMARY.md"))); - if let Ok(mut f) = summary { debug!("[*]: Writing to SUMMARY.md"); try!(writeln!(f, "# Summary")); @@ -143,6 +122,10 @@ impl MDBook { let mut chapter_1 = try!(File::create(&src.join("chapter_1.md"))); try!(writeln!(chapter_1, "# Chapter 1")); + } else { + + // Summary does exist, read it and create the missing files + } return Ok(()); @@ -173,15 +156,9 @@ impl MDBook { let theme_dir = self.config.get_src().join("theme"); - // Hacky way to check if the directory exists... Until PathExt moves to stable - match metadata(&theme_dir) { - Err(_) => { - // There is a very high chance that the error is due to the fact that - // the directory / file does not exist - debug!("[*]: {:?} does not exist, trying to create directory", theme_dir); - try!(fs::create_dir(&theme_dir)); - }, - Ok(_) => { /* If there is no error, the directory / file does exist */ } + if !theme_dir.exists() { + debug!("[*]: {:?} does not exist, trying to create directory", theme_dir); + try!(fs::create_dir(&theme_dir)); } // index.hbs diff --git a/src/theme/mod.rs b/src/theme/mod.rs index bd6f1795..3d7f26ca 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -1,7 +1,9 @@ use std::path::Path; -use std::fs::{File, metadata}; +use std::fs::File; use std::io::Read; +use utils::{PathExt}; + pub static INDEX: &'static [u8] = include_bytes!("index.hbs"); pub static CSS: &'static [u8] = include_bytes!("book.css"); pub static JS: &'static [u8] = include_bytes!("book.js"); @@ -35,26 +37,14 @@ impl Theme { }; // Check if the given path exists - // Hacky way to check if the path exists... Until PathExt moves to stable - match metadata(&src) { - Err(_) => return theme, - Ok(f) => { - if !f.is_dir() { - return theme; - } - }, + if !src.exists() || !src.is_dir() { + return theme } let src = src.join("theme"); // If src does exist, check if there is a theme directory in it - // Hacky way to check if the path exists... Until PathExt moves to stable - match metadata(&src) { - Err(_) => return theme, - Ok(f) => { - if !f.is_dir() { - return theme; - } - } + if !src.exists() || !src.is_dir() { + return theme } // Check for individual files if they exist diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 3551de29..a4d64a8a 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -87,23 +87,14 @@ pub fn create_path(path: &Path) -> Result<(), Box> { constructed_path.push(&dir); debug!("[*]: {:?}", constructed_path); - // Check if path exists - match metadata(&constructed_path) { - // Any way to combine the Err and first Ok branch ?? - Err(_) => { - try!(fs::create_dir(&constructed_path)); - debug!("[*]: Directory created {:?}", constructed_path); - }, - Ok(f) => { - if !f.is_dir() { - try!(fs::create_dir(&constructed_path)); - debug!("[*]: Directory created {:?}", constructed_path); - } else { - debug!("[*]: Directory exists {:?}", constructed_path); - continue - } - }, + if !constructed_path.exists() || !constructed_path.is_dir() { + try!(fs::create_dir(&constructed_path)); + debug!("[*]: Directory created {:?}", constructed_path); + } else { + debug!("[*]: Directory exists {:?}", constructed_path); + continue } + } debug!("[*]: Constructed path: {:?}", constructed_path); @@ -122,7 +113,7 @@ pub fn create_file(path: &Path) -> Result> { try!(create_path(p)); } - debug!("[*]: Create file: {}", path); + debug!("[*]: Create file: {:?}", path); let f = try!(File::create(path)); Ok(f)