diff options
author | sharkdp <davidpeter@web.de> | 2020-09-20 19:50:39 +0200 |
---|---|---|
committer | sharkdp <davidpeter@web.de> | 2020-09-20 19:50:39 +0200 |
commit | f18009e5d57bb4ee03511a9a2b6fd933d58d539f (patch) | |
tree | cbdeae659febe3e103d85496743a053537ccc099 /build.rs | |
parent | 83c775065657dfc5bbaa83060f073a83d7fec911 (diff) |
Remove 'liquid' dependency
Diffstat (limited to 'build.rs')
-rw-r--r-- | build.rs | 28 |
1 files changed, 17 insertions, 11 deletions
@@ -8,6 +8,7 @@ fn main() {} #[cfg(feature = "application")] fn main() -> Result<(), Box<dyn std::error::Error>> { + use std::collections::HashMap; use std::error::Error; use std::fs; use std::path::Path; @@ -15,27 +16,32 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { // 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 liquid template. + /// Generates a file from a template. fn template( - variables: &liquid::Object, + variables: &HashMap<&str, &str>, in_file: &str, out_file: impl AsRef<Path>, ) -> Result<(), Box<dyn Error>> { - let template = liquid::ParserBuilder::with_stdlib() - .build()? - .parse(&fs::read_to_string(in_file)?)?; + let mut content = fs::read_to_string(in_file)?; - fs::write(out_file, template.render(variables)?)?; + 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 variables = liquid::object!({ - "PROJECT_NAME": project_name, - "PROJECT_EXECUTABLE": executable_name, - "PROJECT_VERSION": PROJECT_VERSION, - }); + let mut variables = HashMap::new(); + variables.insert("PROJECT_NAME", project_name); + variables.insert("PROJECT_EXECUTABLE", executable_name); + variables.insert("PROJECT_EXECUTABLE_UPPERCASE", &executable_name_uppercase); + variables.insert("PROJECT_VERSION", PROJECT_VERSION); let out_dir_env = std::env::var_os("OUT_DIR").expect("OUT_DIR to be set in build.rs"); let out_dir = Path::new(&out_dir_env); |