Skip to main content

vs_core/service/
install.rs

1//! Services for planning and performing plugin installations.
2
3use crate::{App, CoreError, InstalledVersion};
4
5impl App {
6    /// Installs a plugin version, choosing the first available version when omitted.
7    pub fn install_plugin_version(
8        &self,
9        plugin_name: &str,
10        version: Option<&str>,
11    ) -> Result<InstalledVersion, CoreError> {
12        let entry = self.resolve_registry_entry(plugin_name)?;
13        let plugin = self.load_plugin(&entry)?;
14        let available_versions = plugin.available_versions(&[])?;
15        let selected_version = version
16            .map(str::to_string)
17            .or_else(|| {
18                available_versions
19                    .first()
20                    .map(|candidate| candidate.version.clone())
21            })
22            .ok_or_else(|| {
23                CoreError::Unsupported(format!(
24                    "plugin {plugin_name} does not expose installable versions"
25                ))
26            })?;
27
28        let plan = plugin.install_plan(&selected_version)?;
29        let runtime = self.installer.install(&plan)?;
30        plugin.post_install(&runtime)?;
31        Ok(InstalledVersion {
32            plugin: plugin_name.to_string(),
33            version: runtime.version,
34            install_dir: runtime.root_dir,
35        })
36    }
37
38    /// Returns the version requested by the project config for a plugin, when present.
39    pub fn project_tool_version(&self, plugin_name: &str) -> Result<Option<String>, CoreError> {
40        Ok(vs_config::find_project_file(&self.cwd)
41            .map(|path| vs_config::read_tool_versions(&path))
42            .transpose()?
43            .and_then(|tools| tools.tools.get(plugin_name).cloned()))
44    }
45
46    /// Lists configured tools that should be installed for the current context.
47    pub fn configured_tools_for_install(&self) -> Result<Vec<(String, String)>, CoreError> {
48        Ok(self
49            .collect_current_tools()?
50            .into_iter()
51            .map(|tool| (tool.plugin, tool.version))
52            .collect())
53    }
54}