Skip to content

Commit

Permalink
refactor: remove backend arg from tool-version (#2980)
Browse files Browse the repository at this point in the history
Prerequisite for #2979. Because backend arg will change during TV resolving, this allows me to modify backend_arg.full during resolving
  • Loading branch information
jdx authored Nov 10, 2024
1 parent 4a3d355 commit 3aa3250
Show file tree
Hide file tree
Showing 20 changed files with 91 additions and 99 deletions.
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ feature-depth = 1
# output a note when they are encountered.
ignore = [
{ id = "RUSTSEC-2024-0370", reason = "subdependency cannot be updated" },
{ id = "RUSTSEC-2024-0384", reason = "subdependency cannot be updated" },
#"RUSTSEC-0000-0000",
#{ id = "RUSTSEC-0000-0000", reason = "you can specify a reason the advisory is ignored" },
#"a-crate-that-is-yanked@0.1.1", # you can also ignore yanked crate versions if you wish
Expand Down
8 changes: 4 additions & 4 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ pub trait Backend: Debug + Send + Sync {
}
}
}
fn is_version_outdated(&self, tv: &ToolVersion, p: &dyn Backend) -> bool {
let latest = match tv.latest_version(p) {
fn is_version_outdated(&self, tv: &ToolVersion) -> bool {
let latest = match tv.latest_version() {
Ok(latest) => latest,
Err(e) => {
debug!(
Expand Down Expand Up @@ -407,7 +407,7 @@ pub trait Backend: Debug + Send + Sync {
None
}

#[requires(ctx.tv.backend.backend_type == self.get_type())]
#[requires(ctx.tv.backend().backend_type == self.get_type())]
fn install_version(&self, ctx: InstallContext) -> eyre::Result<()> {
if let Some(plugin) = self.plugin() {
plugin.is_installed_err()?;
Expand All @@ -433,7 +433,7 @@ pub trait Backend: Debug + Send + Sync {
)));
}

BackendMeta::write(&ctx.tv.backend)?;
BackendMeta::write(ctx.tv.backend())?;

self.cleanup_install_dirs(&ctx.tv);
// attempt to touch all the .tool-version files to trigger updates in hook-env
Expand Down
4 changes: 2 additions & 2 deletions src/cli/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ fn list_versions(config: &Config, args: &[String]) -> Result<()> {
_ => None,
};
if let Some(plugin) = plugin {
versions.retain(|(_, v)| &v.backend.to_string() == plugin);
versions.retain(|(_, v)| &v.backend().to_string() == plugin);
for (_, version) in versions {
miseprintln!("{}", version.version);
}
} else {
for (plugin, versions) in &versions.into_iter().chunk_by(|(_, v)| v.backend.clone()) {
for (plugin, versions) in &versions.into_iter().chunk_by(|(_, v)| v.backend().clone()) {
miseprintln!("{}", plugin);
for (_, tv) in versions {
miseprintln!(" {}", tv.version);
Expand Down
6 changes: 4 additions & 2 deletions src/cli/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,12 @@ impl Current {
}
for tv in versions {
if !plugin.is_version_installed(tv, true) {
let source = ts.versions.get(&tv.backend).unwrap().source.clone();
let source = ts.versions.get(tv.backend()).unwrap().source.clone();
warn!(
"{}@{} is specified in {}, but not installed",
&tv.backend, &tv.version, &source
&tv.backend(),
&tv.version,
&source
);
hint!(
"tools_missing",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl Latest {
plugin.ensure_installed(&mpr, false)?;
}
if let Some(v) = prefix {
prefix = Some(config.resolve_alias(backend.as_ref(), &v)?);
prefix = Some(config.resolve_alias(&backend, &v)?);
}

let latest_version = if self.installed {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl Ls {
// only displaying 1 plugin so only show the version
miseprintln!("{}", tv.version);
} else {
miseprintln!("{} {}", &tv.backend, tv.version);
miseprintln!("{} {}", tv.backend(), tv.version);
}
}
Ok(())
Expand Down Expand Up @@ -228,7 +228,7 @@ impl Ls {
})
.map(|(k, (p, tv))| {
let source = match &active.get(&k) {
Some((_, tv)) => ts.versions.get(&tv.backend).map(|tv| tv.source.clone()),
Some((_, tv)) => ts.versions.get(tv.backend()).map(|tv| tv.source.clone()),
None => None,
};
(self, p, tv, source)
Expand Down Expand Up @@ -326,7 +326,7 @@ impl From<(&Ls, &dyn Backend, &ToolVersion, &Option<ToolSource>)> for VersionSta
let outdated = if ls.offline {
false
} else {
p.is_version_outdated(tv, p)
p.is_version_outdated(tv)
};
VersionStatus::Active(tv.version.clone(), outdated)
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/prune.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Prune {
.collect::<BTreeMap<String, (Arc<dyn Backend>, ToolVersion)>>();

if let Some(backends) = &self.plugin {
to_delete.retain(|_, (_, tv)| backends.contains(&tv.backend));
to_delete.retain(|_, (_, tv)| backends.contains(tv.backend()));
}

for cf in config.get_tracked_config_files()?.values() {
Expand Down
7 changes: 2 additions & 5 deletions src/cli/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,12 @@ impl Uninstall {
.into_iter()
.map(|v| {
let tvr = ToolRequest::new(tool.fa().clone(), v, ToolSource::Unknown)?;
let tv = ToolVersion::new(tool.as_ref(), tvr, v.into());
let tv = ToolVersion::new(tvr, v.into());
Ok((tool.clone(), tv))
})
.collect::<Result<Vec<_>>>()?;
if let Some(tvr) = &a.tvr {
tvs.push((
tool.clone(),
tvr.resolve(tool.as_ref(), &Default::default())?,
));
tvs.push((tool.clone(), tvr.resolve(&Default::default())?));
}
if tvs.is_empty() {
warn!("no versions found for {}", style(&tool).blue().for_stderr());
Expand Down
2 changes: 1 addition & 1 deletion src/cli/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Upgrade {
outdated.retain(|o| {
self.tool
.iter()
.any(|t| t.backend == o.tool_version.backend)
.any(|t| &t.backend == o.tool_version.backend())
});
}
if outdated.is_empty() {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl Use {
let mut cf = self.get_config_file()?;
let pin = self.pin || !self.fuzzy && (SETTINGS.pin || SETTINGS.asdf_compat);

for (fa, tvl) in &versions.iter().chunk_by(|tv| &tv.backend) {
for (fa, tvl) in &versions.iter().chunk_by(|tv| tv.backend()) {
let versions: Vec<_> = tvl
.into_iter()
.map(|tv| {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/where.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Where {

let ba = tvr.backend();
let backend = backend::get(ba);
let tv = tvr.resolve(backend.as_ref(), &Default::default())?;
let tv = tvr.resolve(&Default::default())?;

if backend.is_version_installed(&tv, true) {
miseprintln!("{}", tv.install_path().to_string_lossy());
Expand Down
3 changes: 1 addition & 2 deletions src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ impl dyn ConfigFile {
.into_iter()
.map(|tvr| {
if pin {
let plugin = backend::get(&fa);
let tv = tvr.resolve(plugin.as_ref(), &Default::default())?;
let tv = tvr.resolve(&Default::default())?;
Ok((tv.version, tv.request.options()))
} else {
Ok((tvr.version(), tvr.options()))
Expand Down
2 changes: 1 addition & 1 deletion src/config/env_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl EnvResults {
if ts
.list_missing_versions()
.iter()
.any(|tv| tv.backend.name == "python")
.any(|tv| tv.backend().name == "python")
{
debug!("python not installed, skipping venv creation");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rayon::prelude::*;
pub use settings::Settings;
use walkdir::WalkDir;

use crate::backend::Backend;
use crate::backend::ABackend;
use crate::cli::args::BackendArg;
use crate::cli::version;
use crate::config::config_file::legacy_version::LegacyVersionFile;
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Config {
.collect())
}

pub fn resolve_alias(&self, backend: &dyn Backend, v: &str) -> Result<String> {
pub fn resolve_alias(&self, backend: &ABackend, v: &str) -> Result<String> {
if let Some(plugin_aliases) = self.aliases.get(backend.fa()) {
if let Some(alias) = plugin_aliases.get(v) {
return Ok(alias.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn update_lockfiles(new_versions: &[ToolVersion]) -> Result<()> {
}

// add versions added within this session such as from `mise use` or `mise up`
for (backend, group) in &new_versions.iter().chunk_by(|tv| &tv.backend) {
for (backend, group) in &new_versions.iter().chunk_by(|tv| tv.backend()) {
let tvs = group.cloned().collect_vec();
let source = tvs[0].request.source().clone();
let mut tvl = ToolVersionList::new(backend.clone(), source.clone());
Expand Down
6 changes: 3 additions & 3 deletions src/shims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,17 +275,17 @@ fn err_no_version_set(ts: Toolset, bin_name: &str, tvs: Vec<ToolVersion>) -> Res
if tvs.is_empty() {
bail!("{} is not a valid shim", bin_name);
}
let missing_plugins = tvs.iter().map(|tv| &tv.backend).collect::<HashSet<_>>();
let missing_plugins = tvs.iter().map(|tv| tv.backend()).collect::<HashSet<_>>();
let mut missing_tools = ts
.list_missing_versions()
.into_iter()
.filter(|t| missing_plugins.contains(&t.backend))
.filter(|t| missing_plugins.contains(t.backend()))
.collect_vec();
if missing_tools.is_empty() {
let mut msg = format!("No version is set for shim: {}\n", bin_name);
msg.push_str("Set a global default version with one of the following:\n");
for tv in tvs {
msg.push_str(&format!("mise use -g {}@{}\n", tv.backend, tv.version));
msg.push_str(&format!("mise use -g {}@{}\n", tv.backend(), tv.version));
}
Err(eyre!(msg.trim().to_string()))
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl Toolset {
.into_iter()
.filter(|(p, tv)| opts.force || !p.is_version_installed(tv, true))
.map(|(_, tv)| tv)
.filter(|tv| matches!(self.versions[&tv.backend].source, ToolSource::Argument))
.filter(|tv| matches!(self.versions[tv.backend()].source, ToolSource::Argument))
.map(|tv| tv.request)
.collect_vec();
let versions = self.install_versions(config, versions, &mpr, opts)?;
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Toolset {
sleep(Duration::from_millis(100));
}
}
let tv = tr.resolve(t.as_ref(), &opts.resolve_options)?;
let tv = tr.resolve(&opts.resolve_options)?;
let ctx = InstallContext {
ts,
pr: mpr.add(&tv.style()),
Expand Down Expand Up @@ -284,7 +284,7 @@ impl Toolset {
Some((p, tv)) => Ok((p.clone(), tv.clone())),
None => {
let tv = ToolRequest::new(p.fa().clone(), &v, ToolSource::Unknown)?
.resolve(p.as_ref(), &Default::default())
.resolve(&Default::default())
.unwrap();
Ok((p.clone(), tv))
}
Expand Down Expand Up @@ -333,7 +333,7 @@ impl Toolset {
source: v.request.source().clone(),
};
let version = format!("ref:{r}");
ToolVersion::new(p.as_ref(), request, version)
ToolVersion::new(request, version)
}
_ => v.clone(),
};
Expand Down Expand Up @@ -364,7 +364,7 @@ impl Toolset {
let latest_result = if bump {
t.latest_version(prefix.clone())
} else {
tv.latest_version(t.as_ref()).map(Option::from)
tv.latest_version().map(Option::from)
};
let mut out = OutdatedInfo::new(tv.clone(), tv.request.source().clone());
out.current = if t.is_version_installed(&tv, true) {
Expand Down Expand Up @@ -523,7 +523,7 @@ impl Toolset {
let versions = self
.list_missing_versions()
.into_iter()
.filter(|tv| &tv.backend == plugin.fa())
.filter(|tv| tv.backend() == plugin.fa())
.map(|tv| tv.request)
.collect_vec();
if !versions.is_empty() {
Expand Down Expand Up @@ -731,7 +731,7 @@ pub struct OutdatedInfo {
impl OutdatedInfo {
fn new(tv: ToolVersion, source: ToolSource) -> Self {
Self {
name: tv.backend.short.to_string(),
name: tv.backend().short.to_string(),
current: None,
requested: tv.request.version(),
tool_request: tv.request.clone(),
Expand Down
7 changes: 3 additions & 4 deletions src/toolset/tool_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use eyre::{bail, Result};
use versions::{Chunk, Version};
use xx::file;

use crate::backend::Backend;
use crate::cli::args::BackendArg;
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::tool_version::ResolveOptions;
Expand Down Expand Up @@ -161,7 +160,7 @@ impl ToolRequest {

pub fn is_installed(&self) -> bool {
let backend = backend::get(self.backend());
let tv = ToolVersion::new(backend.as_ref(), self.clone(), self.version());
let tv = ToolVersion::new(self.clone(), self.version());

backend.is_version_installed(&tv, false)
}
Expand Down Expand Up @@ -226,8 +225,8 @@ impl ToolRequest {
Ok(None)
}

pub fn resolve(&self, plugin: &dyn Backend, opts: &ResolveOptions) -> Result<ToolVersion> {
ToolVersion::resolve(plugin, self.clone(), opts)
pub fn resolve(&self, opts: &ResolveOptions) -> Result<ToolVersion> {
ToolVersion::resolve(self.clone(), opts)
}
}

Expand Down
Loading

0 comments on commit 3aa3250

Please sign in to comment.