diff options
author | cyqsimon <28627918+cyqsimon@users.noreply.github.com> | 2023-10-30 17:49:50 +0800 |
---|---|---|
committer | Martin Nordholts <enselic@gmail.com> | 2023-11-02 17:54:57 +0100 |
commit | 79a03b4299556a67ef7f3f40bda71db52535cf1a (patch) | |
tree | dc9b74500f3b2209b568c4da4f1c86e38d3ee325 | |
parent | f3a5e9a73cb7c1fc5d276b3b7a383ecccaeeb2c8 (diff) |
Reorganise build script into modules
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | build/application.rs (renamed from build.rs) | 33 | ||||
-rw-r--r-- | build/main.rs | 10 | ||||
-rw-r--r-- | build/util.rs | 21 |
4 files changed, 35 insertions, 31 deletions
@@ -8,7 +8,7 @@ name = "bat" repository = "https://github.com/sharkdp/bat" version = "0.24.0" exclude = ["assets/syntaxes/*", "assets/themes/*"] -build = "build.rs" +build = "build/main.rs" edition = '2021' rust-version = "1.70" diff --git a/build.rs b/build/application.rs index db3cd29c..4a6dc42f 100644 --- a/build.rs +++ b/build/application.rs @@ -1,17 +1,9 @@ -use std::{collections::HashMap, fs, path::Path}; +use std::{env, fs, path::PathBuf}; -fn main() -> anyhow::Result<()> { - #[cfg(feature = "application")] - gen_man_and_comp()?; - - Ok(()) -} +use crate::util::render_template; /// Generate manpage and shell completions for the bat application. -#[cfg(feature = "application")] -fn gen_man_and_comp() -> anyhow::Result<()> { - use std::{env, path::PathBuf}; - +pub fn gen_man_and_comp() -> anyhow::Result<()> { // Read environment variables. let project_name = env::var("PROJECT_NAME").unwrap_or("bat".into()); let executable_name = env::var("PROJECT_EXECUTABLE").unwrap_or(project_name.clone()); @@ -65,22 +57,3 @@ fn gen_man_and_comp() -> anyhow::Result<()> { Ok(()) } - -/// Generates a file from a template. -#[allow(dead_code)] -fn render_template( - variables: &HashMap<&str, String>, - in_file: &str, - out_file: impl AsRef<Path>, -) -> 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}}}}}"); - content = content.replace(&pattern, value); - } - - fs::write(out_file, content)?; - Ok(()) -} diff --git a/build/main.rs b/build/main.rs new file mode 100644 index 00000000..5d33418b --- /dev/null +++ b/build/main.rs @@ -0,0 +1,10 @@ +#[cfg(feature = "application")] +mod application; +mod util; + +fn main() -> anyhow::Result<()> { + #[cfg(feature = "application")] + application::gen_man_and_comp()?; + + Ok(()) +} diff --git a/build/util.rs b/build/util.rs new file mode 100644 index 00000000..daae52ba --- /dev/null +++ b/build/util.rs @@ -0,0 +1,21 @@ +#![allow(dead_code)] + +use std::{collections::HashMap, fs, path::Path}; + +/// Generates a file from a template. +pub fn render_template( + variables: &HashMap<&str, String>, + in_file: &str, + out_file: impl AsRef<Path>, +) -> 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}}}}}"); + content = content.replace(&pattern, value); + } + + fs::write(out_file, content)?; + Ok(()) +} |