use crate::{ local::Installation, package::{PkgIdent, PkgSpec}, repo::RepoPackage, }; use itertools::Itertools; use rust_i18n::t; impl super::Packie { pub fn calculate_installation( &mut self, pkgs: &[PkgSpec], options: &CalculateInstallationOptions, ) -> Result, GraphError> { let mut all = Vec::with_capacity(pkgs.len() * 4); for pkg in pkgs { let mut current = self._calculate_installation(pkg, options)?; all.append(&mut current); } let manual_install: Vec<(PkgIdent, PkgSpec)> = all .iter() .filter(|x| !x.auto_installed) .map(|x| { ( x.repo_pkg.pkg_manifest.pkg_ident(), x.install_pkgspec.clone(), ) }) .collect(); let mut all: Vec<_> = all .into_iter() .unique_by(|x| x.repo_pkg.pkg_manifest.pkg_ident()) .collect(); for pkg in &mut all { let manual = manual_install .iter() .find(|(pkgid, _)| pkgid == &pkg.repo_pkg.pkg_manifest.pkg_ident()); if let Some((_, install_pkgspec)) = manual { pkg.auto_installed = false; pkg.install_pkgspec = install_pkgspec.clone(); } } Ok(all) } fn _calculate_installation( &mut self, desired: &PkgSpec, options: &CalculateInstallationOptions, ) -> Result, GraphError> { let prior_arch = self.profile.host_arch.clone(); let mut query = self.query_repo(); let mut depgraph = Vec::new(); let mut stack = vec![desired.clone()]; loop { let Some(pkgspec) = stack.pop() else { break; }; let mut found = query.search_package(&pkgspec); if found.is_empty() { return Err(GraphError::NotFound(pkgspec.to_string())); } let prior_pkg = prior_pkg( found.iter().map(|x| x.pkg_manifest.pkg_ident()), &prior_arch, ); let prior_pkg = found .iter() .enumerate() .find(|(_, p)| p.pkg_manifest.pkg_ident() == prior_pkg) .unwrap() .0; let repo_pkg = found.remove(prior_pkg); let already_installed = !self .search_installation(&pkgspec) .unwrap_or_default() .is_empty(); if already_installed && !(&pkgspec == desired && options.no_existence_check) { continue; } depgraph.push(InstallationNode { repo_pkg: repo_pkg.clone(), auto_installed: &pkgspec != desired, install_pkgspec: pkgspec, }); for dep in &repo_pkg.pkg_manifest.dependencies { stack.push(dep.clone()); } if !options.skip_recommendations { for recommended in &repo_pkg.pkg_manifest.recommendations { stack.push(recommended.clone()); } } } depgraph.reverse(); Ok(depgraph) } pub fn calculate_upgradation( &mut self, pkgspecs: Vec, ) -> Result, GraphError> { // Collect all package idents required to upgrade let mut installations = Vec::with_capacity(pkgspecs.len()); if pkgspecs.is_empty() { self.for_each_installation(|x| Ok::<_, rusqlite::Error>(installations.push(x))) .unwrap(); } else { for pkgspec in pkgspecs { let mut searched = self.search_installation(&pkgspec).unwrap_or_default(); installations.append(&mut searched); } } // Calculate upgradation nodes for each package ident required to upgrade let mut nodes = Vec::with_capacity(installations.len() / 3); for i in installations { let Some(node) = self._calculate_upgradation(&i.pkg_manifest.pkg_ident())? else { continue; }; nodes.push(node); } // Collect upgradation summary let mut ops = Vec::with_capacity(nodes.len()); for node in &nodes { let mut ti = self.calculate_installation( &[node.new.pkg_manifest.pkg_ident().into()], &CalculateInstallationOptions { skip_recommendations: false, no_existence_check: false, }, )?; ti.pop(); ti.retain(|x| { !nodes .iter() .any(|y| y.new.pkg_manifest.pkg_ident() == x.repo_pkg.pkg_manifest.pkg_ident()) }); for i in ti { ops.push(UpgradationOp::Install(i)); } ops.push(UpgradationOp::Replace(node.clone())); } Ok(ops) } fn _calculate_upgradation( &mut self, pkg: &PkgIdent, ) -> Result, GraphError> { let original = self .local_db .select_installation_by_ident(pkg) .map_err(|_| GraphError::NotFound(pkg.to_string()))?; let depgraph = self._calculate_installation( &original.install_pkgspec, &CalculateInstallationOptions { skip_recommendations: false, no_existence_check: true, }, ); let Ok(mut depgraph) = depgraph else { return Ok(None); }; if depgraph.last().map(|x| x.repo_pkg.pkg_manifest.pkg_ident()) == Some(pkg.clone()) { return Ok(None); } Ok(Some(UpgradationNode { original, new: depgraph.pop().unwrap().repo_pkg, })) } } #[derive(Debug, thiserror::Error)] pub enum GraphError { #[error("{}", t!("graph.GraphError.NotFound", item = .0))] NotFound(String), } #[derive(Debug, Default, Clone)] pub struct CalculateInstallationOptions { /// True if we don't need to collect recommendations. skip_recommendations: bool, /// True if we don't need to check if the package is already installed. This is used for checking for updates. no_existence_check: bool, } impl CalculateInstallationOptions { pub fn new() -> Self { Self::default() } } #[derive(Debug, Clone)] pub struct InstallationNode { pub repo_pkg: RepoPackage, pub install_pkgspec: PkgSpec, pub auto_installed: bool, } #[derive(Debug, Clone)] pub struct UpgradationNode { pub original: Installation, pub new: RepoPackage, } #[derive(Debug, Clone)] pub enum UpgradationOp { Install(InstallationNode), Replace(UpgradationNode), } /// Gets prior package that is installed by default in package candidates. pub(crate) fn prior_pkg(all: impl Iterator, prior_arch: &str) -> PkgIdent { let mut all = all.collect::>(); all.sort_by(|x, y| x.version.cmp(&y.version)); all.reverse(); for i in &all { if i.arch == prior_arch { return i.clone(); } } all.remove(0) }