~starkingdoms/starkingdoms

ref: f85bb4de01556b03b4b711345acb2bcf3c370502 starkingdoms/crates/xtask/src/util.rs -rw-r--r-- 2.1 KiB
f85bb4deghostly_zsh ship editor fix: thruster attachment and connected part sprite an hour ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::path::{Path, PathBuf};
use std::process::exit;
use colored::Colorize;

pub fn workspace_dir() -> PathBuf {
    let output = std::process::Command::new(env!("CARGO"))
        .arg("locate-project")
        .arg("--workspace")
        .arg("--message-format=plain")
        .output()
        .unwrap()
        .stdout;
    let cargo_path = Path::new(std::str::from_utf8(&output).unwrap().trim());
    cargo_path.parent().unwrap().to_path_buf()
}

pub fn cargo(cmd: String) {
    println!("{} cargo {}", "[cargo]".bold().cyan(), cmd);
    let mut output = std::process::Command::new(env!("CARGO"));

    for (key, _) in std::env::vars_os() {
        let Some(key) = key.to_str() else { continue };
        if SANITIZED_ENV_VARS.matches(key) {
            output.env_remove(key);
        }
    }

    for command in cmd.split(" ") {
        output.arg(command);
    }
    let output = output.spawn().unwrap().wait().unwrap();

    if !output.success() {
        println!("{}", "============ TASK FAILED".bold().red());
        exit(1);
    }
}

#[derive(Debug)]
struct SanitizedEnvVars {
    // At the moment we only ban some prefixes, but we may also want to ban env
    // vars by exact name in the future.
    prefixes: &'static [&'static str],
}

impl SanitizedEnvVars {
    const fn new() -> Self {
        // Remove many of the environment variables set in
        // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts.
        // This is done to avoid recompilation with crates like ring between
        // `cargo clippy` and `cargo xtask clippy`. (This is really a bug in
        // both ring's build script and in Cargo.)
        //
        // The current list is informed by looking at ring's build script, so
        // it's not guaranteed to be exhaustive and it may need to grow over
        // time.
        let prefixes = &["CARGO_PKG_", "CARGO_MANIFEST_", "CARGO_CFG_"];
        Self { prefixes }
    }

    fn matches(&self, key: &str) -> bool {
        self.prefixes.iter().any(|prefix| key.starts_with(prefix))
    }
}

static SANITIZED_ENV_VARS: SanitizedEnvVars = SanitizedEnvVars::new();