on master help sign in
bat/build.rs
106 lines 3.4 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::collections::HashMap;
12 use std::error::Error;
13 use std::fs;
14 use std::path::Path;
16 // Read environment variables.
17 let project_name = option_env!("PROJECT_NAME").unwrap_or("bat");
18 let executable_name = option_env!("PROJECT_EXECUTABLE").unwrap_or(project_name);
19 let executable_name_uppercase = executable_name.to_uppercase();
20 static PROJECT_VERSION: &str = env!("CARGO_PKG_VERSION");
22 /// Generates a file from a template.
23 fn template(
24 variables: &HashMap<&str, &str>,
25 in_file: &str,
26 out_file: impl AsRef<Path>,
27 ) -> Result<(), Box<dyn Error>> {
28 let mut content = fs::read_to_string(in_file)?;
30 for (variable_name, value) in variables {
31 // Replace {{variable_name}} by the value
32 let pattern = format!("{{{{{variable_name}}}}}", variable_name = variable_name);
33 content = content.replace(&pattern, value);
34 }
36 fs::write(out_file, content)?;
37 Ok(())
38 }
40 let mut variables = HashMap::new();
41 variables.insert("PROJECT_NAME", project_name);
42 variables.insert("PROJECT_EXECUTABLE", executable_name);
43 variables.insert("PROJECT_EXECUTABLE_UPPERCASE", &executable_name_uppercase);
44 variables.insert("PROJECT_VERSION", PROJECT_VERSION);
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 fs::create_dir_all(out_dir.join("assets/manual")).unwrap();
50 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.bash.in",
60 out_dir.join("assets/completions/bat.bash"),
61 )?;
62 template(
63 &variables,
64 "assets/completions/bat.fish.in",
65 out_dir.join("assets/completions/bat.fish"),
66 )?;
67 template(
68 &variables,
69 "assets/completions/_bat.ps1.in",
70 out_dir.join("assets/completions/_bat.ps1"),
71 )?;
72 template(
73 &variables,
74 "assets/completions/bat.zsh.in",
75 out_dir.join("assets/completions/bat.zsh"),
76 )?;
78 Ok(())
81// #[macro_use]
82// extern crate clap;
84// use clap::Shell;
85// use std::fs;
87// include!("src/clap_app.rs");
89// const BIN_NAME: &str = "bat";
91// fn main() {
92// let outdir = std::env::var_os("SHELL_COMPLETIONS_DIR").or(std::env::var_os("OUT_DIR"));
94// let outdir = match outdir {
95// None => return,
96// Some(outdir) => outdir,
97// };
99// fs::create_dir_all(&outdir).unwrap();
101// let mut app = build_app(true);
102// app.gen_completions(BIN_NAME, Shell::Bash, &outdir);
103// app.gen_completions(BIN_NAME, Shell::Fish, &outdir);
104// app.gen_completions(BIN_NAME, Shell::Zsh, &outdir);
105// app.gen_completions(BIN_NAME, Shell::PowerShell, &outdir);
106// }