2023-10-30 10:49:50 +01:00
|
|
|
use std::{env, fs, path::PathBuf};
|
2020-03-30 18:43:13 +02:00
|
|
|
|
2023-10-30 10:49:50 +01:00
|
|
|
use crate::util::render_template;
|
2020-03-30 18:43:13 +02:00
|
|
|
|
2023-10-27 08:37:48 +02:00
|
|
|
/// Generate manpage and shell completions for the bat application.
|
2023-10-30 10:49:50 +01:00
|
|
|
pub fn gen_man_and_comp() -> anyhow::Result<()> {
|
2023-11-02 16:29:42 +01:00
|
|
|
println!("cargo:rerun-if-changed=assets/manual/");
|
|
|
|
println!("cargo:rerun-if-changed=assets/completions/");
|
|
|
|
|
|
|
|
println!("cargo:rerun-if-env-changed=PROJECT_NAME");
|
|
|
|
println!("cargo:rerun-if-env-changed=PROJECT_EXECUTABLE");
|
|
|
|
println!("cargo:rerun-if-env-changed=CARGO_PKG_VERSION");
|
|
|
|
println!("cargo:rerun-if-env-changed=BAT_ASSETS_GEN_DIR");
|
|
|
|
|
2020-03-30 18:43:13 +02:00
|
|
|
// Read environment variables.
|
2023-10-28 03:26:00 +02:00
|
|
|
let project_name = env::var("PROJECT_NAME").unwrap_or("bat".into());
|
|
|
|
let executable_name = env::var("PROJECT_EXECUTABLE").unwrap_or(project_name.clone());
|
2020-09-20 19:50:39 +02:00
|
|
|
let executable_name_uppercase = executable_name.to_uppercase();
|
2023-10-28 03:26:00 +02:00
|
|
|
let project_version = env::var("CARGO_PKG_VERSION")?;
|
2020-03-30 18:43:13 +02:00
|
|
|
|
2023-10-28 06:01:28 +02:00
|
|
|
let variables = [
|
|
|
|
("PROJECT_NAME", project_name),
|
|
|
|
("PROJECT_EXECUTABLE", executable_name),
|
|
|
|
("PROJECT_EXECUTABLE_UPPERCASE", executable_name_uppercase),
|
|
|
|
("PROJECT_VERSION", project_version),
|
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.collect();
|
2019-09-27 21:13:55 +02:00
|
|
|
|
2023-10-28 03:26:00 +02:00
|
|
|
let Some(out_dir) = env::var_os("BAT_ASSETS_GEN_DIR")
|
|
|
|
.or_else(|| env::var_os("OUT_DIR"))
|
|
|
|
.map(PathBuf::from)
|
2023-10-31 06:45:40 +01:00
|
|
|
else {
|
|
|
|
anyhow::bail!("BAT_ASSETS_GEN_DIR or OUT_DIR should be set for build.rs");
|
|
|
|
};
|
2020-03-22 11:33:50 +01:00
|
|
|
|
2020-04-24 07:56:54 +02:00
|
|
|
fs::create_dir_all(out_dir.join("assets/manual")).unwrap();
|
|
|
|
fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
|
2020-03-22 11:33:50 +01:00
|
|
|
|
2023-10-27 08:37:48 +02:00
|
|
|
render_template(
|
2020-03-22 11:33:50 +01:00
|
|
|
&variables,
|
|
|
|
"assets/manual/bat.1.in",
|
|
|
|
out_dir.join("assets/manual/bat.1"),
|
|
|
|
)?;
|
2023-10-27 08:37:48 +02:00
|
|
|
render_template(
|
2021-06-03 16:57:05 +02:00
|
|
|
&variables,
|
|
|
|
"assets/completions/bat.bash.in",
|
|
|
|
out_dir.join("assets/completions/bat.bash"),
|
|
|
|
)?;
|
2023-10-27 08:37:48 +02:00
|
|
|
render_template(
|
2019-09-27 21:13:55 +02:00
|
|
|
&variables,
|
|
|
|
"assets/completions/bat.fish.in",
|
2020-03-22 11:33:50 +01:00
|
|
|
out_dir.join("assets/completions/bat.fish"),
|
2019-09-27 21:13:55 +02:00
|
|
|
)?;
|
2023-10-27 08:37:48 +02:00
|
|
|
render_template(
|
2021-08-28 13:39:17 +02:00
|
|
|
&variables,
|
|
|
|
"assets/completions/_bat.ps1.in",
|
|
|
|
out_dir.join("assets/completions/_bat.ps1"),
|
|
|
|
)?;
|
2023-10-27 08:37:48 +02:00
|
|
|
render_template(
|
2020-09-08 12:34:42 +02:00
|
|
|
&variables,
|
|
|
|
"assets/completions/bat.zsh.in",
|
|
|
|
out_dir.join("assets/completions/bat.zsh"),
|
|
|
|
)?;
|
2019-09-27 21:13:55 +02:00
|
|
|
|
2019-09-27 21:06:54 +02:00
|
|
|
Ok(())
|
|
|
|
}
|