1 #![deny(rust_2018_idioms)]
3 use clap::{crate_version};
6 use std::path::{Path, PathBuf};
8 use clap::{App, ArgMatches, SubCommand, AppSettings};
10 use mdbook_1::{MDBook as MDBook1};
11 use mdbook_1::errors::{Result as Result1};
14 use mdbook::errors::Result;
17 let d_message = "-d, --dest-dir=[dest-dir]
18 'The output directory for your book{n}(Defaults to ./book when omitted)'";
19 let dir_message = "[dir]
20 'A directory for your book{n}(Defaults to Current Directory when omitted)'";
21 let vers_message = "-m, --mdbook-vers=[md-version]
22 'The version of mdbook to use for your book{n}(Defaults to 1 when omitted)'";
24 let matches = App::new("rustbook")
25 .about("Build a book with mdBook")
26 .author("Steve Klabnik <steve@steveklabnik.com>")
27 .version(&*format!("v{}", crate_version!()))
28 .setting(AppSettings::SubcommandRequired)
29 .subcommand(SubCommand::with_name("build")
30 .about("Build the book from the markdown files")
31 .arg_from_usage(d_message)
32 .arg_from_usage(dir_message)
33 .arg_from_usage(vers_message))
36 // Check which subcomamnd the user ran...
37 match matches.subcommand() {
38 ("build", Some(sub_matches)) => {
39 match sub_matches.value_of("mdbook-vers") {
41 if let Err(e) = build_1(sub_matches) {
42 eprintln!("Error: {}", e);
44 for cause in e.iter().skip(1) {
45 eprintln!("\tCaused By: {}", cause);
48 ::std::process::exit(101);
51 Some("2") | Some("3") => {
52 if let Err(e) = build(sub_matches) {
53 eprintln!("Error: {}", e);
55 for cause in e.iter().skip(1) {
56 eprintln!("\tCaused By: {}", cause);
59 ::std::process::exit(101);
63 panic!("Invalid mdBook version! Select '1' or '2' or '3'");
67 (_, _) => unreachable!(),
71 // Build command implementation
72 pub fn build_1(args: &ArgMatches<'_>) -> Result1<()> {
73 let book_dir = get_book_dir(args);
74 let mut book = MDBook1::load(&book_dir)?;
76 // Set this to allow us to catch bugs in advance.
77 book.config.build.create_missing = false;
79 if let Some(dest_dir) = args.value_of("dest-dir") {
80 book.config.build.build_dir = PathBuf::from(dest_dir);
88 // Build command implementation
89 pub fn build(args: &ArgMatches<'_>) -> Result<()> {
90 let book_dir = get_book_dir(args);
91 let mut book = MDBook::load(&book_dir)?;
93 // Set this to allow us to catch bugs in advance.
94 book.config.build.create_missing = false;
96 if let Some(dest_dir) = args.value_of("dest-dir") {
97 book.config.build.build_dir = PathBuf::from(dest_dir);
105 fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
106 if let Some(dir) = args.value_of("dir") {
107 // Check if path is relative from current dir, or absolute...
108 let p = Path::new(dir);
110 env::current_dir().unwrap().join(dir)
115 env::current_dir().unwrap()