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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
use std::sync::Arc;

use color_eyre::eyre::{eyre, Result};
use console::style;
use itertools::Itertools;
use rayon::prelude::*;
use rayon::ThreadPoolBuilder;

use crate::cli::args::tool::{ToolArg, ToolArgParser};
use crate::cli::command::Command;
use crate::config::Config;
use crate::config::MissingRuntimeBehavior::AutoInstall;

use crate::output::Output;

use crate::runtime_symlinks;
use crate::shims;
use crate::tool::Tool;
use crate::toolset::{
    ToolVersion, ToolVersionOptions, ToolVersionRequest, Toolset, ToolsetBuilder,
};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::ProgressReport;

/// Install a tool version
///
/// This will install a tool version to `~/.local/share/rtx/installs/<PLUGIN>/<VERSION>`
/// It won't be used simply by being installed, however.
/// For that, you must set up a `.rtx.toml`/`.tool-version` file manually or with `rtx use`.
/// Or you can call a tool version explicitly with `rtx exec <TOOL>@<VERSION> -- <COMMAND>`.
///
/// Tools will be installed in parallel. To disable, set `--jobs=1` or `RTX_JOBS=1`
#[derive(Debug, clap::Args)]
#[clap(visible_alias = "i", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct Install {
    /// Tool(s) to install
    /// e.g.: node@20
    #[clap(value_name="TOOL@VERSION", value_parser = ToolArgParser)]
    tool: Option<Vec<ToolArg>>,

    /// Force reinstall even if already installed
    #[clap(long, short, requires = "tool")]
    force: bool,

    /// Show installation output
    #[clap(long, short, action = clap::ArgAction::Count)]
    verbose: u8,
}

impl Command for Install {
    fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
        config.settings.missing_runtime_behavior = AutoInstall;

        match &self.tool {
            Some(runtime) => self.install_runtimes(config, runtime)?,
            None => self.install_missing_runtimes(config)?,
        }

        Ok(())
    }
}

impl Install {
    fn install_runtimes(&self, mut config: Config, runtimes: &[ToolArg]) -> Result<()> {
        let mpr = MultiProgressReport::new(config.show_progress_bars());
        let ts = ToolsetBuilder::new()
            .with_latest_versions()
            .build(&mut config)?;
        ThreadPoolBuilder::new()
            .num_threads(config.settings.jobs)
            .build()?
            .install(|| -> Result<()> {
                let tool_versions =
                    self.get_requested_tool_versions(&mut config, &ts, runtimes, &mpr)?;
                if tool_versions.is_empty() {
                    warn!("no runtimes to install");
                    warn!("specify a version with `rtx install <PLUGIN>@<VERSION>`");
                    return Ok(());
                }
                self.uninstall_existing_versions(&config, &mpr, &tool_versions)?;
                self.install_requested_versions(&config, &mpr, tool_versions)?;
                shims::reshim(&mut config, &ts)
                    .map_err(|err| eyre!("failed to reshim: {}", err))?;
                runtime_symlinks::rebuild(&config)?;
                Ok(())
            })
    }

    fn get_requested_tool_versions(
        &self,
        config: &mut Config,
        ts: &Toolset,
        runtimes: &[ToolArg],
        mpr: &MultiProgressReport,
    ) -> Result<Vec<(Arc<Tool>, ToolVersion)>> {
        let mut requests = vec![];
        for runtime in ToolArg::double_tool_condition(runtimes) {
            let default_opts = ToolVersionOptions::new();
            match runtime.tvr {
                Some(tv) => requests.push((runtime.plugin, tv, default_opts.clone())),
                None => {
                    if runtime.tvr.is_none() {
                        match ts.versions.get(&runtime.plugin) {
                            Some(tvl) => {
                                for (tvr, opts) in &tvl.requests {
                                    requests.push((
                                        runtime.plugin.clone(),
                                        tvr.clone(),
                                        opts.clone(),
                                    ));
                                }
                            }
                            None => {
                                let tvr = ToolVersionRequest::Version(
                                    runtime.plugin.clone(),
                                    "latest".into(),
                                );
                                requests.push((runtime.plugin, tvr, default_opts.clone()));
                            }
                        }
                    }
                }
            }
        }
        let mut tool_versions = vec![];
        for (plugin_name, tvr, opts) in requests {
            let plugin = config.get_or_create_tool(&plugin_name);
            if !plugin.is_installed() {
                let mut pr = mpr.add();
                if let Err(err) = plugin.install(config, &mut pr, false) {
                    pr.error();
                    return Err(err)?;
                }
            }
            let tv = tvr.resolve(config, &plugin, opts, ts.latest_versions)?;
            tool_versions.push((plugin, tv));
        }
        Ok(tool_versions)
    }

    fn install_missing_runtimes(&self, mut config: Config) -> Result<()> {
        let mut ts = ToolsetBuilder::new()
            .with_latest_versions()
            .build(&mut config)?;
        if ts.list_missing_versions(&config).is_empty() {
            warn!("no runtimes to install");
        }
        let mpr = MultiProgressReport::new(config.show_progress_bars());
        ts.install_missing(&mut config, mpr)?;

        Ok(())
    }

    fn uninstall_existing_versions(
        &self,
        config: &Config,
        mpr: &MultiProgressReport,
        tool_versions: &[(Arc<Tool>, ToolVersion)],
    ) -> Result<()> {
        let already_installed_tool_versions = tool_versions
            .iter()
            .filter(|(t, tv)| t.is_version_installed(tv))
            .map(|(t, tv)| (t, tv.clone()));
        if self.force {
            already_installed_tool_versions
                .par_bridge()
                .map(|(tool, tv)| self.uninstall_version(config, tool, &tv, mpr.add()))
                .collect::<Result<Vec<_>>>()?;
        } else {
            for (_, tv) in already_installed_tool_versions {
                warn!("{} already installed", style(tv).cyan().for_stderr());
            }
        }
        Ok(())
    }
    fn install_requested_versions(
        &self,
        config: &Config,
        mpr: &MultiProgressReport,
        tool_versions: Vec<(Arc<Tool>, ToolVersion)>,
    ) -> Result<()> {
        let grouped_tool_versions: Vec<(Arc<Tool>, Vec<ToolVersion>)> = tool_versions
            .into_iter()
            .filter(|(t, tv)| !t.is_version_installed(tv))
            .group_by(|(t, _)| t.clone())
            .into_iter()
            .map(|(t, tvs)| (t, tvs.map(|(_, tv)| tv).collect()))
            .collect();
        grouped_tool_versions
            .into_par_iter()
            .map(|(tool, versions)| {
                for tv in versions {
                    self.install_version(config, &tool, &tv, mpr.add())?;
                }
                Ok(())
            })
            .collect::<Result<Vec<_>>>()?;
        Ok(())
    }
    fn uninstall_version(
        &self,
        config: &Config,
        tool: &Tool,
        tv: &ToolVersion,
        mut pr: ProgressReport,
    ) -> Result<()> {
        tool.decorate_progress_bar(&mut pr, Some(tv));
        match tool.uninstall_version(config, tv, &pr, false) {
            Ok(_) => {
                pr.finish();
                Ok(())
            }
            Err(err) => {
                pr.error();
                Err(err.wrap_err(format!("failed to uninstall {}", tv)))
            }
        }
    }
    fn install_version(
        &self,
        config: &Config,
        tool: &Tool,
        tv: &ToolVersion,
        mut pr: ProgressReport,
    ) -> Result<()> {
        tool.decorate_progress_bar(&mut pr, Some(tv));
        match tool.install_version(config, tv, &mut pr, self.force) {
            Ok(_) => Ok(()),
            Err(err) => {
                pr.error();
                Err(err.wrap_err(format!("failed to install {}", tv)))
            }
        }
    }
}

static AFTER_LONG_HELP: &str = color_print::cstr!(
    r#"<bold><underline>Examples:</underline></bold>
  $ <bold>rtx install node@20.0.0</bold>  # install specific node version
  $ <bold>rtx install node@20</bold>      # install fuzzy node version
  $ <bold>rtx install node</bold>         # install version specified in .tool-versions or .rtx.toml
  $ <bold>rtx install</bold>                # installs everything specified in .tool-versions or .rtx.toml
"#
);

#[cfg(test)]
mod tests {
    use pretty_assertions::assert_str_eq;

    use crate::{assert_cli, assert_cli_snapshot, dirs};

    #[test]
    fn test_install_force() {
        assert_cli!("install", "-f", "tiny");
    }

    #[test]
    fn test_install_asdf_style() {
        assert_cli!("install", "tiny", "2");
    }

    #[test]
    fn test_install_with_alias() {
        assert_cli!("install", "-f", "tiny@my/alias");
        assert_cli_snapshot!("where", "tiny@my/alias");
    }

    #[test]
    fn test_install_ref() {
        assert_cli!("install", "-f", "dummy@ref:master");
        assert_cli!("global", "dummy@ref:master");
        let output = assert_cli!("where", "dummy");
        assert_str_eq!(
            output.trim(),
            dirs::INSTALLS.join("dummy/ref-master").to_string_lossy()
        );
        assert_cli!("global", "--unset", "dummy");
    }

    #[test]
    fn test_install_nothing() {
        // this doesn't do anything since dummy isn't specified
        assert_cli_snapshot!("install", "dummy");
    }
}