From 47eb4788cb7294b7617c2972832d82c2b85232e8 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Sat, 18 Nov 2017 20:41:04 +0800 Subject: [PATCH] Introduced the `BookBuilder`. - You now use a `BookBuilder` for creating a book directory tree - This also removes the `--no-create` argument --- src/bin/build.rs | 21 ++-- src/bin/init.rs | 33 ++---- src/bin/serve.rs | 9 +- src/bin/test.rs | 2 +- src/bin/watch.rs | 2 +- src/book/init.rs | 156 ++++++++++++++++++++++++++++ src/book/mod.rs | 214 ++++----------------------------------- tests/init.rs | 29 +++--- tests/rendered_output.rs | 16 +-- tests/testing.rs | 4 +- 10 files changed, 225 insertions(+), 261 deletions(-) create mode 100644 src/book/init.rs diff --git a/src/bin/build.rs b/src/bin/build.rs index 54dffbd1..bc784ea9 100644 --- a/src/bin/build.rs +++ b/src/bin/build.rs @@ -1,5 +1,5 @@ use std::path::PathBuf; -use clap::{ArgMatches, SubCommand, App}; +use clap::{App, ArgMatches, SubCommand}; use mdbook::MDBook; use mdbook::errors::Result; use {get_book_dir, open}; @@ -10,32 +10,23 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { .about("Build the book from the markdown files") .arg_from_usage("-o, --open 'Open the compiled book in a web browser'") .arg_from_usage( - "-d, --dest-dir=[dest-dir] 'The output directory for your \ - book{n}(Defaults to ./book when omitted)'", - ) - .arg_from_usage( - "--no-create 'Will not create non-existent files linked from SUMMARY.md (deprecated: use book.toml instead)'", - ) - .arg_from_usage( - "[dir] 'A directory for your book{n}(Defaults to Current Directory \ + "-d, --dest-dir=[dest-dir] 'The output directory for your book{n}(Defaults to ./book \ when omitted)'", ) + .arg_from_usage( + "[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'", + ) } // Build command implementation pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir).read_config()?; + let mut book = MDBook::load(&book_dir)?; if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = PathBuf::from(dest_dir); } - // This flag is deprecated in favor of being set via `book.toml`. - if args.is_present("no-create") { - book.config.build.create_missing = false; - } - book.build()?; if args.is_present("open") { diff --git a/src/bin/init.rs b/src/bin/init.rs index 83ac54be..cbc08291 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -3,6 +3,7 @@ use std::io::Write; use clap::{App, ArgMatches, SubCommand}; use mdbook::MDBook; use mdbook::errors::Result; +use mdbook::config::Config; use get_book_dir; // Create clap subcommand arguments @@ -19,45 +20,31 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { // Init command implementation pub fn execute(args: &ArgMatches) -> Result<()> { let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir); - - // Call the function that does the initialization - book.init()?; + let mut builder = MDBook::init(&book_dir); // If flag `--theme` is present, copy theme to src if args.is_present("theme") { // Skip this if `--force` is present if !args.is_present("force") { // Print warning - print!("\nCopying the default theme to {:?}", book.get_source()); - println!("could potentially overwrite files already present in that directory."); + print!("\nCopying the default theme to {}", builder.config().book.src.display()); + println!("This could potentially overwrite files already present in that directory."); print!("\nAre you sure you want to continue? (y/n) "); // Read answer from user and exit if it's not 'yes' - if !confirm() { - println!("\nSkipping...\n"); - println!("All done, no errors..."); - ::std::process::exit(0); + if confirm() { + builder.copy_theme(true); } } - - // Call the function that copies the theme - book.copy_theme()?; - println!("\nTheme copied."); } - // Because of `src/book/mdbook.rs#L37-L39`, `dest` will always start with `root` - let is_dest_inside_root = book.get_destination().starts_with(&book.root); + println!("\nDo you want a .gitignore to be created? (y/n)"); - if !args.is_present("force") && is_dest_inside_root { - println!("\nDo you want a .gitignore to be created? (y/n)"); - - if confirm() { - book.create_gitignore(); - println!("\n.gitignore created."); - } + if confirm() { + builder.create_gitignore(true); } + builder.build()?; println!("\nAll done, no errors..."); Ok(()) diff --git a/src/bin/serve.rs b/src/bin/serve.rs index ac1ba144..2768e466 100644 --- a/src/bin/serve.rs +++ b/src/bin/serve.rs @@ -49,7 +49,7 @@ pub fn execute(args: &ArgMatches) -> Result<()> { const RELOAD_COMMAND: &'static str = "reload"; let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir).read_config()?; + let mut book = MDBook::load(&book_dir)?; if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = PathBuf::from(dest_dir); @@ -64,7 +64,8 @@ pub fn execute(args: &ArgMatches) -> Result<()> { let address = format!("{}:{}", interface, port); let ws_address = format!("{}:{}", interface, ws_port); - book.livereload = Some(format!(r#" + book.livereload = Some(format!( + r#"