]> git.lizzy.rs Git - rust.git/blob - src/tools/rustbook/src/main.rs
Rollup merge of #57259 - king6cong:master, r=alexcrichton
[rust.git] / src / tools / rustbook / src / main.rs
1 //
2 use clap::{crate_version};
3
4 use std::env;
5 use std::path::{Path, PathBuf};
6
7 use clap::{App, ArgMatches, SubCommand, AppSettings};
8
9 use mdbook_1::{MDBook as MDBook1};
10 use mdbook_1::errors::{Result as Result1};
11
12 use mdbook_2::{MDBook as MDBook2};
13 use mdbook_2::errors::{Result as Result2};
14
15 fn main() {
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)'";
22
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))
33                     .get_matches();
34
35     // Check which subcomamnd the user ran...
36     match matches.subcommand() {
37         ("build", Some(sub_matches)) => {
38             match sub_matches.value_of("mdbook-vers") {
39                 None | Some("1") => {
40                     if let Err(e) = build_1(sub_matches) {
41                         eprintln!("Error: {}", e);
42
43                         for cause in e.iter().skip(1) {
44                             eprintln!("\tCaused By: {}", cause);
45                         }
46
47                         ::std::process::exit(101);
48                     }
49                 }
50                 Some("2") => {
51                     if let Err(e) = build_2(sub_matches) {
52                         eprintln!("Error: {}", e);
53
54                         for cause in e.iter().skip(1) {
55                             eprintln!("\tCaused By: {}", cause);
56                         }
57
58                         ::std::process::exit(101);
59                     }
60                 }
61                 _ => {
62                     panic!("Invalid mdBook version! Select '1' or '2'");
63                 }
64             };
65         },
66         (_, _) => unreachable!(),
67     };
68 }
69
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)?;
74
75     // Set this to allow us to catch bugs in advance.
76     book.config.build.create_missing = false;
77
78     if let Some(dest_dir) = args.value_of("dest-dir") {
79         book.config.build.build_dir = PathBuf::from(dest_dir);
80     }
81
82     book.build()?;
83
84     Ok(())
85 }
86
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)?;
91
92     // Set this to allow us to catch bugs in advance.
93     book.config.build.create_missing = false;
94
95     if let Some(dest_dir) = args.value_of("dest-dir") {
96         book.config.build.build_dir = PathBuf::from(dest_dir);
97     }
98
99     book.build()?;
100
101     Ok(())
102 }
103
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);
108         if p.is_relative() {
109             env::current_dir().unwrap().join(dir)
110         } else {
111             p.to_path_buf()
112         }
113     } else {
114         env::current_dir().unwrap()
115     }
116 }