2 use clap::{crate_version};
5 use std::path::{Path, PathBuf};
7 use clap::{App, ArgMatches, SubCommand, AppSettings};
9 use mdbook_1::{MDBook as MDBook1};
10 use mdbook_1::errors::{Result as Result1};
12 use mdbook_2::{MDBook as MDBook2};
13 use mdbook_2::errors::{Result as Result2};
16 let d_message = "-d, --dest-dir=[dest-dir]
17 'The output directory for your book{n}(Defaults to ./book when omitted)'";
18 let dir_message = "[dir]
19 'A directory for your book{n}(Defaults to Current Directory when omitted)'";
20 let vers_message = "-m, --mdbook-vers=[md-version]
21 'The version of mdbook to use for your book{n}(Defaults to 1 when omitted)'";
23 let matches = App::new("rustbook")
24 .about("Build a book with mdBook")
25 .author("Steve Klabnik <steve@steveklabnik.com>")
26 .version(&*format!("v{}", crate_version!()))
27 .setting(AppSettings::SubcommandRequired)
28 .subcommand(SubCommand::with_name("build")
29 .about("Build the book from the markdown files")
30 .arg_from_usage(d_message)
31 .arg_from_usage(dir_message)
32 .arg_from_usage(vers_message))
35 // Check which subcomamnd the user ran...
36 match matches.subcommand() {
37 ("build", Some(sub_matches)) => {
38 match sub_matches.value_of("mdbook-vers") {
40 if let Err(e) = build_1(sub_matches) {
41 eprintln!("Error: {}", e);
43 for cause in e.iter().skip(1) {
44 eprintln!("\tCaused By: {}", cause);
47 ::std::process::exit(101);
51 if let Err(e) = build_2(sub_matches) {
52 eprintln!("Error: {}", e);
54 for cause in e.iter().skip(1) {
55 eprintln!("\tCaused By: {}", cause);
58 ::std::process::exit(101);
62 panic!("Invalid mdBook version! Select '1' or '2'");
66 (_, _) => unreachable!(),
70 // Build command implementation
71 pub fn build_1(args: &ArgMatches) -> Result1<()> {
72 let book_dir = get_book_dir(args);
73 let mut book = MDBook1::load(&book_dir)?;
75 // Set this to allow us to catch bugs in advance.
76 book.config.build.create_missing = false;
78 if let Some(dest_dir) = args.value_of("dest-dir") {
79 book.config.build.build_dir = PathBuf::from(dest_dir);
87 // Build command implementation
88 pub fn build_2(args: &ArgMatches) -> Result2<()> {
89 let book_dir = get_book_dir(args);
90 let mut book = MDBook2::load(&book_dir)?;
92 // Set this to allow us to catch bugs in advance.
93 book.config.build.create_missing = false;
95 if let Some(dest_dir) = args.value_of("dest-dir") {
96 book.config.build.build_dir = PathBuf::from(dest_dir);
104 fn get_book_dir(args: &ArgMatches) -> PathBuf {
105 if let Some(dir) = args.value_of("dir") {
106 // Check if path is relative from current dir, or absolute...
107 let p = Path::new(dir);
109 env::current_dir().unwrap().join(dir)
114 env::current_dir().unwrap()