~starkingdoms/starkingdoms

ref: 6f949473ea4886e5054dc2e360af440501a8a0b2 starkingdoms/crates/xtask/src/util.rs -rw-r--r-- 2.9 KiB
6f949473 — core netcode: fix attachment(2) (i odiot) 4 days 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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();

pub fn wasmopt(cmd: String) {
    println!("{} wasm-opt {}", "[wasm-opt]".bold().cyan(), cmd);
    let mut output = std::process::Command::new("wasm-opt");

    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);
    }
}

pub fn wbg(cmd: String) {
    println!("{} wasm-bindgen {}", "[wasm-bindgen]".bold().cyan(), cmd);
    let mut output = std::process::Command::new("wasm-bindgen");

    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);
    }
}