Skip to main content

vs_core/service/
current.rs

1//! Services for resolving the currently active tool version.
2
3use crate::{App, CoreError, CurrentTool};
4
5impl App {
6    /// Resolves the active version for a specific plugin.
7    pub fn current_tool(&self, plugin_name: &str) -> Result<Option<CurrentTool>, CoreError> {
8        let resolved = vs_config::resolve_tool_version(
9            self.home(),
10            &self.cwd,
11            self.session_id.as_deref(),
12            plugin_name,
13        )?;
14        let Some(resolved) = resolved else {
15            return Ok(None);
16        };
17        if self
18            .load_installed_runtime(&resolved.plugin, &resolved.version)?
19            .is_none()
20        {
21            return Ok(None);
22        }
23        Ok(Some(CurrentTool {
24            plugin: resolved.plugin,
25            version: resolved.version,
26            scope: resolved.scope,
27            source: resolved.source,
28        }))
29    }
30
31    /// Resolves all active tools for the current context.
32    pub fn current_tools(&self) -> Result<Vec<CurrentTool>, CoreError> {
33        let mut current = Vec::new();
34        for configured in self.collect_current_tools()? {
35            if self
36                .load_installed_runtime(&configured.plugin, &configured.version)?
37                .is_some()
38            {
39                current.push(configured);
40            }
41        }
42        Ok(current)
43    }
44
45    /// Returns all known plugins with their current version when available.
46    pub fn current_tool_statuses(&self) -> Result<Vec<(String, Option<String>)>, CoreError> {
47        let mut names = self
48            .added_plugins()?
49            .into_iter()
50            .map(|entry| entry.name)
51            .collect::<std::collections::BTreeSet<_>>();
52        names.extend(
53            self.list_installed_versions()?
54                .into_iter()
55                .map(|installed| installed.plugin),
56        );
57
58        let mut statuses = Vec::new();
59        for plugin in names {
60            let current = self.current_tool(&plugin)?;
61            statuses.push((plugin, current.map(|tool| tool.version)));
62        }
63        Ok(statuses)
64    }
65}