| 1 |
use super::command_prelude::*; |
| 2 |
use crate::{get_book_dir, open}; |
| 3 |
use anyhow::Result; |
| 4 |
use mdbook_driver::MDBook; |
| 5 |
use tracing::error; |
| 6 |
|
| 7 |
// Create clap subcommand arguments |
| 8 |
pub fn make_subcommand() -> Command { |
| 9 |
Command::new("build") |
| 10 |
.about("Builds a book from its markdown files") |
| 11 |
.arg_dest_dir() |
| 12 |
.arg_root_dir() |
| 13 |
.arg_open() |
| 14 |
} |
| 15 |
|
| 16 |
// Build command implementation |
| 17 |
pub fn execute(args: &ArgMatches) -> Result<()> { |
| 18 |
let book_dir = get_book_dir(args); |
| 19 |
let mut book = MDBook::load(book_dir)?; |
| 20 |
|
| 21 |
set_dest_dir(args, &mut book); |
| 22 |
|
| 23 |
book.build()?; |
| 24 |
|
| 25 |
if args.get_flag("open") { |
| 26 |
// FIXME: What's the right behaviour if we don't use the HTML renderer? |
| 27 |
let path = book.build_dir_for("html").join("index.html"); |
| 28 |
if !path.exists() { |
| 29 |
error!("No chapter available to open"); |
| 30 |
std::process::exit(1) |
| 31 |
} |
| 32 |
open(path); |
| 33 |
} |
| 34 |
|
| 35 |
Ok(()) |
| 36 |
} |