From b1577cc08322effa41cb4e345b470e66bb825ca4 Mon Sep 17 00:00:00 2001 From: cyqsimon <28627918+cyqsimon@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:37:48 +0800 Subject: [PATCH] Reorganise functions in build script --- build.rs | 64 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/build.rs b/build.rs index 43543904..7a90ca01 100644 --- a/build.rs +++ b/build.rs @@ -1,38 +1,21 @@ -// For bat-as-a-library, no build script is required. The build script is for -// the manpage and completions, which are only relevant to the bat application. -#[cfg(not(feature = "application"))] -fn main() {} +use std::{collections::HashMap, fs, path::Path}; -#[cfg(feature = "application")] fn main() -> anyhow::Result<()> { - use std::collections::HashMap; - use std::fs; - use std::path::Path; + #[cfg(feature = "application")] + gen_man_and_comp()?; + Ok(()) +} + +/// Generate manpage and shell completions for the bat application. +#[cfg(feature = "application")] +fn gen_man_and_comp() -> anyhow::Result<()> { // Read environment variables. let project_name = option_env!("PROJECT_NAME").unwrap_or("bat"); let executable_name = option_env!("PROJECT_EXECUTABLE").unwrap_or(project_name); let executable_name_uppercase = executable_name.to_uppercase(); static PROJECT_VERSION: &str = env!("CARGO_PKG_VERSION"); - /// Generates a file from a template. - fn template( - variables: &HashMap<&str, &str>, - in_file: &str, - out_file: impl AsRef, - ) -> anyhow::Result<()> { - let mut content = fs::read_to_string(in_file)?; - - for (variable_name, value) in variables { - // Replace {{variable_name}} by the value - let pattern = format!("{{{{{variable_name}}}}}", variable_name = variable_name); - content = content.replace(&pattern, value); - } - - fs::write(out_file, content)?; - Ok(()) - } - let mut variables = HashMap::new(); variables.insert("PROJECT_NAME", project_name); variables.insert("PROJECT_EXECUTABLE", executable_name); @@ -49,27 +32,27 @@ fn main() -> anyhow::Result<()> { fs::create_dir_all(out_dir.join("assets/manual")).unwrap(); fs::create_dir_all(out_dir.join("assets/completions")).unwrap(); - template( + render_template( &variables, "assets/manual/bat.1.in", out_dir.join("assets/manual/bat.1"), )?; - template( + render_template( &variables, "assets/completions/bat.bash.in", out_dir.join("assets/completions/bat.bash"), )?; - template( + render_template( &variables, "assets/completions/bat.fish.in", out_dir.join("assets/completions/bat.fish"), )?; - template( + render_template( &variables, "assets/completions/_bat.ps1.in", out_dir.join("assets/completions/_bat.ps1"), )?; - template( + render_template( &variables, "assets/completions/bat.zsh.in", out_dir.join("assets/completions/bat.zsh"), @@ -77,3 +60,22 @@ fn main() -> anyhow::Result<()> { Ok(()) } + +/// Generates a file from a template. +#[allow(dead_code)] +fn render_template( + variables: &HashMap<&str, &str>, + in_file: &str, + out_file: impl AsRef, +) -> anyhow::Result<()> { + let mut content = fs::read_to_string(in_file)?; + + for (variable_name, value) in variables { + // Replace {{variable_name}} by the value + let pattern = format!("{{{{{variable_name}}}}}", variable_name = variable_name); + content = content.replace(&pattern, value); + } + + fs::write(out_file, content)?; + Ok(()) +}