Skip to content

Commit

Permalink
upgrade: fix bug with missing versions
Browse files Browse the repository at this point in the history
If a version was completely missing it would install, then uninstall it
  • Loading branch information
jdx committed Dec 2, 2023
1 parent f25a058 commit ef3bc0d
Showing 1 changed file with 21 additions and 56 deletions.
77 changes: 21 additions & 56 deletions src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use std::sync::Arc;

use eyre::Result;
use eyre::WrapErr;
use itertools::Itertools;
use rayon::prelude::*;
use rayon::ThreadPoolBuilder;

use crate::cli::args::tool::{ToolArg, ToolArgParser};
use crate::config::Config;
Expand Down Expand Up @@ -52,64 +49,33 @@ impl Upgrade {

fn upgrade(&self, config: &mut Config, outdated: OutputVec) -> Result<()> {
let mpr = MultiProgressReport::new(config.show_progress_bars());
ThreadPoolBuilder::new()
.num_threads(config.settings.jobs)
.build()?
.install(|| -> Result<()> {
self.install_new_versions(config, &mpr, outdated)?;
let mut ts = ToolsetBuilder::new().with_args(&self.tool).build(config)?;

Check warning on line 52 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L52

Added line #L52 was not covered by tests

let ts = ToolsetBuilder::new().with_args(&self.tool).build(config)?;
shims::reshim(config, &ts).wrap_err("failed to reshim")?;
runtime_symlinks::rebuild(config)?;

Ok(())
let new_versions = outdated
.iter()
.map(|(_, tv, latest)| {
let mut tv = tv.clone();
tv.version = latest.clone();
tv

Check warning on line 59 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L54-L59

Added lines #L54 - L59 were not covered by tests
})
}
.collect();

Check warning on line 61 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L61

Added line #L61 was not covered by tests

fn install_new_versions(
&self,
config: &Config,
mpr: &MultiProgressReport,
outdated: OutputVec,
) -> Result<()> {
let grouped_tool_versions: GroupedToolVersions = outdated
.into_iter()
.group_by(|(t, _, _)| t.clone())
let to_remove = outdated

Check warning on line 63 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L63

Added line #L63 was not covered by tests
.into_iter()
.map(|(t, tvs)| (t, tvs.map(|(_, tv, latest)| (tv, latest)).collect()))
.collect();
grouped_tool_versions
.into_par_iter()
.map(|(tool, versions)| {
for (tv, latest) in versions {
let mut pr = mpr.add();
self.install_new_version(config, &tool, &tv, latest, &mut pr)?;
self.uninstall_old_version(config, &tool, &tv, &mut pr)?;
}
Ok(())
})
.collect::<Result<Vec<_>>>()?;
Ok(())
}
.filter(|(tool, tv, _)| tool.is_version_installed(tv))
.map(|(tool, tv, _)| (tool, tv))
.collect::<Vec<_>>();

Check warning on line 67 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L65-L67

Added lines #L65 - L67 were not covered by tests

fn install_new_version(
&self,
config: &Config,
tool: &Tool,
tv: &ToolVersion,
latest: String,
pr: &mut ProgressReport,
) -> Result<()> {
let mut tv = tv.clone();
tv.version = latest;
tool.decorate_progress_bar(pr, Some(&tv));
match tool.install_version(config, &tv, pr, false) {
Ok(_) => Ok(()),
Err(err) => {
pr.error(err.to_string());
Err(err.wrap_err(format!("failed to install {tv}")))
}
ts.install_versions(config, new_versions, &mpr, false)?;
for (tool, tv) in to_remove {
let mut pr = mpr.add();
self.uninstall_old_version(config, &tool, &tv, &mut pr)?;

Check warning on line 72 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L69-L72

Added lines #L69 - L72 were not covered by tests
}

let ts = ToolsetBuilder::new().with_args(&self.tool).build(config)?;
shims::reshim(config, &ts).wrap_err("failed to reshim")?;
runtime_symlinks::rebuild(config)?;
Ok(())

Check warning on line 78 in src/cli/upgrade.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/upgrade.rs#L75-L78

Added lines #L75 - L78 were not covered by tests
}

fn uninstall_old_version(
Expand All @@ -134,4 +100,3 @@ impl Upgrade {
}

type OutputVec = Vec<(Arc<Tool>, ToolVersion, String)>;
type GroupedToolVersions = Vec<(Arc<Tool>, Vec<(ToolVersion, String)>)>;

0 comments on commit ef3bc0d

Please sign in to comment.