Extracted `mdbook init` to separate file/module

This commit is contained in:
Michal Budzynski 2017-06-26 01:02:32 +02:00
parent 4c78fdf431
commit efdd0330c1
2 changed files with 70 additions and 59 deletions

67
src/bin/init.rs Normal file
View File

@ -0,0 +1,67 @@
use std::error::Error;
use clap::{ArgMatches, SubCommand, App};
use mdbook::MDBook;
use {get_book_dir, confirm};
// Init command implementation
pub fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir);
// Call the function that does the initialization
book.init()?;
// 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!("\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);
}
}
// 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()
.map(|p| p.starts_with(book.get_root()))
.unwrap_or(false);
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.");
}
}
println!("\nAll done, no errors...");
Ok(())
}
pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> {
SubCommand::with_name("init")
.about("Create boilerplate structure and files in the directory")
// the {n} denotes a newline which will properly aligned in all help messages
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'")
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
.arg_from_usage("--force 'skip confirmation prompts'")
}

View File

@ -14,6 +14,7 @@ use std::path::{Path, PathBuf};
use clap::{App, ArgMatches, SubCommand, AppSettings};
pub mod build;
pub mod init;
#[cfg(feature = "serve")]
pub mod serve;
#[cfg(feature = "watch")]
@ -34,12 +35,7 @@ fn main() {
.version(concat!("v",crate_version!()))
.setting(AppSettings::SubcommandRequired)
.after_help("For more information about a specific command, try `mdbook <command> --help`\nSource code for mdbook available at: https://github.com/azerupi/mdBook")
.subcommand(SubCommand::with_name("init")
.about("Create boilerplate structure and files in the directory")
// the {n} denotes a newline which will properly aligned in all help messages
.arg_from_usage("[dir] 'A directory for your book{n}(Defaults to Current Directory when omitted)'")
.arg_from_usage("--theme 'Copies the default theme into your source folder'")
.arg_from_usage("--force 'skip confirmation prompts'"))
.subcommand(init::make_subcommand())
.subcommand(build::make_subcommand())
.subcommand(SubCommand::with_name("test")
.about("Test that code samples compile"));
@ -51,7 +47,7 @@ fn main() {
// Check which subcomamnd the user ran...
let res = match app.get_matches().subcommand() {
("init", Some(sub_matches)) => init(sub_matches),
("init", Some(sub_matches)) => init::init(sub_matches),
("build", Some(sub_matches)) => build::build(sub_matches),
#[cfg(feature = "watch")]
("watch", Some(sub_matches)) => watch::watch(sub_matches),
@ -80,58 +76,6 @@ fn confirm() -> bool {
}
// Init command implementation
fn init(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir);
// Call the function that does the initialization
book.init()?;
// 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!("\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);
}
}
// 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()
.map(|p| p.starts_with(book.get_root()))
.unwrap_or(false);
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.");
}
}
println!("\nAll done, no errors...");
Ok(())
}
fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
let book_dir = get_book_dir(args);
let mut book = MDBook::new(&book_dir).read_config()?;