sagiegurari / cargo-make
1
//! # rustup_component_installer
2
//!
3
//! Installs rustup components.
4
//!
5

6
#[cfg(test)]
7
#[path = "./rustup_component_installer_test.rs"]
8
mod rustup_component_installer_test;
9

10
use crate::command;
11
use crate::toolchain::wrap_command;
12
use crate::types::InstallRustupComponentInfo;
13
use std::process::Command;
14

15 8
pub(crate) fn is_installed(toolchain: &Option<String>, binary: &str, test_args: &[String]) -> bool {
16 8
    let mut command_struct = match toolchain {
17 8
        Some(ref toolchain_string) => {
18 8
            let command_spec = wrap_command(toolchain_string, binary, &None);
19 8
            let mut cmd = Command::new(command_spec.command);
20

21 8
            let args_vec = command_spec.args.unwrap();
22 8
            for arg in args_vec.iter() {
23 8
                cmd.arg(arg);
24
            }
25

26 8
            cmd
27 8
        }
28 8
        None => Command::new(binary),
29
    };
30

31 8
    let result = command_struct.args(test_args).output();
32

33 8
    match result {
34 8
        Ok(output) => {
35 8
            let exit_code = command::get_exit_code(Ok(output.status), false);
36

37 8
            if exit_code != 0 {
38 2
                false
39
            } else {
40 8
                true
41
            }
42 6
        }
43 8
        Err(error) => {
44 8
            debug!(
45 0
                "Unable to check if crate is installed: {} {:#?}",
46 0
                binary, &error
47
            );
48 8
            false
49 6
        }
50 2
    }
51 8
}
52

53 8
pub(crate) fn invoke_rustup_install(
54
    toolchain: &Option<String>,
55
    info: &InstallRustupComponentInfo,
56
) -> bool {
57 8
    let mut command_spec = Command::new("rustup");
58 8
    command_spec.arg("component");
59 8
    command_spec.arg("add");
60

61 8
    match toolchain {
62 8
        Some(ref toolchain_string) => {
63 8
            command_spec.arg("--toolchain");
64 8
            command_spec.arg(toolchain_string);
65
        }
66
        None => {}
67
    };
68

69 8
    let result = command_spec.arg(&info.rustup_component_name).output();
70

71 8
    match result {
72 8
        Ok(output) => {
73 8
            let exit_code = command::get_exit_code(Ok(output.status), false);
74

75 8
            if exit_code != 0 {
76 8
                debug!(
77 0
                    "Failed to add component: {} via rustup",
78 0
                    &info.rustup_component_name
79
                );
80

81 8
                false
82
            } else {
83 0
                debug!(
84 0
                    "Component: {} added via rustup",
85 0
                    &info.rustup_component_name
86
                );
87

88 0
                true
89
            }
90 6
        }
91 2
        Err(error) => {
92 0
            debug!(
93 0
                "Failed to add component: {} via rustup, error: {:#?}",
94 0
                &info.rustup_component_name, &error
95
            );
96 0
            false
97 0
        }
98 0
    }
99 8
}
100

101 8
pub(crate) fn install(
102
    toolchain: &Option<String>,
103
    info: &InstallRustupComponentInfo,
104
    validate: bool,
105
) -> bool {
106 8
    let mut installed = match info.binary {
107 8
        Some(ref binary) => match info.test_arg {
108 8
            Some(ref test_arg) => is_installed(&toolchain, binary, test_arg),
109 0
            None => false,
110
        },
111 0
        None => false,
112
    };
113

114 8
    if !installed {
115 8
        debug!(
116 0
            "Rustup Component: {} not installed.",
117 0
            &info.rustup_component_name
118
        );
119

120 8
        installed = invoke_rustup_install(&toolchain, &info);
121

122 8
        if validate && !installed {
123 0
            error!(
124 0
                "Failed to add rustup component: {}",
125 0
                &info.rustup_component_name
126
            );
127
        }
128

129 8
        installed
130
    } else {
131 8
        true
132
    }
133 8
}

Read our documentation on viewing source code .

Loading