]> git.lizzy.rs Git - rust.git/blob - src/tools/rustbook/src/main.rs
Auto merge of #40706 - irfanhudda:doc-next-power-of-two, r=alexcrichton
[rust.git] / src / tools / rustbook / src / main.rs
1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10 //
11 extern crate mdbook;
12 #[macro_use]
13 extern crate clap;
14
15 use std::env;
16 use std::error::Error;
17 use std::io::{self, Write};
18 use std::path::{Path, PathBuf};
19
20 use clap::{App, ArgMatches, SubCommand, AppSettings};
21
22 use mdbook::MDBook;
23
24 fn main() {
25     let d_message = "-d, --dest-dir=[dest-dir]
26 'The output directory for your book{n}(Defaults to ./book when omitted)'";
27     let dir_message = "[dir]
28 'A directory for your book{n}(Defaults to Current Directory when omitted)'";
29
30     let matches = App::new("rustbook")
31                     .about("Build a book with mdBook")
32                     .author("Steve Klabnik <steve@steveklabnik.com>")
33                     .version(&*format!("v{}", crate_version!()))
34                     .setting(AppSettings::SubcommandRequired)
35                     .subcommand(SubCommand::with_name("build")
36                         .about("Build the book from the markdown files")
37                         .arg_from_usage(d_message)
38                         .arg_from_usage(dir_message))
39                     .get_matches();
40
41     // Check which subcomamnd the user ran...
42     let res = match matches.subcommand() {
43         ("build", Some(sub_matches)) => build(sub_matches),
44         ("test", Some(sub_matches)) => test(sub_matches),
45         (_, _) => unreachable!(),
46     };
47
48     if let Err(e) = res {
49         writeln!(&mut io::stderr(), "An error occured:\n{}", e).ok();
50         ::std::process::exit(101);
51     }
52 }
53
54 // Build command implementation
55 fn build(args: &ArgMatches) -> Result<(), Box<Error>> {
56     let book = build_mdbook_struct(args);
57
58     let mut book = match args.value_of("dest-dir") {
59         Some(dest_dir) => book.set_dest(Path::new(dest_dir)),
60         None => book
61     };
62
63     try!(book.build());
64
65     Ok(())
66 }
67
68 fn test(args: &ArgMatches) -> Result<(), Box<Error>> {
69     let mut book = build_mdbook_struct(args);
70
71     try!(book.test());
72
73     Ok(())
74 }
75
76 fn build_mdbook_struct(args: &ArgMatches) -> mdbook::MDBook {
77     let book_dir = get_book_dir(args);
78     let mut book = MDBook::new(&book_dir).read_config();
79
80     // By default mdbook will attempt to create non-existent files referenced
81     // from SUMMARY.md files. This is problematic on CI where we mount the
82     // source directory as readonly. To avoid any issues, we'll disabled
83     // mdbook's implicit file creation feature.
84     book.create_missing = false;
85
86     book
87 }
88
89 fn get_book_dir(args: &ArgMatches) -> PathBuf {
90     if let Some(dir) = args.value_of("dir") {
91         // Check if path is relative from current dir, or absolute...
92         let p = Path::new(dir);
93         if p.is_relative() {
94             env::current_dir().unwrap().join(dir)
95         } else {
96             p.to_path_buf()
97         }
98     } else {
99         env::current_dir().unwrap()
100     }
101 }