From d053cb92262c597df3466fa85cbff52d09784d21 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Tue, 10 Dec 2024 14:56:17 +0100 Subject: [PATCH] pre-process transaction and remove menu items --- crates/rattler/src/install/driver.rs | 28 ++++++++++++++++++++++++-- crates/rattler_menuinst/src/macos.rs | 28 ++++++++++---------------- crates/rattler_menuinst/src/schema.rs | 1 + crates/rattler_menuinst/src/slugify.rs | 1 + crates/rattler_menuinst/src/util.rs | 1 + 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/crates/rattler/src/install/driver.rs b/crates/rattler/src/install/driver.rs index 8a7b33200..13bdd08f1 100644 --- a/crates/rattler/src/install/driver.rs +++ b/crates/rattler/src/install/driver.rs @@ -1,6 +1,7 @@ use std::{ borrow::Borrow, collections::{HashMap, HashSet}, + ffi::OsStr, path::{Path, PathBuf}, sync::{Arc, Mutex, MutexGuard}, }; @@ -155,10 +156,11 @@ impl InstallDriver { transaction: &Transaction, target_prefix: &Path, ) -> Result, PrePostLinkError> { + let mut result = None; if self.execute_link_scripts { match self.run_pre_unlink_scripts(transaction, target_prefix) { Ok(res) => { - return Ok(Some(res)); + result = Some(res); } Err(e) => { tracing::error!("Error running pre-unlink scripts: {:?}", e); @@ -166,7 +168,29 @@ impl InstallDriver { } } - Ok(None) + // For all packages that are removed, we need to remove menuinst entries as well + for record in transaction.removed_packages().map(Borrow::borrow) { + for path in record.paths_data.paths.iter() { + if path.relative_path.starts_with("Menu") + && path.relative_path.extension() == Some(OsStr::new("json")) + { + match rattler_menuinst::remove_menu_items( + &target_prefix.join(&path.relative_path), + target_prefix, + target_prefix, + Platform::current(), + rattler_menuinst::MenuMode::User, + ) { + Ok(_) => {} + Err(e) => { + tracing::warn!("Failed to remove menu item: {}", e); + } + } + } + } + } + + Ok(result) } /// Runs a blocking task that will execute on a separate thread. The task is diff --git a/crates/rattler_menuinst/src/macos.rs b/crates/rattler_menuinst/src/macos.rs index c98319edd..ae7532fe1 100644 --- a/crates/rattler_menuinst/src/macos.rs +++ b/crates/rattler_menuinst/src/macos.rs @@ -220,7 +220,7 @@ impl MacOSMenu { let bundle_name = format!("{name}.app"); let directories = Directories::new(menu_mode, &bundle_name); - tracing::info!("Installing menu item for {bundle_name}"); + tracing::info!("Editing menu item for {bundle_name}"); let refined_placeholders = placeholders.refine(&directories.location); Self { @@ -707,12 +707,16 @@ impl MacOSMenu { } pub fn remove(&self) -> Result, MenuInstError> { - println!("Removing {}", self.directories.location.display()); + tracing::info!("Removing menu item {}", self.directories.location.display()); self.maybe_register_with_launchservices(false)?; - fs_err::remove_dir_all(&self.directories.location).unwrap_or_else(|e| { - println!("Failed to remove directory: {e}. Ignoring error."); - }); - Ok(vec![self.directories.location.clone()]) + if self.directories.location.exists() { + fs_err::remove_dir_all(&self.directories.location).unwrap_or_else(|e| { + tracing::warn!("Failed to remove directory: {e}. Ignoring error."); + }); + return Ok(vec![self.directories.location.clone()]); + } else { + return Ok(vec![]); + } } } @@ -736,14 +740,4 @@ pub(crate) fn remove_menu_item( ) -> Result, MenuInstError> { let menu = MacOSMenu::new(prefix, macos_item, command, menu_mode, placeholders); menu.remove() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_slugify() { - assert_eq!(slugify("Hello, World!"), "hello-world"); - } -} +} \ No newline at end of file diff --git a/crates/rattler_menuinst/src/schema.rs b/crates/rattler_menuinst/src/schema.rs index 65a3ccfe2..c929a0b18 100644 --- a/crates/rattler_menuinst/src/schema.rs +++ b/crates/rattler_menuinst/src/schema.rs @@ -107,6 +107,7 @@ pub struct NameComplex { pub enum Environment { Base, + #[allow(dead_code)] NotBase, } diff --git a/crates/rattler_menuinst/src/slugify.rs b/crates/rattler_menuinst/src/slugify.rs index f5bc40984..19f4509ab 100644 --- a/crates/rattler_menuinst/src/slugify.rs +++ b/crates/rattler_menuinst/src/slugify.rs @@ -22,6 +22,7 @@ mod tests { #[test] fn test_basic_slugify() { assert_eq!(slugify("Hello World"), "hello-world"); + assert_eq!(slugify("Hello, World!"), "hello-world"); } #[test] diff --git a/crates/rattler_menuinst/src/util.rs b/crates/rattler_menuinst/src/util.rs index 351de58f6..b64a9f86c 100644 --- a/crates/rattler_menuinst/src/util.rs +++ b/crates/rattler_menuinst/src/util.rs @@ -1,3 +1,4 @@ +#[allow(dead_code)] pub fn log_output(cmd_info: &str, output: std::process::Output) { tracing::info!("{}: status {}", cmd_info, output.status); tracing::info!(