Skip to content

Commit

Permalink
refactor: move opts from ToolVersion to ToolVersionRequest struct (#2057
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jdx authored May 11, 2024
1 parent 88f58b4 commit 0cd86a7
Show file tree
Hide file tree
Showing 29 changed files with 265 additions and 269 deletions.
55 changes: 29 additions & 26 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use eyre::Result;

use crate::cli::args::ToolArg;
use crate::config::Config;
use crate::forge;
use crate::toolset::{
InstallOptions, ToolVersion, ToolVersionOptions, ToolVersionRequest, Toolset, ToolsetBuilder,
};
Expand Down Expand Up @@ -52,18 +51,18 @@ impl Install {
match &self.tool {
Some(runtime) => self.install_runtimes(&config, runtime)?,
None => self.install_missing_runtimes(&config)?,
}
};

Ok(())
}
fn install_runtimes(&self, config: &Config, runtimes: &[ToolArg]) -> Result<()> {
fn install_runtimes(&self, config: &Config, runtimes: &[ToolArg]) -> Result<Vec<ToolVersion>> {
let mpr = MultiProgressReport::get();
let mut ts = ToolsetBuilder::new().build(config)?;
let tool_versions = self.get_requested_tool_versions(&ts, runtimes, &mpr)?;
let tool_versions = self.get_requested_tool_versions(&ts, runtimes)?;
if tool_versions.is_empty() {
warn!("no runtimes to install");
warn!("specify a version with `mise install <PLUGIN>@<VERSION>`");
return Ok(());
return Ok(vec![]);
}
ts.install_versions(config, tool_versions, &mpr, &self.install_opts())
}
Expand All @@ -81,51 +80,55 @@ impl Install {
&self,
ts: &Toolset,
runtimes: &[ToolArg],
mpr: &MultiProgressReport,
) -> Result<Vec<ToolVersion>> {
) -> Result<Vec<ToolVersionRequest>> {
let mut requests = vec![];
for ta in ToolArg::double_tool_condition(runtimes)? {
let default_opts = ToolVersionOptions::new();
match ta.tvr {
Some(tv) => requests.push((ta.forge, tv, default_opts.clone())),
// user provided an explicit version
// TODO: this should install using options from config if the version matches
Some(tv) => requests.push(tv),
None => {
if ta.tvr.is_none() {
match ts.versions.get(&ta.forge) {
// the tool is in config so fetch the params from config
// this may match multiple versions of one tool (e.g.: python)
Some(tvl) => {
for (tvr, opts) in &tvl.requests {
requests.push((ta.forge.clone(), tvr.clone(), opts.clone()));
for tvr in &tvl.requests {
requests.push(tvr.clone());
}
}
// in this case the user specified a tool which is not in config
// so we default to @latest with no options
None => {
let tvr =
ToolVersionRequest::Version(ta.forge.clone(), "latest".into());
requests.push((ta.forge, tvr, default_opts.clone()));
let tvr = ToolVersionRequest::Version {
forge: ta.forge.clone(),
version: "latest".into(),
options: default_opts.clone(),
};
requests.push(tvr);
}
}
}
}
}
}
let mut tool_versions = vec![];
for (fa, tvr, opts) in requests {
let plugin = forge::get(&fa);
plugin.ensure_installed(mpr, false)?;
let tv = tvr.resolve(plugin.as_ref(), opts, true)?;
tool_versions.push(tv);
}
Ok(tool_versions)
Ok(requests)
}

fn install_missing_runtimes(&self, config: &Config) -> Result<()> {
fn install_missing_runtimes(&self, config: &Config) -> Result<Vec<ToolVersion>> {
let mut ts = ToolsetBuilder::new().build(config)?;
let versions = ts.list_missing_versions();
let versions: Vec<_> = ts
.list_missing_versions()
.into_iter()
.map(|tv| tv.request)
.collect();
if versions.is_empty() {
info!("all runtimes are installed");
return Ok(());
return Ok(vec![]);
}
let mpr = MultiProgressReport::get();
ts.install_versions(config, versions, &mpr, &self.install_opts())?;
Ok(())
ts.install_versions(config, versions, &mpr, &self.install_opts())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Latest {
let config = Config::try_get()?;
let mut prefix = match self.tool.tvr {
None => self.asdf_version,
Some(ToolVersionRequest::Version(_, version)) => Some(version),
Some(ToolVersionRequest::Version { version, .. }) => Some(version),
_ => bail!("invalid version: {}", self.tool.style()),
};

Expand Down
2 changes: 1 addition & 1 deletion src/cli/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl LsRemote {
fn run_single(self, plugin: Arc<dyn Forge>) -> Result<()> {
let prefix = match &self.plugin {
Some(tool_arg) => match &tool_arg.tvr {
Some(ToolVersionRequest::Version(_, v)) => Some(v.clone()),
Some(ToolVersionRequest::Version { version: v, .. }) => Some(v.clone()),
_ => self.prefix.clone(),
},
_ => self.prefix.clone(),
Expand Down
7 changes: 2 additions & 5 deletions src/cli/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,12 @@ impl Uninstall {
.into_iter()
.map(|v| {
let tvr = ToolVersionRequest::new(tool.fa().clone(), v);
let tv = ToolVersion::new(tool.as_ref(), tvr, Default::default(), v.into());
let tv = ToolVersion::new(tool.as_ref(), tvr, v.into());
(tool.clone(), tv)
})
.collect::<Vec<_>>();
if let Some(tvr) = &a.tvr {
tvs.push((
tool.clone(),
tvr.resolve(tool.as_ref(), Default::default(), false)?,
));
tvs.push((tool.clone(), tvr.resolve(tool.as_ref(), false)?));
}
if tvs.is_empty() {
warn!("no versions found for {}", style(&tool).blue().for_stderr());
Expand Down
3 changes: 2 additions & 1 deletion src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Upgrade {
tv.version.clone_from(latest);
tv
})
.collect();
.collect::<Vec<_>>();

let to_remove = outdated
.into_iter()
Expand All @@ -101,6 +101,7 @@ impl Upgrade {
raw: self.raw,
latest_versions: true,
};
let new_versions = new_versions.into_iter().map(|tv| tv.request).collect();
ts.install_versions(config, new_versions, &mpr, &opts)?;
for (tool, tv) in to_remove {
let pr = mpr.add(&tv.style());
Expand Down
19 changes: 8 additions & 11 deletions src/cli/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::env::{
use crate::file::display_path;
use crate::toolset::{InstallOptions, ToolSource, ToolVersion, ToolVersionRequest, ToolsetBuilder};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::{env, file, forge};
use crate::{env, file};

/// Install tool version and add it to config
///
Expand Down Expand Up @@ -83,19 +83,16 @@ impl Use {
let config = Config::try_get()?;
let mut ts = ToolsetBuilder::new().build(&config)?;
let mpr = MultiProgressReport::get();
let versions = self
let versions: Vec<_> = self
.tool
.iter()
.map(|t| {
let tvr = match &t.tvr {
Some(ref tvr) => tvr.clone(),
None => ToolVersionRequest::new(t.forge.clone(), "latest"),
};
let plugin = forge::get(&t.forge);
ToolVersion::resolve(plugin.as_ref(), tvr, Default::default(), false)
.cloned()
.map(|t| match t.tvr {
Some(tvr) => tvr,
None => ToolVersionRequest::new(t.forge, "latest"),
})
.collect::<Result<Vec<_>>>()?;
ts.install_versions(
.collect();
let versions = ts.install_versions(
&config,
versions.clone(),
&mpr,
Expand Down
4 changes: 2 additions & 2 deletions src/cli/where.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Where {
.versions
.get(&self.tool.forge)
.and_then(|v| v.requests.first())
.map(|(r, _)| r.version());
.map(|r| r.version());
self.tool.with_version(&v.unwrap_or(String::from("latest")))
}
},
Expand All @@ -53,7 +53,7 @@ impl Where {
match runtime
.tvr
.as_ref()
.map(|tvr| tvr.resolve(plugin.as_ref(), Default::default(), false))
.map(|tvr| tvr.resolve(plugin.as_ref(), false))
{
Some(Ok(tv)) if plugin.is_version_installed(&tv) => {
miseprintln!("{}", tv.install_path().to_string_lossy());
Expand Down
5 changes: 1 addition & 4 deletions src/config/config_file/legacy_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ impl LegacyVersionFile {
for plugin in plugins {
let version = plugin.parse_legacy_file(&path)?;
for version in version.split_whitespace() {
toolset.add_version(
ToolVersionRequest::new(plugin.fa().clone(), version),
Default::default(),
);
toolset.add_version(ToolVersionRequest::new(plugin.fa().clone(), version));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/config_file/mise_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ impl ConfigFile for MiseToml {
trust_check(&self.path)?;
}
let version = self.parse_template(&tool.tt.to_string())?;
let tvr = ToolVersionRequest::new(fa.clone(), &version);
let mut options = tool.options.clone();
for v in options.values_mut() {
*v = self.parse_template(v)?;
}
toolset.add_version(tvr, options);
let tvr = ToolVersionRequest::new_opts(fa.clone(), &version, options);
toolset.add_version(tvr);
}
}
Ok(toolset)
Expand Down
6 changes: 3 additions & 3 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl dyn ConfigFile {
for (fa, versions) in &plugins_to_update {
let mut tvl = ToolVersionList::new(fa.clone(), ts.source.as_ref().unwrap().clone());
for tv in versions {
tvl.requests.push(((*tv).clone(), Default::default()));
tvl.requests.push((*tv).clone());
}
ts.versions.insert(fa.clone(), tvl);
}
Expand All @@ -111,7 +111,7 @@ impl dyn ConfigFile {
.map(|tvr| {
if pin {
let plugin = forge::get(&fa);
let tv = tvr.resolve(plugin.as_ref(), Default::default(), false)?;
let tv = tvr.resolve(plugin.as_ref(), false)?;
Ok(tv.version)
} else {
Ok(tvr.version())
Expand Down Expand Up @@ -144,7 +144,7 @@ impl dyn ConfigFile {
})?
.requests
.iter()
.map(|(tvr, _)| tvr.version())
.map(|tvr| tvr.version())
.collect::<Vec<_>>();
miseprintln!("{}", tvl.join(" "));
return Ok(true);
Expand Down
Loading

0 comments on commit 0cd86a7

Please sign in to comment.