Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync code with .env in build.rs #4876

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions common/network-defaults/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ default = ["env", "network"]
env = ["dotenvy", "log"]
network = ["schemars", "serde", "url"]
utoipa = [ "dep:utoipa" ]

[build-dependencies]
regex = { workspace = true }
64 changes: 64 additions & 0 deletions common/network-defaults/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use regex::Regex;
use std::{collections::HashMap, fs};

const TARGET_ENV_FILE: &str = "../../envs/mainnet.env";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about this, do you think it's possible to make this relative to the repo root directory? Soon I would like to move the root Rust workspace to a subdirectory to avoid the workspace nesting, and this would then break

Also put in a check that we are writing to the directory we think we are. So that it doesn't try to write the file unless the directory path is correct


/// Sync variable values defined in code with .env file
fn main() {
let file = include_str!("src/mainnet.rs");

let variables_to_track = [
"NETWORK_NAME",
"BECH32_PREFIX",
"MIXNET_CONTRACT_ADDRESS",
"VESTING_CONTRACT_ADDRESS",
"GROUP_CONTRACT_ADDRESS",
"ECASH_CONTRACT_ADDRESS",
"MULTISIG_CONTRACT_ADDRESS",
"COCONUT_DKG_CONTRACT_ADDRESS",
"REWARDING_VALIDATOR_ADDRESS",
"NYM_API",
"NYXD_WS",
"EXPLORER_API",
"NYM_VPN_API",
];

let mut replace_with = HashMap::new();

for var in variables_to_track {
// if script fails, debug with `cargo check -vv``
println!("Looking for {}", var);

// read pattern that looks like:
// <var>: &str = "<whatever is between quotes>"
let pattern = format!(r#"{}: &str\s*=\s*"([^"]*)""#, regex::escape(var));

let re = Regex::new(&pattern).unwrap();
let value = re
.captures(file)
.and_then(|caps| caps.get(1).map(|match_| match_.as_str().to_string()))
.expect("Couldn't find var in source file");
println!("Storing {}={}", var, value);
replace_with.insert(var, value);
}

let mut contents = fs::read_to_string(TARGET_ENV_FILE).unwrap();

for (var, value) in replace_with {
// match a pattern that looks like:
// <var> = "<value>"
// where `<var>` is a variable name inserted into search pattern
let pattern = format!(r#"{}\s*=\s*"([^"]*)""#, regex::escape(var));

// replace matched pattern with
// <var>="<value>"
let re = Regex::new(&pattern).unwrap();
contents = re
.replace(&contents, |_: &regex::Captures| {
format!(r#"{}="{}""#, var, value)
})
.to_string();
}

fs::write(TARGET_ENV_FILE, contents).unwrap();
}
2 changes: 1 addition & 1 deletion envs/mainnet.env
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ NYXD="https://rpc.nymtech.net"
NYM_API="https://validator.nymtech.net/api/"
NYXD_WS="wss://rpc.nymtech.net/websocket"
EXPLORER_API="https://explorer.nymtech.net/api/"
NYM_VPN_API="https://nymvpn.com/api"
NYM_VPN_API="https://nymvpn.net/api/"
Loading