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
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! # installer
//!
//! Installs external dependencies for tasks.<br>
//! There are 2 types of dependencies: install_crate, install_script.<br>
//! install_crate ensures the crate command is available and if not installs the crate based on the provided name.<br>
//! install_script always gets executed before the task command.
//!

pub(crate) mod cargo_plugin_installer;
pub(crate) mod crate_installer;
pub(crate) mod crate_version_check;
pub(crate) mod rustup_component_installer;

#[cfg(test)]
#[path = "mod_test.rs"]
mod mod_test;

use crate::scriptengine;
use crate::types::{FlowInfo, InstallCrate, Task};

fn get_cargo_plugin_info_from_command(task_config: &Task) -> Option<(String, String)> {
    match task_config.command {
        Some(ref command) => {
            if command == "cargo" {
                match task_config.args {
                    Some(ref args) => {
                        if args.len() > 0 && !args[0].starts_with("-") {
                            // create crate name
                            let mut crate_name = "cargo-".to_string();
                            crate_name = crate_name + &args[0];

                            Some((args[0].clone(), crate_name))
                        } else {
                            None
                        }
                    }
                    None => None,
                }
            } else {
                None
            }
        }
        None => None,
    }
}

pub(crate) fn install(task_config: &Task, flow_info: &FlowInfo) {
    let validate = !task_config.should_ignore_errors();

    let toolchain = task_config.toolchain.clone();

    let mut install_crate = task_config.install_crate.clone();
    if let Some(ref install_crate_value) = install_crate {
        if let InstallCrate::Enabled(enabled) = install_crate_value {
            if *enabled {
                // enabled true is the same as no install_crate defined
                install_crate = None;
            }
        }
    }

    match install_crate {
        Some(ref install_crate_info) => match install_crate_info {
            InstallCrate::Enabled(_) => (),
            InstallCrate::Value(ref crate_name) => {
                let cargo_command = match task_config.args {
                    Some(ref args) => &args[0],
                    None => {
                        error!("Missing cargo command to invoke.");
                        panic!("Missing cargo command to invoke.");
                    }
                };

                cargo_plugin_installer::install_crate(
                    &toolchain,
                    cargo_command,
                    crate_name,
                    &task_config.install_crate_args,
                    validate,
                    &None,
                );
            }
            InstallCrate::CargoPluginInfo(ref install_info) => {
                let (cargo_command, crate_name) =
                    match get_cargo_plugin_info_from_command(&task_config) {
                        Some(cargo_plugin_info) => cargo_plugin_info,
                        None => match task_config.args {
                            Some(ref args) => match install_info.crate_name {
                                Some(ref crate_name) => {
                                    (args[0].to_string(), crate_name.to_string())
                                }
                                None => {
                                    error!("Missing crate name to invoke.");
                                    panic!("Missing crate name to invoke.");
                                }
                            },
                            None => {
                                error!("Missing cargo command to invoke.");
                                panic!("Missing cargo command to invoke.");
                            }
                        },
                    };

                cargo_plugin_installer::install_crate(
                    &toolchain,
                    &cargo_command,
                    &crate_name,
                    &task_config.install_crate_args,
                    validate,
                    &Some(install_info.min_version.clone()),
                );
            }
            InstallCrate::CrateInfo(ref install_info) => crate_installer::install(
                &toolchain,
                install_info,
                &task_config.install_crate_args,
                validate,
            ),
            InstallCrate::RustupComponentInfo(ref install_info) => {
                rustup_component_installer::install(&toolchain, install_info, validate);
            }
        },
        None => match task_config.install_script {
            Some(ref script) => {
                scriptengine::invoke_script_in_flow_context(
                    &script,
                    task_config.script_runner.clone(),
                    task_config.script_runner_args.clone(),
                    task_config.script_extension.clone(),
                    validate,
                    Some(flow_info),
                );
                ()
            }
            None => match get_cargo_plugin_info_from_command(&task_config) {
                Some((cargo_command, crate_name)) => {
                    cargo_plugin_installer::install_crate(
                        &toolchain,
                        &cargo_command,
                        &crate_name,
                        &task_config.install_crate_args,
                        validate,
                        &None,
                    );
                }
                None => debug!("No installation script defined."),
            },
        },
    }
}