]>
Commit | Line | Data |
---|---|---|
e1599b0c | 1 | use clap::crate_version; |
8bb4bdeb XL |
2 | |
3 | use std::env; | |
8bb4bdeb XL |
4 | use std::path::{Path, PathBuf}; |
5 | ||
e1599b0c | 6 | use clap::{App, AppSettings, ArgMatches, SubCommand}; |
8bb4bdeb | 7 | |
e1599b0c | 8 | use mdbook::errors::Result as Result3; |
dc9dc135 | 9 | use mdbook::MDBook; |
416331ca | 10 | |
60c5eb7d XL |
11 | #[cfg(feature = "linkcheck")] |
12 | use failure::Error; | |
13 | #[cfg(feature = "linkcheck")] | |
14 | use mdbook::renderer::RenderContext; | |
15 | ||
8bb4bdeb XL |
16 | fn main() { |
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 | ||
22 | let matches = App::new("rustbook") | |
e1599b0c XL |
23 | .about("Build a book with mdBook") |
24 | .author("Steve Klabnik <steve@steveklabnik.com>") | |
25 | .version(&*format!("v{}", crate_version!())) | |
26 | .setting(AppSettings::SubcommandRequired) | |
27 | .subcommand( | |
28 | 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 | ) | |
33 | .subcommand( | |
34 | SubCommand::with_name("linkcheck") | |
35 | .about("Run linkcheck with mdBook 3") | |
36 | .arg_from_usage(dir_message), | |
37 | ) | |
38 | .get_matches(); | |
8bb4bdeb XL |
39 | |
40 | // Check which subcomamnd the user ran... | |
9fa01778 XL |
41 | match matches.subcommand() { |
42 | ("build", Some(sub_matches)) => { | |
416331ca XL |
43 | if let Err(e) = build(sub_matches) { |
44 | eprintln!("Error: {}", e); | |
9fa01778 | 45 | |
416331ca XL |
46 | for cause in e.iter().skip(1) { |
47 | eprintln!("\tCaused By: {}", cause); | |
9fa01778 | 48 | } |
9fa01778 | 49 | |
416331ca XL |
50 | ::std::process::exit(101); |
51 | } | |
e1599b0c | 52 | } |
416331ca | 53 | ("linkcheck", Some(sub_matches)) => { |
60c5eb7d XL |
54 | #[cfg(feature = "linkcheck")] |
55 | { | |
56 | if let Err(err) = linkcheck(sub_matches) { | |
57 | eprintln!("Error: {}", err); | |
58 | std::process::exit(101); | |
59 | } | |
60 | } | |
61 | ||
62 | #[cfg(not(feature = "linkcheck"))] | |
63 | { | |
64 | // This avoids the `unused_binding` lint. | |
65 | println!( | |
66 | "mdbook-linkcheck is disabled, but arguments were passed: {:?}", | |
67 | sub_matches | |
68 | ); | |
69 | } | |
e1599b0c | 70 | } |
8bb4bdeb XL |
71 | (_, _) => unreachable!(), |
72 | }; | |
9fa01778 | 73 | } |
8bb4bdeb | 74 | |
60c5eb7d XL |
75 | #[cfg(feature = "linkcheck")] |
76 | pub fn linkcheck(args: &ArgMatches<'_>) -> Result<(), Error> { | |
77 | let book_dir = get_book_dir(args); | |
78 | let book = MDBook::load(&book_dir).unwrap(); | |
79 | let cfg = book.config; | |
80 | let render_ctx = RenderContext::new(&book_dir, book.book, cfg, &book_dir); | |
81 | let cache_file = render_ctx.destination.join("cache.json"); | |
82 | let color = codespan_reporting::term::termcolor::ColorChoice::Auto; | |
83 | mdbook_linkcheck::run(&cache_file, color, &render_ctx) | |
84 | } | |
85 | ||
8bb4bdeb | 86 | // Build command implementation |
416331ca | 87 | pub fn build(args: &ArgMatches<'_>) -> Result3<()> { |
ea8adc8c | 88 | let book_dir = get_book_dir(args); |
dc9dc135 | 89 | let mut book = MDBook::load(&book_dir)?; |
8bb4bdeb | 90 | |
2c00a5a8 XL |
91 | // Set this to allow us to catch bugs in advance. |
92 | book.config.build.create_missing = false; | |
93 | ||
94 | if let Some(dest_dir) = args.value_of("dest-dir") { | |
95 | book.config.build.build_dir = PathBuf::from(dest_dir); | |
96 | } | |
8bb4bdeb | 97 | |
ea8adc8c | 98 | book.build()?; |
8bb4bdeb XL |
99 | |
100 | Ok(()) | |
101 | } | |
102 | ||
532ac7d7 | 103 | fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf { |
8bb4bdeb XL |
104 | if let Some(dir) = args.value_of("dir") { |
105 | // Check if path is relative from current dir, or absolute... | |
106 | let p = Path::new(dir); | |
e74abb32 | 107 | if p.is_relative() { env::current_dir().unwrap().join(dir) } else { p.to_path_buf() } |
8bb4bdeb XL |
108 | } else { |
109 | env::current_dir().unwrap() | |
110 | } | |
111 | } |