From f3c8535870d135f5c1bca5f684d0c6f887ed80f4 Mon Sep 17 00:00:00 2001 From: Michal Budzynski Date: Mon, 26 Jun 2017 01:24:33 +0200 Subject: [PATCH] Extracted `mdbook test` and `mdbook init` to separate files/modules --- src/bin/init.rs | 15 +++++++++++++- src/bin/mdbook.rs | 50 ++++++++++++----------------------------------- src/bin/test.rs | 20 +++++++++++++++++++ 3 files changed, 46 insertions(+), 39 deletions(-) create mode 100644 src/bin/test.rs diff --git a/src/bin/init.rs b/src/bin/init.rs index a9bd6a4b..f0f51861 100644 --- a/src/bin/init.rs +++ b/src/bin/init.rs @@ -1,9 +1,11 @@ +use std::io; +use std::io::Write; use std::error::Error; use clap::{ArgMatches, SubCommand, App}; use mdbook::MDBook; -use {get_book_dir, confirm}; +use get_book_dir; // Init command implementation pub fn init(args: &ArgMatches) -> Result<(), Box> { @@ -57,6 +59,17 @@ pub fn init(args: &ArgMatches) -> Result<(), Box> { Ok(()) } +// Simple function that user comfirmation +fn confirm() -> bool { + io::stdout().flush().unwrap(); + let mut s = String::new(); + io::stdin().read_line(&mut s).ok(); + match &*s.trim() { + "Y" | "y" | "yes" | "Yes" => true, + _ => false, + } +} + pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { SubCommand::with_name("init") .about("Create boilerplate structure and files in the directory") diff --git a/src/bin/mdbook.rs b/src/bin/mdbook.rs index 097ac0aa..961007be 100644 --- a/src/bin/mdbook.rs +++ b/src/bin/mdbook.rs @@ -6,22 +6,20 @@ extern crate env_logger; extern crate open; use std::env; -use std::error::Error; use std::ffi::OsStr; use std::io::{self, Write}; use std::path::{Path, PathBuf}; -use clap::{App, ArgMatches, SubCommand, AppSettings}; +use clap::{App, ArgMatches, AppSettings}; pub mod build; pub mod init; +pub mod test; #[cfg(feature = "serve")] pub mod serve; #[cfg(feature = "watch")] pub mod watch; -use mdbook::MDBook; - const NAME: &'static str = "mdbook"; fn main() { @@ -29,16 +27,15 @@ fn main() { // Create a list of valid arguments and sub-commands let app = App::new(NAME) - .about("Create a book in form of a static website from markdown files") - .author("Mathieu David ") - // Get the version from our Cargo.toml using clap's crate_version!() macro - .version(concat!("v",crate_version!())) - .setting(AppSettings::SubcommandRequired) - .after_help("For more information about a specific command, try `mdbook --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook") - .subcommand(init::make_subcommand()) - .subcommand(build::make_subcommand()) - .subcommand(SubCommand::with_name("test") - .about("Test that code samples compile")); + .about("Create a book in form of a static website from markdown files") + .author("Mathieu David ") + // Get the version from our Cargo.toml using clap's crate_version!() macro + .version(concat!("v",crate_version!())) + .setting(AppSettings::SubcommandRequired) + .after_help("For more information about a specific command, try `mdbook --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook") + .subcommand(init::make_subcommand()) + .subcommand(build::make_subcommand()) + .subcommand(test::make_subcommand()); #[cfg(feature = "watch")] let app = app.subcommand(watch::make_subcommand()); @@ -53,7 +50,7 @@ fn main() { ("watch", Some(sub_matches)) => watch::watch(sub_matches), #[cfg(feature = "serve")] ("serve", Some(sub_matches)) => serve::serve(sub_matches), - ("test", Some(sub_matches)) => test(sub_matches), + ("test", Some(sub_matches)) => test::test(sub_matches), (_, _) => unreachable!(), }; @@ -63,29 +60,6 @@ fn main() { } } - -// Simple function that user comfirmation -fn confirm() -> bool { - io::stdout().flush().unwrap(); - let mut s = String::new(); - io::stdin().read_line(&mut s).ok(); - match &*s.trim() { - "Y" | "y" | "yes" | "Yes" => true, - _ => false, - } -} - - -fn test(args: &ArgMatches) -> Result<(), Box> { - let book_dir = get_book_dir(args); - let mut book = MDBook::new(&book_dir).read_config()?; - - book.test()?; - - Ok(()) -} - - fn get_book_dir(args: &ArgMatches) -> PathBuf { if let Some(dir) = args.value_of("dir") { // Check if path is relative from current dir, or absolute... diff --git a/src/bin/test.rs b/src/bin/test.rs new file mode 100644 index 00000000..9517ffa5 --- /dev/null +++ b/src/bin/test.rs @@ -0,0 +1,20 @@ +use std::error::Error; + +use clap::{ArgMatches, SubCommand, App}; +use mdbook::MDBook; + +use get_book_dir; + +// test command implementation +pub fn test(args: &ArgMatches) -> Result<(), Box> { + let book_dir = get_book_dir(args); + let mut book = MDBook::new(&book_dir).read_config()?; + + book.test()?; + + Ok(()) +} + +pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { + SubCommand::with_name("test").about("Test that code samples compile") +}