49 lines
1.8 KiB
Rust
49 lines
1.8 KiB
Rust
use std::path::Path;
|
|
|
|
const KEYS: [&str; 3] = ["GRID_USER", "GRID_PASSWORD", "GRID_LOGIN_URL"];
|
|
const LIVE_OPT_IN: &str = "RUN_LIVE_TESTS";
|
|
|
|
fn dotenv_has(path: &Path, key: &str) -> bool {
|
|
std::fs::read_to_string(path).is_ok_and(|contents| {
|
|
contents.lines().any(|line| {
|
|
let line = line.trim().strip_prefix("export ").unwrap_or(line.trim());
|
|
line.split_once('=').is_some_and(|(name, value)| {
|
|
name.trim() == key && !value.trim().trim_matches(['\'', '"']).is_empty()
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
fn main() {
|
|
let dotenv = Path::new(&std::env::var("CARGO_MANIFEST_DIR").expect("manifest directory"))
|
|
.join("../..")
|
|
.join(".env");
|
|
println!("cargo:rustc-check-cfg=cfg(live_grid_credentials)");
|
|
// A missing rerun-if-changed input is perpetually stale to Cargo. CI and
|
|
// normal offline builds do not have this ignored credential file, so only
|
|
// watch it once it exists. RUN_LIVE_TESTS remains an unconditional watched
|
|
// input and therefore makes the documented opt-in rebuild discover a newly
|
|
// created .env file.
|
|
if dotenv.is_file() {
|
|
println!("cargo:rerun-if-changed={}", dotenv.display());
|
|
}
|
|
for key in KEYS {
|
|
println!("cargo:rerun-if-env-changed={key}");
|
|
}
|
|
println!("cargo:rerun-if-env-changed={LIVE_OPT_IN}");
|
|
let opted_in = std::env::var(LIVE_OPT_IN).is_ok_and(|value| {
|
|
matches!(
|
|
value.trim().to_ascii_lowercase().as_str(),
|
|
"1" | "true" | "yes"
|
|
)
|
|
});
|
|
if opted_in
|
|
&& KEYS.iter().all(|key| {
|
|
std::env::var(key).is_ok_and(|value| !value.trim().is_empty())
|
|
|| dotenv_has(&dotenv, key)
|
|
})
|
|
{
|
|
println!("cargo:rustc-cfg=live_grid_credentials");
|
|
}
|
|
}
|