Added a utility method to remove all content of a dir but not the dir itelf. Fixes #32

This commit is contained in:
Mathieu David 2015-08-16 13:20:10 +02:00
parent a5aa357f57
commit 262aa91948
2 changed files with 16 additions and 1 deletions

View File

@ -3,7 +3,7 @@ use std::fs::{self, File};
use std::io::Write;
use std::error::Error;
use {BookConfig, BookItem, theme, parse};
use {BookConfig, BookItem, theme, parse, utils};
use book::BookItems;
use renderer::{Renderer, HtmlHandlebars};
use utils::{PathExt, create_path};
@ -142,6 +142,9 @@ impl MDBook {
try!(self.parse_summary());
// Clean output directory
try!(utils::remove_dir_content(&self.config.get_dest()));
try!(self.renderer.render(
self.iter(),
&self.config,

View File

@ -118,3 +118,15 @@ pub fn create_file(path: &Path) -> Result<File, Box<Error>> {
Ok(f)
}
/// Removes all the content of a directory but not the directory itself
pub fn remove_dir_content(dir: &Path) -> Result<(), Box<Error>> {
for item in try!(fs::read_dir(dir)) {
if let Ok(item) = item {
let item = item.path();
if item.is_dir() { try!(fs::remove_dir_all(item)); } else { try!(fs::remove_file(item)); }
}
}
Ok(())
}