mdBook/src/main.rs

147 lines
4.3 KiB
Rust
Raw Normal View History

2015-08-01 12:59:05 +08:00
#[macro_use]
extern crate clap;
#[macro_use]
extern crate log;
use anyhow::anyhow;
use chrono::Local;
2022-07-04 23:16:31 +08:00
use clap::{Arg, ArgMatches, Command};
2022-01-19 05:56:45 +08:00
use clap_complete::Shell;
use env_logger::Builder;
2018-07-24 01:45:01 +08:00
use log::LevelFilter;
use mdbook::utils;
2018-07-24 01:45:01 +08:00
use std::env;
use std::ffi::OsStr;
use std::io::Write;
2022-07-04 23:16:31 +08:00
use std::path::PathBuf;
2015-07-07 03:12:24 +08:00
mod cmd;
const VERSION: &str = concat!("v", crate_version!());
2015-07-07 03:12:24 +08:00
fn main() {
init_logger();
2016-08-14 21:55:10 +08:00
2022-07-04 23:16:31 +08:00
let command = create_clap_command();
2022-07-04 23:16:31 +08:00
// Check which subcommand the user ran...
let res = match command.get_matches().subcommand() {
2022-01-19 05:56:45 +08:00
Some(("init", sub_matches)) => cmd::init::execute(sub_matches),
Some(("build", sub_matches)) => cmd::build::execute(sub_matches),
Some(("clean", sub_matches)) => cmd::clean::execute(sub_matches),
#[cfg(feature = "watch")]
2022-01-19 05:56:45 +08:00
Some(("watch", sub_matches)) => cmd::watch::execute(sub_matches),
#[cfg(feature = "serve")]
2022-01-19 05:56:45 +08:00
Some(("serve", sub_matches)) => cmd::serve::execute(sub_matches),
Some(("test", sub_matches)) => cmd::test::execute(sub_matches),
Some(("completions", sub_matches)) => (|| {
2022-07-04 23:16:31 +08:00
let shell = sub_matches
.get_one::<Shell>("shell")
.ok_or_else(|| anyhow!("Shell name missing."))?;
2022-07-04 23:16:31 +08:00
let mut complete_app = create_clap_command();
2022-01-19 05:56:45 +08:00
clap_complete::generate(
2022-07-04 23:16:31 +08:00
*shell,
2022-01-19 05:56:45 +08:00
&mut complete_app,
"mdbook",
&mut std::io::stdout().lock(),
);
Ok(())
})(),
2022-01-19 05:56:45 +08:00
_ => unreachable!(),
};
if let Err(e) = res {
utils::log_backtrace(&e);
std::process::exit(101);
}
}
/// Create a list of valid arguments and sub-commands
2022-07-04 23:16:31 +08:00
fn create_clap_command() -> Command {
let app = Command::new(crate_name!())
2018-12-25 20:10:07 +08:00
.about(crate_description!())
2018-08-03 04:48:22 +08:00
.author("Mathieu David <mathieudavid@mathieudavid.org>")
.version(VERSION)
2022-07-04 23:16:31 +08:00
.propagate_version(true)
.arg_required_else_help(true)
2018-08-03 04:48:22 +08:00
.after_help(
"For more information about a specific command, try `mdbook <command> --help`\n\
The source code for mdBook is available at: https://github.com/rust-lang/mdBook",
2018-08-03 04:48:22 +08:00
)
.subcommand(cmd::init::make_subcommand())
.subcommand(cmd::build::make_subcommand())
.subcommand(cmd::test::make_subcommand())
.subcommand(cmd::clean::make_subcommand())
.subcommand(
2022-07-04 23:16:31 +08:00
Command::new("completions")
.about("Generate shell completions for your shell to stdout")
.arg(
Arg::new("shell")
2022-07-04 23:16:31 +08:00
.value_parser(clap::value_parser!(Shell))
.help("the shell to generate completions for")
.value_name("SHELL")
.required(true),
),
);
#[cfg(feature = "watch")]
let app = app.subcommand(cmd::watch::make_subcommand());
#[cfg(feature = "serve")]
let app = app.subcommand(cmd::serve::make_subcommand());
2015-08-01 12:59:05 +08:00
app
2015-07-07 03:12:24 +08:00
}
fn init_logger() {
let mut builder = Builder::new();
builder.format(|formatter, record| {
2018-02-18 15:10:47 +08:00
writeln!(
formatter,
"{} [{}] ({}): {}",
Local::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
record.target(),
record.args()
)
});
if let Ok(var) = env::var("RUST_LOG") {
builder.parse_filters(&var);
} else {
// if no RUST_LOG provided, default to logging at the Info level
builder.filter(None, LevelFilter::Info);
// Filter extraneous html5ever not-implemented messages
builder.filter(Some("html5ever"), LevelFilter::Error);
}
builder.init();
}
2015-08-01 12:59:05 +08:00
fn get_book_dir(args: &ArgMatches) -> PathBuf {
2022-07-04 23:16:31 +08:00
if let Some(p) = args.get_one::<PathBuf>("dir") {
2015-08-01 12:59:05 +08:00
// Check if path is relative from current dir, or absolute...
if p.is_relative() {
2022-07-04 23:16:31 +08:00
env::current_dir().unwrap().join(p)
2015-08-01 12:59:05 +08:00
} else {
p.to_path_buf()
2015-08-01 12:59:05 +08:00
}
} else {
env::current_dir().expect("Unable to determine the current directory")
2015-07-07 03:12:24 +08:00
}
}
2016-04-02 10:46:05 +08:00
2017-01-02 01:42:47 +08:00
fn open<P: AsRef<OsStr>>(path: P) {
2021-09-07 03:52:43 +08:00
info!("Opening web browser");
if let Err(e) = opener::open(path) {
error!("Error opening web browser: {}", e);
2017-01-02 01:42:47 +08:00
}
}
2022-01-19 05:56:45 +08:00
#[test]
fn verify_app() {
2022-07-04 23:16:31 +08:00
create_clap_command().debug_assert();
2022-01-19 05:56:45 +08:00
}