#[cfg(not(feature = "application"))]
fn main() {}
#[cfg(feature = "application")]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::error::Error;
use std::fs;
use std::path::Path;
use lazy_static::lazy_static;
lazy_static! {
static ref PROJECT_NAME: &'static str = option_env!("PROJECT_NAME").unwrap_or("bat");
static ref PROJECT_VERSION: &'static str = option_env!("CARGO_PKG_VERSION").unwrap();
static ref EXECUTABLE_NAME: &'static str = option_env!("PROJECT_EXECUTABLE")
.or(option_env!("PROJECT_NAME"))
.unwrap_or("bat");
}
fn template(
variables: &liquid::Object,
in_file: &str,
out_file: impl AsRef<Path>,
) -> Result<(), Box<dyn Error>> {
let template = liquid::ParserBuilder::with_stdlib()
.build()?
.parse(&fs::read_to_string(in_file)?)?;
fs::write(out_file, template.render(variables)?)?;
Ok(())
}
let variables = liquid::object!({
"PROJECT_NAME": PROJECT_NAME.to_owned(),
"PROJECT_EXECUTABLE": EXECUTABLE_NAME.to_owned(),
"PROJECT_VERSION": PROJECT_VERSION.to_owned(),
});
let out_dir_env = std::env::var_os("OUT_DIR").expect("OUT_DIR to be set in build.rs");
let out_dir = Path::new(&out_dir_env);
std::fs::create_dir_all(out_dir.join("assets/manual")).unwrap();
std::fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
template(
&variables,
"assets/manual/bat.1.in",
out_dir.join("assets/manual/bat.1"),
)?;
template(
&variables,
"assets/completions/bat.fish.in",
out_dir.join("assets/completions/bat.fish"),
)?;
Ok(())
}