mirror of
https://github.com/sharkdp/bat.git
synced 2025-02-10 15:00:13 +01:00
Reorganise functions in build script
This commit is contained in:
parent
28d947fd8b
commit
b1577cc083
64
build.rs
64
build.rs
@ -1,38 +1,21 @@
|
|||||||
// For bat-as-a-library, no build script is required. The build script is for
|
use std::{collections::HashMap, fs, path::Path};
|
||||||
// the manpage and completions, which are only relevant to the bat application.
|
|
||||||
#[cfg(not(feature = "application"))]
|
|
||||||
fn main() {}
|
|
||||||
|
|
||||||
#[cfg(feature = "application")]
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
use std::collections::HashMap;
|
#[cfg(feature = "application")]
|
||||||
use std::fs;
|
gen_man_and_comp()?;
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generate manpage and shell completions for the bat application.
|
||||||
|
#[cfg(feature = "application")]
|
||||||
|
fn gen_man_and_comp() -> anyhow::Result<()> {
|
||||||
// Read environment variables.
|
// Read environment variables.
|
||||||
let project_name = option_env!("PROJECT_NAME").unwrap_or("bat");
|
let project_name = option_env!("PROJECT_NAME").unwrap_or("bat");
|
||||||
let executable_name = option_env!("PROJECT_EXECUTABLE").unwrap_or(project_name);
|
let executable_name = option_env!("PROJECT_EXECUTABLE").unwrap_or(project_name);
|
||||||
let executable_name_uppercase = executable_name.to_uppercase();
|
let executable_name_uppercase = executable_name.to_uppercase();
|
||||||
static PROJECT_VERSION: &str = env!("CARGO_PKG_VERSION");
|
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<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}}}}}", variable_name = variable_name);
|
|
||||||
content = content.replace(&pattern, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
fs::write(out_file, content)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut variables = HashMap::new();
|
let mut variables = HashMap::new();
|
||||||
variables.insert("PROJECT_NAME", project_name);
|
variables.insert("PROJECT_NAME", project_name);
|
||||||
variables.insert("PROJECT_EXECUTABLE", executable_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/manual")).unwrap();
|
||||||
fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
|
fs::create_dir_all(out_dir.join("assets/completions")).unwrap();
|
||||||
|
|
||||||
template(
|
render_template(
|
||||||
&variables,
|
&variables,
|
||||||
"assets/manual/bat.1.in",
|
"assets/manual/bat.1.in",
|
||||||
out_dir.join("assets/manual/bat.1"),
|
out_dir.join("assets/manual/bat.1"),
|
||||||
)?;
|
)?;
|
||||||
template(
|
render_template(
|
||||||
&variables,
|
&variables,
|
||||||
"assets/completions/bat.bash.in",
|
"assets/completions/bat.bash.in",
|
||||||
out_dir.join("assets/completions/bat.bash"),
|
out_dir.join("assets/completions/bat.bash"),
|
||||||
)?;
|
)?;
|
||||||
template(
|
render_template(
|
||||||
&variables,
|
&variables,
|
||||||
"assets/completions/bat.fish.in",
|
"assets/completions/bat.fish.in",
|
||||||
out_dir.join("assets/completions/bat.fish"),
|
out_dir.join("assets/completions/bat.fish"),
|
||||||
)?;
|
)?;
|
||||||
template(
|
render_template(
|
||||||
&variables,
|
&variables,
|
||||||
"assets/completions/_bat.ps1.in",
|
"assets/completions/_bat.ps1.in",
|
||||||
out_dir.join("assets/completions/_bat.ps1"),
|
out_dir.join("assets/completions/_bat.ps1"),
|
||||||
)?;
|
)?;
|
||||||
template(
|
render_template(
|
||||||
&variables,
|
&variables,
|
||||||
"assets/completions/bat.zsh.in",
|
"assets/completions/bat.zsh.in",
|
||||||
out_dir.join("assets/completions/bat.zsh"),
|
out_dir.join("assets/completions/bat.zsh"),
|
||||||
@ -77,3 +60,22 @@ fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Generates a file from a template.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn render_template(
|
||||||
|
variables: &HashMap<&str, &str>,
|
||||||
|
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}}}}}", variable_name = variable_name);
|
||||||
|
content = content.replace(&pattern, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
fs::write(out_file, content)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user