2015-08-01 12:59:05 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
2018-01-03 19:32:49 +08:00
|
|
|
#[macro_use]
|
2017-10-03 19:40:23 +08:00
|
|
|
extern crate log;
|
2015-11-09 21:31:00 +08:00
|
|
|
|
2021-01-05 05:51:02 +08:00
|
|
|
use anyhow::anyhow;
|
2017-12-30 18:43:46 +08:00
|
|
|
use chrono::Local;
|
2022-01-19 05:56:45 +08:00
|
|
|
use clap::{App, AppSettings, Arg, ArgMatches, SubCommand};
|
|
|
|
use clap_complete::Shell;
|
2017-12-30 18:43:46 +08:00
|
|
|
use env_logger::Builder;
|
2018-07-24 01:45:01 +08:00
|
|
|
use log::LevelFilter;
|
2018-01-07 22:10:48 +08:00
|
|
|
use mdbook::utils;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::env;
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::{Path, PathBuf};
|
2015-07-07 03:12:24 +08:00
|
|
|
|
2018-07-25 05:34:49 +08:00
|
|
|
mod cmd;
|
2015-11-09 21:31:00 +08:00
|
|
|
|
2018-12-04 07:10:09 +08:00
|
|
|
const VERSION: &str = concat!("v", crate_version!());
|
2015-07-07 03:12:24 +08:00
|
|
|
|
|
|
|
fn main() {
|
2017-06-29 05:37:03 +08:00
|
|
|
init_logger();
|
2016-08-14 21:55:10 +08:00
|
|
|
|
2021-01-05 05:51:02 +08:00
|
|
|
let app = create_clap_app();
|
|
|
|
|
|
|
|
// Check which subcomamnd the user ran...
|
|
|
|
let res = match app.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),
|
2021-01-05 05:51:02 +08:00
|
|
|
#[cfg(feature = "watch")]
|
2022-01-19 05:56:45 +08:00
|
|
|
Some(("watch", sub_matches)) => cmd::watch::execute(sub_matches),
|
2021-01-05 05:51:02 +08:00
|
|
|
#[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)) => (|| {
|
2021-01-05 05:51:02 +08:00
|
|
|
let shell: Shell = sub_matches
|
|
|
|
.value_of("shell")
|
|
|
|
.ok_or_else(|| anyhow!("Shell name missing."))?
|
|
|
|
.parse()
|
|
|
|
.map_err(|s| anyhow!("Invalid shell: {}", s))?;
|
|
|
|
|
2022-01-19 05:56:45 +08:00
|
|
|
let mut complete_app = create_clap_app();
|
|
|
|
clap_complete::generate(
|
|
|
|
shell,
|
|
|
|
&mut complete_app,
|
|
|
|
"mdbook",
|
|
|
|
&mut std::io::stdout().lock(),
|
|
|
|
);
|
2021-01-05 05:51:02 +08:00
|
|
|
Ok(())
|
|
|
|
})(),
|
2022-01-19 05:56:45 +08:00
|
|
|
_ => unreachable!(),
|
2021-01-05 05:51:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
utils::log_backtrace(&e);
|
|
|
|
|
|
|
|
std::process::exit(101);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a list of valid arguments and sub-commands
|
2022-01-19 05:56:45 +08:00
|
|
|
fn create_clap_app() -> App<'static> {
|
2018-12-25 20:10:07 +08:00
|
|
|
let app = App::new(crate_name!())
|
|
|
|
.about(crate_description!())
|
2018-08-03 04:48:22 +08:00
|
|
|
.author("Mathieu David <mathieudavid@mathieudavid.org>")
|
|
|
|
.version(VERSION)
|
|
|
|
.setting(AppSettings::GlobalVersion)
|
|
|
|
.setting(AppSettings::ArgRequiredElseHelp)
|
2018-12-25 20:35:37 +08:00
|
|
|
.setting(AppSettings::ColoredHelp)
|
2018-08-03 04:48:22 +08:00
|
|
|
.after_help(
|
|
|
|
"For more information about a specific command, try `mdbook <command> --help`\n\
|
2019-10-29 21:04:16 +08:00
|
|
|
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())
|
2021-01-05 05:51:02 +08:00
|
|
|
.subcommand(cmd::clean::make_subcommand())
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("completions")
|
|
|
|
.about("Generate shell completions for your shell to stdout")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("shell")
|
|
|
|
.takes_value(true)
|
2022-01-19 05:56:45 +08:00
|
|
|
.possible_values(Shell::possible_values())
|
2021-01-05 05:51:02 +08:00
|
|
|
.help("the shell to generate completions for")
|
|
|
|
.value_name("SHELL")
|
|
|
|
.required(true),
|
|
|
|
),
|
|
|
|
);
|
2017-06-26 06:31:42 +08:00
|
|
|
|
|
|
|
#[cfg(feature = "watch")]
|
2018-07-25 05:34:49 +08:00
|
|
|
let app = app.subcommand(cmd::watch::make_subcommand());
|
2017-06-26 06:31:42 +08:00
|
|
|
#[cfg(feature = "serve")]
|
2018-07-25 05:34:49 +08:00
|
|
|
let app = app.subcommand(cmd::serve::make_subcommand());
|
2015-08-01 12:59:05 +08:00
|
|
|
|
2021-01-05 05:51:02 +08:00
|
|
|
app
|
2015-07-07 03:12:24 +08:00
|
|
|
}
|
|
|
|
|
2017-06-29 05:37:03 +08:00
|
|
|
fn init_logger() {
|
2017-12-30 18:43:46 +08:00
|
|
|
let mut builder = Builder::new();
|
2017-06-29 05:37:03 +08:00
|
|
|
|
2017-12-30 18:43:46 +08:00
|
|
|
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()
|
|
|
|
)
|
2017-12-30 18:43:46 +08:00
|
|
|
});
|
2017-06-29 05:37:03 +08:00
|
|
|
|
|
|
|
if let Ok(var) = env::var("RUST_LOG") {
|
2019-05-26 20:05:42 +08:00
|
|
|
builder.parse_filters(&var);
|
2017-12-30 18:43:46 +08:00
|
|
|
} else {
|
|
|
|
// if no RUST_LOG provided, default to logging at the Info level
|
|
|
|
builder.filter(None, LevelFilter::Info);
|
2018-03-07 21:02:06 +08:00
|
|
|
// Filter extraneous html5ever not-implemented messages
|
|
|
|
builder.filter(Some("html5ever"), LevelFilter::Error);
|
2017-06-29 05:37:03 +08:00
|
|
|
}
|
|
|
|
|
2017-12-30 18:43:46 +08:00
|
|
|
builder.init();
|
2017-06-29 05:37:03 +08:00
|
|
|
}
|
|
|
|
|
2015-08-01 12:59:05 +08:00
|
|
|
fn get_book_dir(args: &ArgMatches) -> PathBuf {
|
|
|
|
if let Some(dir) = args.value_of("dir") {
|
|
|
|
// Check if path is relative from current dir, or absolute...
|
|
|
|
let p = Path::new(dir);
|
|
|
|
if p.is_relative() {
|
2016-03-18 05:31:28 +08:00
|
|
|
env::current_dir().unwrap().join(dir)
|
2015-08-01 12:59:05 +08:00
|
|
|
} else {
|
2016-03-18 05:31:28 +08:00
|
|
|
p.to_path_buf()
|
2015-08-01 12:59:05 +08:00
|
|
|
}
|
2015-07-18 06:04:20 +08:00
|
|
|
} else {
|
2018-01-07 22:10:48 +08:00
|
|
|
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");
|
2021-09-27 01:55:41 +08:00
|
|
|
if let Err(e) = opener::open(path) {
|
2018-01-07 22:10:48 +08:00
|
|
|
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() {
|
|
|
|
create_clap_app().debug_assert();
|
|
|
|
}
|