on 5f6e310 help sign in
bat/build.rs
91 lines 2.8 kB history raw
1// TODO: Re-enable generation of shell completion files (below) when clap 3 is out.
2// For more details, see https://github.com/sharkdp/bat/issues/372
4// For bat-as-a-library, no build script is required. The build script is for
5// the manpage and completions, which are only relevant to the bat application.
6#[cfg(not(feature = "application"))]
7fn main() {}
9#[cfg(feature = "application")]
10fn main() -> Result<(), Box<dyn std::error::Error>> {
11 use std::error::Error;
12 use std::fs;
13 use std::path::Path;
15 use lazy_static::lazy_static;
17 // Read environment variables.
18 lazy_static! {
19 static ref PROJECT_NAME: &'static str = option_env!("PROJECT_NAME").unwrap_or("bat");
20 static ref PROJECT_VERSION: &'static str = option_env!("CARGO_PKG_VERSION").unwrap();
21 static ref EXECUTABLE_NAME: &'static str = option_env!("PROJECT_EXECUTABLE")
22 .or(option_env!("PROJECT_NAME"))
23 .unwrap_or("bat");
24 }
26 /// Generates a file from a liquid template.
27 fn template(
28 variables: &liquid::Object,
29 in_file: &str,
30 out_file: impl AsRef<Path>,
31 ) -> Result<(), Box<dyn Error>> {
32 let template = liquid::ParserBuilder::with_stdlib()
33 .build()?
34 .parse(&fs::read_to_string(in_file)?)?;
36 fs::write(out_file, template.render(variables)?)?;
37 Ok(())
38 }
40 let variables = liquid::object!({
41 "PROJECT_NAME": PROJECT_NAME.to_owned(),
42 "PROJECT_EXECUTABLE": EXECUTABLE_NAME.to_owned(),
43 "PROJECT_VERSION": PROJECT_VERSION.to_owned(),
44 });
46 let out_dir_env = std::env::var_os("OUT_DIR").expect("OUT_DIR to be set in build.rs");
47 let out_dir = Path::new(&out_dir_env);
49 std::fs::create_dir_all(out_dir.join("assets/manual")).unwrap();
50 std::fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
52 template(
53 &variables,
54 "assets/manual/bat.1.in",
55 out_dir.join("assets/manual/bat.1"),
56 )?;
57 template(
58 &variables,
59 "assets/completions/bat.fish.in",
60 out_dir.join("assets/completions/bat.fish"),
61 )?;
63 Ok(())
66// #[macro_use]
67// extern crate clap;
69// use clap::Shell;
70// use std::fs;
72// include!("src/clap_app.rs");
74// const BIN_NAME: &str = "bat";
76// fn main() {
77// let outdir = std::env::var_os("SHELL_COMPLETIONS_DIR").or(std::env::var_os("OUT_DIR"));
79// let outdir = match outdir {
80// None => return,
81// Some(outdir) => outdir,
82// };
84// fs::create_dir_all(&outdir).unwrap();
86// let mut app = build_app(true);
87// app.gen_completions(BIN_NAME, Shell::Bash, &outdir);
88// app.gen_completions(BIN_NAME, Shell::Fish, &outdir);
89// app.gen_completions(BIN_NAME, Shell::Zsh, &outdir);
90// app.gen_completions(BIN_NAME, Shell::PowerShell, &outdir);
91// }