use std::fs;
use std::path::{Path, PathBuf};
fn minify_json(json: &str) -> String {
let mut result = String::with_capacity(json.len());
let mut in_string = false;
let mut previous_char: Option<char> = None;
for c in json.chars() {
if in_string {
result.push(c);
if c == '"' && previous_char != Some('\\') {
in_string = false;
}
} else {
match c {
'"' => {
result.push(c);
in_string = true;
}
' ' | '\n' | '\r' | '\t' => continue, _ => result.push(c),
}
}
previous_char = Some(c);
}
result
}
fn update_proxy_abi_decl_with_file(source_file_path: &Path, minified_json: &str) {
let mut source_code = fs::read_to_string(source_file_path).expect("Unable to read source file");
let escaped_json = minified_json.replace('\\', "\\\\").replace('"', "\\\"");
let new_abigen = format!(
"abigen!(Contract(name = \"ProxyContract\", abi = \"{}\",));",
escaped_json
);
let re = regex::Regex::new(r#"abigen!\(Contract\(name = "ProxyContract", abi = ".*?",\)\);"#)
.expect("Invalid regex pattern");
if re.is_match(&source_code) {
source_code = re.replace(&source_code, new_abigen.as_str()).to_string();
} else {
panic!("abigen! macro not found in the source file");
}
fs::write(source_file_path, source_code).expect("Unable to write back to the source file");
}
fn main() {
let json_path = PathBuf::from("proxy_abi/proxy_contract-abi.json");
let json_content =
fs::read_to_string(json_path).expect("Unable to read proxy_contract-abi.json");
let minified_json = minify_json(&json_content);
println!("cargo:rerun-if-changed=proxy_abi/proxy_contract-abi.json");
let util_tx_path = PathBuf::from("src/util/tx.rs");
update_proxy_abi_decl_with_file(&util_tx_path, &minified_json);
let test_path = PathBuf::from("tests/deploy.rs");
update_proxy_abi_decl_with_file(&test_path, &minified_json);
let deploy_path = PathBuf::from("src/op/deploy.rs");
update_proxy_abi_decl_with_file(&deploy_path, &minified_json);
}