mirror of
https://github.com/sharkdp/bat.git
synced 2024-11-27 02:03:52 +01:00
Reorganise build script into modules
This commit is contained in:
parent
f3a5e9a73c
commit
79a03b4299
@ -8,7 +8,7 @@ name = "bat"
|
|||||||
repository = "https://github.com/sharkdp/bat"
|
repository = "https://github.com/sharkdp/bat"
|
||||||
version = "0.24.0"
|
version = "0.24.0"
|
||||||
exclude = ["assets/syntaxes/*", "assets/themes/*"]
|
exclude = ["assets/syntaxes/*", "assets/themes/*"]
|
||||||
build = "build.rs"
|
build = "build/main.rs"
|
||||||
edition = '2021'
|
edition = '2021'
|
||||||
rust-version = "1.70"
|
rust-version = "1.70"
|
||||||
|
|
||||||
|
@ -1,17 +1,9 @@
|
|||||||
use std::{collections::HashMap, fs, path::Path};
|
use std::{env, fs, path::PathBuf};
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
use crate::util::render_template;
|
||||||
#[cfg(feature = "application")]
|
|
||||||
gen_man_and_comp()?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate manpage and shell completions for the bat application.
|
/// Generate manpage and shell completions for the bat application.
|
||||||
#[cfg(feature = "application")]
|
pub fn gen_man_and_comp() -> anyhow::Result<()> {
|
||||||
fn gen_man_and_comp() -> anyhow::Result<()> {
|
|
||||||
use std::{env, path::PathBuf};
|
|
||||||
|
|
||||||
// Read environment variables.
|
// Read environment variables.
|
||||||
let project_name = env::var("PROJECT_NAME").unwrap_or("bat".into());
|
let project_name = env::var("PROJECT_NAME").unwrap_or("bat".into());
|
||||||
let executable_name = env::var("PROJECT_EXECUTABLE").unwrap_or(project_name.clone());
|
let executable_name = env::var("PROJECT_EXECUTABLE").unwrap_or(project_name.clone());
|
||||||
@ -65,22 +57,3 @@ fn gen_man_and_comp() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
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
build/main.rs
Normal file
10
build/main.rs
Normal file
@ -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
build/util.rs
Normal file
21
build/util.rs
Normal file
@ -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(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user