Skip to content

Commit

Permalink
Reorganise build script into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
cyqsimon authored and Enselic committed Nov 2, 2023
1 parent f3a5e9a commit 79a03b4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
33 changes: 3 additions & 30 deletions build.rs → build/application.rs
Original file line number Diff line number Diff line change
@@ -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());
Expand Down Expand Up @@ -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(())
}
10 changes: 10 additions & 0 deletions build/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[cfg(feature = "application")]
mod application;
mod util;

fn main() -> anyhow::Result<()> {
#[cfg(feature = "application")]
application::gen_man_and_comp()?;

Ok(())
}
21 changes: 21 additions & 0 deletions build/util.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 79a03b4

Please sign in to comment.