cli/installer/
mod.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! # 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::error::CargoMakeError;
use crate::scriptengine;
use crate::types::{FlowInfo, FlowState, InstallCrate, Task};
use std::cell::RefCell;
use std::rc::Rc;

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,
    }
}

fn get_first_command_arg(task_config: &Task) -> Option<String> {
    match task_config.args {
        Some(ref args) => {
            if args.is_empty() {
                None
            } else {
                Some(args[0].to_string())
            }
        }
        None => None,
    }
}

pub(crate) fn install(
    task_config: &Task,
    flow_info: &FlowInfo,
    flow_state: Rc<RefCell<FlowState>>,
) -> Result<(), CargoMakeError> {
    if let Some(disable_install) = flow_info.config.config.disable_install {
        if disable_install {
            return Ok(());
        }
    }

    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 first_arg = get_first_command_arg(task_config);
                match first_arg {
                    Some(ref cargo_command) => cargo_plugin_installer::install_crate(
                        &toolchain,
                        Some(cargo_command),
                        crate_name,
                        &task_config.install_crate_args,
                        validate,
                        &None,
                        &None,
                        &None,
                    ),
                    None => cargo_plugin_installer::install_crate(
                        &toolchain,
                        None,
                        crate_name,
                        &task_config.install_crate_args,
                        validate,
                        &None,
                        &None,
                        &Some(false), // we can't validate, so we do not allow force
                    ),
                }?;
            }
            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 get_first_command_arg(task_config) {
                            Some(arg) => match install_info.crate_name {
                                Some(ref crate_name) => (arg, crate_name.to_string()),
                                None => {
                                    error!("Missing crate name to invoke.");
                                    return Err(CargoMakeError::NotFound(String::from(
                                        "Missing crate name to invoke.",
                                    )));
                                }
                            },
                            None => match install_info.crate_name {
                                Some(ref crate_name) => {
                                    (crate_name.to_string(), crate_name.to_string())
                                }
                                None => {
                                    error!("Missing cargo command to invoke.");
                                    return Err(CargoMakeError::NotFound(String::from(
                                        "Missing crate command to invoke.",
                                    )));
                                }
                            },
                        },
                    };

                cargo_plugin_installer::install_crate(
                    &toolchain,
                    Some(&cargo_command),
                    &crate_name,
                    &task_config.install_crate_args,
                    validate,
                    &install_info.min_version,
                    &install_info.install_command,
                    &install_info.force,
                )?;
            }
            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),
                    Some(flow_state),
                )?;
                ()
            }
            None => match get_cargo_plugin_info_from_command(&task_config) {
                Some((cargo_command, crate_name)) => {
                    cargo_plugin_installer::install_crate(
                        &toolchain,
                        Some(&cargo_command),
                        &crate_name,
                        &task_config.install_crate_args,
                        validate,
                        &None,
                        &None,
                        &None,
                    )?;
                }
                None => debug!("No installation script defined."),
            },
        },
    };
    Ok(())
}