Skip to content

Commit

Permalink
feat(oma-pm): add read X-AOSC-Features field with aosc feature
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Oct 18, 2024
1 parent 75fb236 commit 9f2c06a
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 61 deletions.
75 changes: 36 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions i18n/en-US/oma.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,11 @@ oma-refresh-lock-dueto = { $exec } ({ $pid }) has acquired the lock to the datab
oma-refresh-success-invoke = Executing Post-refresh configuration script (Post-Invoke-Success) ...
autoremove-tips-1 = { $count } unneeded packages on your system may be removed, which will free up { $size } in storage space; Please use { $cmd } to view the list of packages that can be removed.
autoremove-tips-2 = If you would like to keep a particular package, use { $cmd1 } to mark the package as manually installed; Otherwise, you may use { $cmd2 } to clean up the packages that are no longer needed.
essential-tips = { $pkg } is an ESSENTIAL system componen.
essential-continue = Are you sure that you would like to remove it?
yes-do-as-i-say-continue = Your turn
yes-do-as-i-say = If you are absolutely sure, please type the following: { $input }
features-without-value = The current operation would remove key AOSC OS components. If we proceed, Will cause some system features to be unavailable.
features-tips-1 = The current operation would remove key AOSC OS components. If we proceed, the system features below will no longer be available:
features-abort = To avoid system failure, oma has aborted the operation.
features-continue-prompt = Would you like to proceed with the operation?
8 changes: 8 additions & 0 deletions i18n/zh-CN/oma.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,11 @@ oma-refresh-lock-dueto = { $exec } ({ $pid }) 正占用数据库锁。
oma-refresh-success-invoke = 正在执行刷新后配置脚本 (Post-Invoke-Success) ...
autoremove-tips-1 = 您的系统中有 { $count } 个可清理的软件包,清理后可释放 { $size } 存储空间;请使用 { $cmd } 查阅可清理的软件包。
autoremove-tips-2 = 如需保留某个软件包,请使用 { $cmd1 } 标记保留;否则,您可以使用 { $cmd2 } 清理不再需要的软件包。
essential-tips = { $pkg } 乃是系统必备组件
essential-continue = 您确定要删除该软件包吗?
yes-do-as-i-say-continue = 你真想让你的系统见二次元吗?
yes-do-as-i-say = 如果您确认要继续,请键入:{ $input }
features-without-value = 当前操作可能导致部分关键 AOSC OS 组件被移除,如继续操作,将导致某些系统特性不可用。
features-tips-1 = 当前操作可能导致部分关键 AOSC OS 组件被移除,如继续操作,将导致如下系统特性不可用:
features-abort = 为避免系统故障,oma 已中止该操作。
features-continue-prompt = 您确定要继续该操作吗?
2 changes: 1 addition & 1 deletion oma-pm/examples/install_fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> Result<(), OmaAptError> {

apt.resolve(false, true)?;

let op = apt.summary(|_| false)?;
let op = apt.summary(|_| false, |_| false)?;

let pm = MyProgressManager::default();

Expand Down
21 changes: 21 additions & 0 deletions oma-pm/src/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
process::Command,
};

use ahash::HashSet;
use bon::{builder, Builder};
use chrono::Local;

Expand Down Expand Up @@ -135,6 +136,8 @@ pub enum OmaAptError {
PtrIsNone(#[from] PtrIsNone),
#[error(transparent)]
ChecksumError(#[from] ChecksumError),
#[error("Blocking install due to features")]
Features,
}

pub type OmaAptResult<T> = Result<T, OmaAptError>;
Expand Down Expand Up @@ -943,7 +946,9 @@ impl OmaApt {
pub fn summary(
&self,
how_handle_essential: impl Fn(&str) -> bool,
how_handle_features: impl Fn(&HashSet<Box<str>>) -> bool,
) -> OmaAptResult<OmaOperation> {
let mut features = HashSet::with_hasher(ahash::RandomState::new());
let mut install = vec![];
let mut remove = vec![];
let changes = self.cache.get_changes(true);
Expand Down Expand Up @@ -1014,6 +1019,18 @@ impl OmaApt {
return Err(OmaAptError::PkgIsEssential(name));
}

#[cfg(feature = "aosc")]
if let Some(feat) = pkg
.installed()
.and_then(|x| x.get_record("X-AOSC-Features"))
{
for i in feat.split_ascii_whitespace() {
if !features.contains(i) {
features.insert(Box::from(i));
}
}
}

let is_purge = pkg.marked_purge();

let mut tags = vec![];
Expand Down Expand Up @@ -1112,6 +1129,10 @@ impl OmaApt {
.map(|x| x.download_size())
.sum();

if !features.is_empty() && !how_handle_features(&features) {
return Err(OmaAptError::Features);
}

Ok(OmaOperation {
install,
remove,
Expand Down
8 changes: 6 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,8 @@ pub fn oma_apt_error_to_output(err: OmaAptError) -> OutputError {
source: None,
}
}
OmaAptError::PkgIsEssential(s) => OutputError {
description: fl!("pkg-is-essential", name = s),
OmaAptError::PkgIsEssential(pkg) => OutputError {
description: fl!("pkg-is-essential", name = pkg),
source: None,
},
OmaAptError::PkgNoCandidate(s) => OutputError {
Expand Down Expand Up @@ -735,6 +735,10 @@ pub fn oma_apt_error_to_output(err: OmaAptError) -> OutputError {
source: None,
},
OmaAptError::ChecksumError(e) => oma_checksum_error(e),
OmaAptError::Features => OutputError {
description: fl!("features-abort"),
source: None,
},
}
}

Expand Down
17 changes: 12 additions & 5 deletions src/subcommand/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,29 @@ pub fn execute(
/// "Yes Do as I say" steps
pub fn ask_user_do_as_i_say(pkg: &str) -> anyhow::Result<bool> {
let theme = ColorfulTheme::default();

warn!("{}", fl!("essential-tips", pkg = pkg));

let delete = Confirm::with_theme(&theme)
.with_prompt(format!("DELETE THIS PACKAGE? PACKAGE {pkg} IS ESSENTIAL!",))
.with_prompt(fl!("essential-continue"))
.default(false)
.interact()
.map_err(|_| anyhow!(""))?;

if !delete {
info!("Not confirmed.");
return Ok(false);
}

info!(
"If you are absolutely sure, please type the following: {}",
style("Do as I say!").bold()
"{}",
fl!(
"yes-do-as-i-say",
input = style("Do as I say!").bold().to_string()
),
);

if Input::<String>::with_theme(&theme)
.with_prompt("Your turn")
.with_prompt(fl!("yes-do-as-i-say-continue"))
.interact()?
!= "Do as I say!"
{
Expand Down
18 changes: 11 additions & 7 deletions src/subcommand/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::OmaArgs;
use crate::UpgradeArgs;

use super::remove::ask_user_do_as_i_say;
use super::utils::handle_features;
use super::utils::handle_no_result;
use super::utils::is_nothing_to_do;
use super::utils::lock_oma;
Expand Down Expand Up @@ -126,13 +127,16 @@ pub fn execute(
apt.resolve(false, true)?;
}

let op = apt.summary(|pkg| {
if protect_essentials {
false
} else {
ask_user_do_as_i_say(pkg).unwrap_or(false)
}
})?;
let op = apt.summary(
|pkg| {
if protect_essentials {
false
} else {
ask_user_do_as_i_say(pkg).unwrap_or(false)
}
},
|features| handle_features(features, protect_essentials).unwrap_or(false),
)?;

apt.check_disk_size(&op)?;

Expand Down
Loading

0 comments on commit 9f2c06a

Please sign in to comment.