tracel_xtask/utils/
rustup.rs

1use std::process::{Command, Stdio};
2
3use crate::{endgroup, group, utils::process::run_process};
4
5/// Add a Rust target
6pub fn rustup_add_target(target: &str) -> anyhow::Result<()> {
7    group!("Rustup: add target {}", target);
8    run_process(
9        "rustup",
10        &["target", "add", target],
11        None,
12        None,
13        &format!("Failed to add target {target}"),
14    )?;
15    endgroup!();
16    Ok(())
17}
18
19/// Add a Rust component
20pub fn rustup_add_component(component: &str) -> anyhow::Result<()> {
21    group!("Rustup: add component {}", component);
22    run_process(
23        "rustup",
24        &["component", "add", component],
25        None,
26        None,
27        &format!("Failed to add component {component}"),
28    )?;
29    endgroup!();
30    Ok(())
31}
32
33// Returns the output of the rustup command to get the installed targets
34pub fn rustup_get_installed_targets() -> String {
35    let output = Command::new("rustup")
36        .args(["target", "list", "--installed"])
37        .stdout(Stdio::piped())
38        .output()
39        .expect("Rustup command should execute successfully");
40    String::from_utf8(output.stdout).expect("Output should be valid UTF-8")
41}
42
43/// Returns true if the current toolchain is the nightly
44pub fn is_current_toolchain_nightly() -> bool {
45    let output = Command::new("rustup")
46        .arg("show")
47        .output()
48        .expect("Should get the list of installed Rust toolchains");
49    let output_str = String::from_utf8_lossy(&output.stdout);
50    for line in output_str.lines() {
51        // look for the "rustc.*-nightly" line
52        if line.contains("rustc") && line.contains("-nightly") {
53            return true;
54        }
55    }
56    // assume we are using a stable toolchain if we did not find the nightly compiler
57    false
58}