on 5ebf021 help sign in
bat/build.rs
85 lines 2.6 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 // Read environment variables.
16 let project_name = option_env!("PROJECT_NAME").unwrap_or("bat");
17 let executable_name = option_env!("PROJECT_EXECUTABLE").unwrap_or(project_name);
18 static PROJECT_VERSION: &str = env!("CARGO_PKG_VERSION");
20 /// Generates a file from a liquid template.
21 fn template(
22 variables: &liquid::Object,
23 in_file: &str,
24 out_file: impl AsRef<Path>,
25 ) -> Result<(), Box<dyn Error>> {
26 let template = liquid::ParserBuilder::with_stdlib()
27 .build()?
28 .parse(&fs::read_to_string(in_file)?)?;
30 fs::write(out_file, template.render(variables)?)?;
31 Ok(())
32 }
34 let variables = liquid::object!({
35 "PROJECT_NAME": project_name,
36 "PROJECT_EXECUTABLE": executable_name,
37 "PROJECT_VERSION": PROJECT_VERSION,
38 });
40 let out_dir_env = std::env::var_os("OUT_DIR").expect("OUT_DIR to be set in build.rs");
41 let out_dir = Path::new(&out_dir_env);
43 fs::create_dir_all(out_dir.join("assets/manual")).unwrap();
44 fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
46 template(
47 &variables,
48 "assets/manual/bat.1.in",
49 out_dir.join("assets/manual/bat.1"),
50 )?;
51 template(
52 &variables,
53 "assets/completions/bat.fish.in",
54 out_dir.join("assets/completions/bat.fish"),
55 )?;
57 Ok(())
60// #[macro_use]
61// extern crate clap;
63// use clap::Shell;
64// use std::fs;
66// include!("src/clap_app.rs");
68// const BIN_NAME: &str = "bat";
70// fn main() {
71// let outdir = std::env::var_os("SHELL_COMPLETIONS_DIR").or(std::env::var_os("OUT_DIR"));
73// let outdir = match outdir {
74// None => return,
75// Some(outdir) => outdir,
76// };
78// fs::create_dir_all(&outdir).unwrap();
80// let mut app = build_app(true);
81// app.gen_completions(BIN_NAME, Shell::Bash, &outdir);
82// app.gen_completions(BIN_NAME, Shell::Fish, &outdir);
83// app.gen_completions(BIN_NAME, Shell::Zsh, &outdir);
84// app.gen_completions(BIN_NAME, Shell::PowerShell, &outdir);
85// }