feat: track local dependencies

Signed-off-by: sisungo <[email protected]>
This commit is contained in:
sisungo committed 2026-10-14 02:00:26 +00:00
1 parent 6cbfa14b64
commit 3ba1638f3c
10 files changed
+184 -11

No files matched your search

+17 -4
View File
@@ -144,12 +144,17 @@ pub fn download_packages<T>(
let download_size = packages.iter().map(|x| pkginfo(&x.0).download_size).sum();
let progress_bar =
ProgressBar::new(download_size).with_style(crate::common::progress_style_multidownload());
for (package, file_path) in packages.iter_mut() {
let nr_packages = packages.len();
for (count, (package, file_path)) in packages.iter_mut().enumerate() {
let repo_pkg = pkginfo(package);
let init_pos = progress_bar.position();
progress_bar.println(format!(
"Downloading \"{}\"...",
repo_pkg.pkg_manifest.pkg_ident()
"[{count}/{nr_packages}] {}",
t!(
"common.download_packages.downloading_hint",
pkg = style(repo_pkg.pkg_manifest.pkg_ident()).green(),
),
));
progress_bar.set_prefix(repo_pkg.pkg_manifest.pkg_ident().to_string());
let path = packie.download_package(&repo_pkg.repo_pkg_ident().unwrap(), |ev| match ev {
@@ -157,7 +162,15 @@ pub fn download_packages<T>(
progress_bar.set_position(init_pos + sum);
}
DownloadPackageEvent::Error(url, err) => {
progress_bar.println(format!("error: failed to download from \"{url}\": {err}"));
progress_bar.println(format!(
"{}",
style(t!(
"common.download_packages.download_error",
url = url,
error = err
))
.red()
));
}
})?;
*file_path = path;
+12 -3
View File
@@ -1,5 +1,6 @@
use crate::common::{SummaryItem, format_size, print_summary, yesno};
use clap::Parser;
use console::style;
use indicatif::ProgressBar;
use packie::{
PackieBuilder, graph::CalculateInstallationOptions, install::InstallOptions, package::PkgSpec,
@@ -77,9 +78,17 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
crate::common::download_packages(&mut packie, &mut packages, |x| &x.repo_pkg)?;
let progress_bar = ProgressBar::new(packages.len() as _);
for (package, file_path) in packages {
progress_bar.println(format!("Installing \"{}\"...", package.install_pkgspec));
let nr_packages = packages.len();
let progress_bar = ProgressBar::new(nr_packages as _);
for (count, (package, file_path)) in packages.into_iter().enumerate() {
progress_bar.println(format!(
"[{}/{nr_packages}] {}",
count + 1,
t!(
"install.installing_hint",
pkgspec = style(&package.install_pkgspec).green()
)
));
let mut options = InstallOptions::default();
options.auto_installed = package.auto_installed;
+16 -2
View File
@@ -1,5 +1,6 @@
use crate::common::{SummaryItem, format_size, print_summary, yesno};
use clap::Parser;
use console::style;
use indicatif::ProgressBar;
use packie::{PackieBuilder, install::InstallOptions, package::PkgSpec};
use rust_i18n::t;
@@ -78,13 +79,26 @@ pub fn main(cli: Cli) -> Result<(), crate::Error> {
crate::common::download_packages(&mut packie, &mut installs, |x| &x.repo_pkg)?;
let progress_bar = ProgressBar::new(installs.len() as u64 + removes.len() as u64);
let nr_actions = installs.len() + removes.len();
let progress_bar = ProgressBar::new(nr_actions as _);
for removal in removes {
progress_bar.println(format!("Removing \"{removal}\"..."));
progress_bar.println(format!(
"[{}/{nr_actions}] {}",
progress_bar.position() + 1,
t!("upgrade.removing_hint", pkg = style(&removal).red()),
));
packie.remove(&removal.into())?;
progress_bar.inc(1);
}
for (package, file_path) in installs {
progress_bar.println(format!(
"[{}/{nr_actions}] {}",
progress_bar.position() + 1,
t!(
"upgrade.installing_hint",
pkg = style(&package.install_pkgspec).green(),
)
));
progress_bar.println(format!("Installing \"{}\"...", package.install_pkgspec));
let mut options = InstallOptions::default();
+8
View File
@@ -11,6 +11,10 @@ install.InstallError.DependencyRing:
en: "dependency ring detected"
zh-CN: "检测到依赖环"
ja: "依存関係の循環が検出されました"
install.InstallError.DependencyNotSatisfied:
en: "dependency not satisfied: %{pkgspec}"
zh-CN: "依赖关系未满足:%{pkgspec}"
ja: "依存関係が満たされていません:%{pkgspec}"
remove.RemoveError.NotUniquePkgSpec:
en: "not unique package spec"
zh-CN: "指定的包范围匹配到了多个包"
@@ -27,6 +31,10 @@ remove.RemoveError.RemoveFiles:
en: "failed to remove package files: %{error}"
zh-CN: "无法删除包文件:%{error}"
ja: "パッケージファイルの削除に失敗しました:%{error}"
remove.RemoveError.HasDependents:
en: "some packages depends on the package to be removed"
zh-CN: "一些包依赖于即将被移除的包"
ja: "削除対象のパッケージに依存するパッケージが存在します"
install.InstallError.AlreadyInstalled:
en: "package already installed"
zh-CN: "此包已经安装"
+20
View File
@@ -35,6 +35,10 @@ install.no_package_to_install:
en: "No packages to install."
zh-CN: "没有要安装的包。"
ja: "インストールするパッケージがありません。"
install.installing_hint:
en: "Installing \"%{pkgspec}\"..."
zh-CN: "正在安装 \"%{pkgspec}\"..."
ja: "「%{pkgspec}」をインストールしています..."
upgrade.prompt_to_upgrade:
en: "The following packages are to be changed during upgradation:"
zh-CN: "在更新过程中,将对下列包进行更改:"
@@ -59,6 +63,14 @@ upgrade.nothing_to_do:
en: "Nothing to do today."
zh-CN: "今日无事可做。"
ja: "本日は行うことがありません。"
upgrade.removing_hint:
en: "Removing \"%{pkg}\"..."
zh-CN: "正在移除 \"%{pkg}\"..."
ja: "「%{pkg}」を削除しています..."
upgrade.installing_hint:
en: "Installing \"%{pkgspec}\"..."
zh-CN: "正在安装 \"%{pkgspec}\"..."
ja: "「%{pkgspec}」をインストールしています..."
info.no_package_found:
en: "The specified package was not found."
zh-CN: "找不到指定的包。"
@@ -163,6 +175,14 @@ common.note_pkgspec_not_unique:
en: "The following packages were matched:"
zh-CN: "匹配到了下面的包:"
ja: "以下のパッケージが一致しました:"
common.download_packages.downloading_hint:
en: "Downloading \"%{pkg}\"..."
zh-CN: "正在下载 \"%{pkg}\"..."
ja: "\"%{pkg}\" をダウンロード中..."
common.download_packages.download_error:
en: "error: failed to download from \"%{url}\": %{error}"
zh-CN: "错误:无法下载 \"%{url}\": %{error}"
ja: "エラー: \"%{url}\" からのダウンロードに失敗しました: %{error}"
clean.warn_repos_dir:
en: "warning: failed to read repository cache directory: %{error}"
zh-CN: "警告:无法读取软件包仓库缓存目录:%{error}"
+1 -1
View File
@@ -211,7 +211,7 @@ impl UpgradationSummary {
}
/// Gets prior package that is installed by default in package candidates.
fn prior_pkg(all: impl Iterator<Item = PkgIdent>, prior_arch: &str) -> PkgIdent {
pub(crate) fn prior_pkg(all: impl Iterator<Item = PkgIdent>, prior_arch: &str) -> PkgIdent {
let mut all = all.collect::<Vec<_>>();
all.sort_by(|x, y| x.version.cmp(&y.version));
all.reverse();
+28 -1
View File
@@ -68,10 +68,29 @@ impl super::Packie {
}
// Check if the package spec is valid
if !install_pkgspec.matches(&pkg_ident) {
let real_matches = install_pkgspec.matches(&pkg_ident);
let abstract_matches = pkg_manifest
.provides
.iter()
.any(|x| install_pkgspec.abstract_matches(x));
if !(real_matches || abstract_matches) {
return Err(InstallError::PkgSpec);
}
// Check & Collect "real" dependencies
let mut real_dependencies = Vec::with_capacity(pkg_manifest.dependencies.len());
for pkg_spec_b in pkg_manifest.dependencies.iter() {
let candidates = self.search_installation(pkg_spec_b)?;
if candidates.is_empty() {
return Err(InstallError::DependencyNotSatisfied(pkg_spec_b.clone()));
}
let prior = crate::graph::prior_pkg(
candidates.iter().map(|x| x.pkg_manifest.pkg_ident()),
&self.profile().host_arch,
);
real_dependencies.push(prior);
}
// Copy package files
let dest_dir = self.profile.pkg_dir.join(pkg_ident.prefix_name());
crate::common::copy_dir(package.bundle_dir(), dest_dir).map_err(InstallError::Copy)?;
@@ -91,6 +110,11 @@ impl super::Packie {
};
self.local_db.insert_installation(&installation)?;
// Record "real" dependencies in the database
for pkg_ident_b in real_dependencies {
self.local_db.insert_dependency(&pkg_ident, &pkg_ident_b)?;
}
// Record abstract package provision in the database
for i in installation.pkg_manifest.provides.iter() {
self.local_db.insert_abspkg(&i, &pkg_ident)?;
@@ -148,6 +172,9 @@ pub enum InstallError {
#[error("{}", t!("install.InstallError.DependencyRing"))]
DependencyRing,
#[error("{}", t!("install.InstallError.DependencyNotSatisfied", pkgspec = .0))]
DependencyNotSatisfied(PkgSpec),
#[error("{0}")]
Package(PackageError),
+61
View File
@@ -124,6 +124,11 @@ CREATE TABLE IF NOT EXISTS "installation"(
"installed_size" INTEGER NOT NULL,
"install_flags" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "dependency"(
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"pkg_ident_a" TEXT NOT NULL,
"pkg_ident_b" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "abspkg"(
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"abspkg_ident" TEXT NOT NULL,
@@ -169,6 +174,19 @@ CREATE TABLE IF NOT EXISTS "abspkg"(
Ok(())
}
pub fn insert_dependency(
&mut self,
pkg_ident_a: &PkgIdent,
pkg_ident_b: &PkgIdent,
) -> rusqlite::Result<()> {
self.0
.prepare_cached(
r#"INSERT INTO "dependency"("pkg_ident_a", "pkg_ident_b") VALUES(?1, ?2)"#,
)?
.execute(params![pkg_ident_a.to_string(), pkg_ident_b.to_string()])?;
Ok(())
}
pub fn insert_abspkg(
&mut self,
abspkg: &AbsPkgIdent,
@@ -219,6 +237,42 @@ CREATE TABLE IF NOT EXISTS "abspkg"(
Ok(abspkg)
}
/// Selects all package that the provided package depends on.
pub fn select_dependencies(
&mut self,
pkg_ident_a: &PkgIdent,
) -> rusqlite::Result<Vec<PkgIdent>> {
self._select_dependency_by_pkg_ident("pkg_ident_a", "pkg_ident_b", pkg_ident_a)
}
/// Selects all packages depending on the provided package.
pub fn select_dependents(&mut self, pkg_ident_b: &PkgIdent) -> rusqlite::Result<Vec<PkgIdent>> {
self._select_dependency_by_pkg_ident("pkg_ident_b", "pkg_ident_a", pkg_ident_b)
}
fn _select_dependency_by_pkg_ident(
&mut self,
provided: &str,
returns: &str,
data: &PkgIdent,
) -> rusqlite::Result<Vec<PkgIdent>> {
let mut all = Vec::with_capacity(32);
let mut stmt = self.0.prepare_cached(&format!(
"SELECT \"{returns}\" FROM \"dependency\" WHERE \"{provided}\" == ?1"
))?;
let rows = stmt.query_map(params![data.to_string()], |x| x.get::<_, String>(returns))?;
for row in rows {
all.push(row?.parse().map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(
0,
rusqlite::types::Type::Text,
Box::new(e),
)
})?);
}
Ok(all)
}
pub fn for_each_installation<E: From<rusqlite::Error>>(
&mut self,
mut f: impl FnMut(Installation) -> Result<(), E>,
@@ -244,6 +298,13 @@ CREATE TABLE IF NOT EXISTS "abspkg"(
.execute(params![provider.to_string()])
.map(|x| x > 0)
}
pub fn remove_dependency_by_a(&mut self, pkg_ident_a: &PkgIdent) -> rusqlite::Result<bool> {
self.0
.prepare_cached(r#"DELETE FROM "dependency" WHERE "pkg_ident_a" = ?1"#)?
.execute(params![pkg_ident_a.to_string()])
.map(|x| x > 0)
}
}
#[derive(Debug, Clone)]
+4
View File
@@ -138,6 +138,10 @@ impl PkgSpec {
None => true,
}
}
pub fn abstract_matches(&self, ident: &AbsPkgIdent) -> bool {
self.name == ident.name && self.arch == ident.arch
}
}
impl From<PkgIdent> for PkgSpec {
fn from(value: PkgIdent) -> Self {
+17
View File
@@ -17,6 +17,12 @@ impl super::Packie {
let found = found.remove(0);
let pkg_ident = found.pkg_manifest.pkg_ident();
// Check for dependencies
let dependents = self.local_db.select_dependents(&pkg_ident)?;
if !dependents.is_empty() {
return Err(RemoveError::HasDependents);
}
// Run configure scripts
_ = self.run_configure(
&pkg_ident,
@@ -54,6 +60,9 @@ impl super::Packie {
self.local_db
.remove_abspkg_provided_by(&pkg_ident)
.map_err(RemoveError::Database)?;
self.local_db
.remove_dependency_by_a(&pkg_ident)
.map_err(RemoveError::Database)?;
// Remove the links file
_ = std::fs::remove_file(self.local_data.links_file(&pkg_ident));
@@ -73,6 +82,14 @@ pub enum RemoveError {
#[error("{}", t!("remove.RemoveError.RemoveFiles", error = .0))]
RemoveFiles(std::io::Error),
#[error("{}", t!("remove.RemoveError.HasDependents"))]
HasDependents,
#[error("{}", t!("remove.RemoveError.Database", error = .0))]
Database(rusqlite::Error),
}
impl From<rusqlite::Error> for RemoveError {
fn from(value: rusqlite::Error) -> Self {
Self::Database(value)
}
}