Add support for env variable

This commit is contained in:
Raamakrishnan 2020-01-28 22:37:55 +05:30
parent 81331461d4
commit f8086d39aa
5 changed files with 86 additions and 1 deletions

52
Cargo.lock generated
View file

@ -1,6 +1,58 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "aho-corasick"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "regex"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "sv-filelist-parser"
version = "0.1.0"
dependencies = [
"regex 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[metadata]
"checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d"
"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
"checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223"
"checksum regex 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b5508c1941e4e7cb19965abef075d35a9a8b5cdf0846f30b4050e9b55dc55e87"
"checksum regex-syntax 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "e734e891f5b408a29efbf8309e656876276f49ab6a6ac208600b4419bd893d90"
"checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"

View file

@ -7,3 +7,4 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.0"

View file

@ -1,6 +1,7 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use regex::Regex;
use crate::line_parser;
use crate::line_parser::LineType;
@ -37,7 +38,8 @@ pub fn parse_file(path: &str) -> Result<Filelist, Box<dyn Error>> {
let mut filelist = Filelist::new();
for line in contents.lines() {
match line_parser::parse_line(line) {
let line = replace_env_vars(&line);
match line_parser::parse_line(&line) {
LineType::File(file) => filelist.files.push(file.to_string()),
LineType::Define(define_map) => {
for define in define_map.into_iter() {
@ -57,3 +59,23 @@ pub fn parse_file(path: &str) -> Result<Filelist, Box<dyn Error>> {
}
Ok(filelist)
}
fn replace_env_vars(line: &str) -> String {
let re_env_brace = Regex::new(r"\$\{(?P<env>[^}]+)\}").unwrap();
let re_env_paren = Regex::new(r"\$\((?P<env>[^)]+)\)").unwrap();
let mut expanded_line = String::from(line);
for caps in re_env_brace.captures_iter(&line) {
let env = &caps["env"];
if let Ok(env_var) = std::env::var(env) {
expanded_line = expanded_line.replace(&format!("${{{}}}", env), &env_var);
}
}
for caps in re_env_paren.captures_iter(&line) {
let env = &caps["env"];
if let Ok(env_var) = std::env::var(env) {
expanded_line = expanded_line.replace(&format!("$({})", env), &env_var);
}
}
expanded_line
}

View file

@ -4,4 +4,6 @@ testcase/file3.sv
+incdir+testcase/
// Some comment
+define+a=b+c=d+e=f
+define+$(VAR1)=var1
+define+${VAR2}=var2
-f testcase/files2.f

View file

@ -7,6 +7,9 @@ fn simple_test() {
defines.insert("a".to_string(), "bad".to_string());
defines.insert("e".to_string(), "f".to_string());
defines.insert("c".to_string(), "d".to_string());
defines.insert("ENV_VAR1".to_string(), "var1".to_string());
defines.insert("ENV_VAR2".to_string(), "var2".to_string());
let filelist_exp = sv_filelist_parser::Filelist {
files : vec!["testcase/file1.sv".to_string(),
@ -17,6 +20,11 @@ fn simple_test() {
defines : defines,
comments_present : true
};
// Add env vars
std::env::set_var("VAR1", "ENV_VAR1");
std::env::set_var("VAR2", "ENV_VAR2");
let filelist = sv_filelist_parser::parse("testcase/files.f")
.expect("Error parsing");
assert_eq!(filelist_exp, filelist);