sagiegurari / cargo-make
1
//! # crate_installer
2
//!
3
//! Installs crates via rustup/cargo.
4
//!
5

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

10
use crate::command;
11
use crate::installer::crate_version_check;
12
use crate::installer::{cargo_plugin_installer, rustup_component_installer};
13
use crate::toolchain::wrap_command;
14
use crate::types::{CommandSpec, InstallCrateInfo, InstallRustupComponentInfo};
15

16 8
fn invoke_rustup_install(toolchain: &Option<String>, info: &InstallCrateInfo) -> bool {
17 8
    match info.rustup_component_name {
18 8
        Some(ref component) => {
19 8
            let rustup_component_info = InstallRustupComponentInfo {
20 8
                rustup_component_name: component.to_string(),
21 8
                binary: Some(info.binary.clone()),
22 8
                test_arg: Some(info.test_arg.clone()),
23 0
            };
24 8
            rustup_component_installer::invoke_rustup_install(&toolchain, &rustup_component_info)
25 8
        }
26 8
        None => false,
27
    }
28 8
}
29

30 8
fn invoke_cargo_install(
31
    toolchain: &Option<String>,
32
    info: &InstallCrateInfo,
33
    args: &Option<Vec<String>>,
34
    validate: bool,
35
) {
36 8
    let install_args = cargo_plugin_installer::get_install_crate_args(
37 8
        &info.crate_name,
38
        true,
39 8
        &args,
40 8
        &info.min_version,
41
    );
42

43 8
    let command_spec = match toolchain {
44 8
        Some(ref toolchain_string) => wrap_command(toolchain_string, "cargo", &Some(install_args)),
45 8
        None => CommandSpec {
46 8
            command: "cargo".to_string(),
47 8
            args: Some(install_args),
48
        },
49
    };
50

51 8
    command::run_command(&command_spec.command, &command_spec.args, validate);
52 2
}
53

54 8
fn is_crate_only_info(info: &InstallCrateInfo) -> bool {
55 8
    match info.rustup_component_name {
56 8
        Some(_) => false,
57 8
        None => true,
58
    }
59 8
}
60

61 8
pub(crate) fn install(
62
    toolchain: &Option<String>,
63
    info: &InstallCrateInfo,
64
    args: &Option<Vec<String>>,
65
    validate: bool,
66
) {
67
    let installed =
68 8
        rustup_component_installer::is_installed(&toolchain, &info.binary, &info.test_arg);
69 8
    let crate_only_info = is_crate_only_info(&info);
70 8
    let run_installation = if !installed {
71 8
        true
72 8
    } else if crate_only_info && toolchain.is_none() {
73 8
        match info.min_version {
74 8
            Some(ref version) => {
75 8
                !crate_version_check::is_min_version_valid(&info.crate_name, version)
76
            }
77 8
            None => false,
78
        }
79
    } else {
80 0
        false
81
    };
82

83 8
    if run_installation {
84 8
        debug!("Crate: {} not installed.", &info.crate_name);
85

86 8
        if !invoke_rustup_install(&toolchain, &info) {
87 8
            invoke_cargo_install(&toolchain, &info, &args, validate);
88
        }
89
    }
90 8
}

Read our documentation on viewing source code .

Loading