Init now creates 'src' and 'book' folder. In 'src' SUMMARY.md is created with one dummy chapter

This commit is contained in:
Mathieu David 2015-07-07 11:46:49 +02:00
parent a27bc498eb
commit a6b3f99c53
2 changed files with 30 additions and 15 deletions

View File

@ -115,7 +115,9 @@ fn init(args: Vec<String>) {
let dir = std::env::current_dir().unwrap(); let dir = std::env::current_dir().unwrap();
let book = MDBook::new(); let book = MDBook::new();
book.init(&dir); if let Err(e) = book.init(&dir) {
println!("Error: {}", e);
}
} }

View File

@ -1,5 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::{self, File, metadata}; use std::fs::{self, File, metadata};
use std::io::Write;
pub struct MDBook { pub struct MDBook {
dest: PathBuf, dest: PathBuf,
@ -37,31 +38,43 @@ impl MDBook {
// Hacky way to check if the directory exists... Until PathExt moves to stable // Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&dest) { match metadata(&dest) {
Err(_) => { Err(_) => {
// There is a very high chance that the error is due to the fact that // There is a very high chance that the error is due to the fact that
// the directory / file does not exist // the directory / file does not exist
fs::create_dir(&dest).unwrap(); fs::create_dir(&dest).unwrap();
}, },
Ok(_) => { /* If there is no error, the directory / file does exist */ } Ok(_) => { /* If there is no error, the directory / file does exist */ }
} }
// Hacky way to check if the directory exists... Until PathExt moves to stable // Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(&src) { match metadata(&src) {
Err(_) => { Err(_) => {
// There is a very high chance that the error is due to the fact that // There is a very high chance that the error is due to the fact that
// the directory / file does not exist // the directory / file does not exist
fs::create_dir(&src).unwrap(); fs::create_dir(&src).unwrap();
}, },
Ok(_) => { /* If there is no error, the directory / file does exist */ } Ok(_) => { /* If there is no error, the directory / file does exist */ }
} }
// Hacky way to check if the directory exists... Until PathExt moves to stable // Hacky way to check if the directory exists... Until PathExt moves to stable
match metadata(dir.join("src/SUMMARY.md")) { let summary = match metadata(&src.join("SUMMARY.md")) {
Err(_) => { Err(_) => {
// There is a very high chance that the error is due to the fact that // There is a very high chance that the error is due to the fact that
// the directory / file does not exist // the directory / file does not exist
File::create("src/SUMMARY.md").unwrap(); Result::Ok(File::create(&src.join("SUMMARY.md")).unwrap())
}, },
Ok(_) => { /* If there is no error, the directory / file does exist */ } Ok(_) => {
/* If there is no error, the directory / file does exist */
Result::Err("SUMMARY.md does already exist")
}
};
if let Ok(mut f) = summary {
writeln!(f, "# Summary");
writeln!(f, "");
writeln!(f, "[Chapter 1](./chapter_1.md)");
let mut chapter_1 = File::create(&src.join("chapter_1.md")).unwrap();
writeln!(chapter_1, "# Chapter 1");
} }
return Ok(()); return Ok(());