]>
Commit | Line | Data |
---|---|---|
8bb4bdeb XL |
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; | |
8bb4bdeb XL |
16 | use std::path::{Path, PathBuf}; |
17 | ||
18 | use clap::{App, ArgMatches, SubCommand, AppSettings}; | |
19 | ||
20 | use mdbook::MDBook; | |
ea8adc8c | 21 | use mdbook::errors::Result; |
8bb4bdeb XL |
22 | |
23 | fn main() { | |
24 | let d_message = "-d, --dest-dir=[dest-dir] | |
25 | 'The output directory for your book{n}(Defaults to ./book when omitted)'"; | |
26 | let dir_message = "[dir] | |
27 | 'A directory for your book{n}(Defaults to Current Directory when omitted)'"; | |
28 | ||
29 | let matches = App::new("rustbook") | |
30 | .about("Build a book with mdBook") | |
31 | .author("Steve Klabnik <steve@steveklabnik.com>") | |
32 | .version(&*format!("v{}", crate_version!())) | |
33 | .setting(AppSettings::SubcommandRequired) | |
34 | .subcommand(SubCommand::with_name("build") | |
35 | .about("Build the book from the markdown files") | |
36 | .arg_from_usage(d_message) | |
37 | .arg_from_usage(dir_message)) | |
38 | .get_matches(); | |
39 | ||
40 | // Check which subcomamnd the user ran... | |
41 | let res = match matches.subcommand() { | |
42 | ("build", Some(sub_matches)) => build(sub_matches), | |
8bb4bdeb XL |
43 | (_, _) => unreachable!(), |
44 | }; | |
45 | ||
46 | if let Err(e) = res { | |
2c00a5a8 XL |
47 | eprintln!("Error: {}", e); |
48 | ||
49 | for cause in e.iter().skip(1) { | |
50 | eprintln!("\tCaused By: {}", cause); | |
51 | } | |
52 | ||
8bb4bdeb XL |
53 | ::std::process::exit(101); |
54 | } | |
55 | } | |
8bb4bdeb | 56 | // Build command implementation |
ea8adc8c XL |
57 | pub fn build(args: &ArgMatches) -> Result<()> { |
58 | let book_dir = get_book_dir(args); | |
2c00a5a8 | 59 | let mut book = MDBook::load(&book_dir)?; |
8bb4bdeb | 60 | |
2c00a5a8 XL |
61 | // Set this to allow us to catch bugs in advance. |
62 | book.config.build.create_missing = false; | |
63 | ||
64 | if let Some(dest_dir) = args.value_of("dest-dir") { | |
65 | book.config.build.build_dir = PathBuf::from(dest_dir); | |
66 | } | |
8bb4bdeb | 67 | |
ea8adc8c | 68 | book.build()?; |
8bb4bdeb XL |
69 | |
70 | Ok(()) | |
71 | } | |
72 | ||
73 | fn get_book_dir(args: &ArgMatches) -> PathBuf { | |
74 | if let Some(dir) = args.value_of("dir") { | |
75 | // Check if path is relative from current dir, or absolute... | |
76 | let p = Path::new(dir); | |
77 | if p.is_relative() { | |
78 | env::current_dir().unwrap().join(dir) | |
79 | } else { | |
80 | p.to_path_buf() | |
81 | } | |
82 | } else { | |
83 | env::current_dir().unwrap() | |
84 | } | |
85 | } |