diff options
| author | Naz <ndpm13@ch-naseem.com> | 2025-08-08 17:18:43 +0100 |
|---|---|---|
| committer | Naz <ndpm13@ch-naseem.com> | 2025-08-08 17:18:43 +0100 |
| commit | e961fad84734ba750386ed463057bacedf24bc17 (patch) | |
| tree | a747865f84c549c13a77e93c8a0801fb48f0d2e2 /src | |
| parent | b0d4e60e525e2438e02fa3e3b5ffaba374a03a71 (diff) | |
✨feat: add desktop integration logic to AppImage and utilize it in PackageManager
Diffstat (limited to 'src')
| -rw-r--r-- | src/appimage.rs | 110 | ||||
| -rw-r--r-- | src/manager.rs | 38 | ||||
| -rw-r--r-- | src/paths.rs | 8 |
3 files changed, 153 insertions, 3 deletions
diff --git a/src/appimage.rs b/src/appimage.rs index c3f61b1..bf0de6d 100644 --- a/src/appimage.rs +++ b/src/appimage.rs @@ -1,7 +1,8 @@ use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{path::PathBuf, process::Command}; +use tokio::fs; -use crate::InstallArgs; +use crate::{Error, InstallArgs, Result, desktops_dir, icons_dir}; #[derive(Debug, Serialize, Deserialize)] pub struct AppImage { @@ -42,4 +43,109 @@ impl AppImage { }, } } + async fn extract_assets(&self) -> Result<PathBuf> { + let temp_dir = std::env::temp_dir().join("zap-rs"); + + fs::create_dir_all(&temp_dir).await?; + + // Extract desktop file + Command::new(&self.file_path) + .arg("--appimage-extract") + .arg("*.desktop") + .current_dir(&temp_dir) + .spawn()? + .wait()?; + + // Extract icon + Command::new(&self.file_path) + .arg("--appimage-extract") + .arg("usr/share/icons/hicolor/512x512/apps/*.png") + .current_dir(&temp_dir) + .spawn()? + .wait()?; + + Ok(temp_dir) + } + async fn fix_desktop(&self, desktop_file_path: &PathBuf) -> Result<()> { + let file_content = fs::read_to_string(&desktop_file_path).await?; + + let appimage_path = self.file_path.to_str().ok_or(Error::InvalidPath)?; + + let icon_path = icons_dir()? + .join(format!("{}.png", self.executable)) + .to_str() + .ok_or(Error::InvalidPath)? + .to_string(); + + let fixed_file_content: Vec<String> = file_content + .lines() + .map(|line| { + if line.contains("Exec=") { + if let Some(exec_line) = line.split_once(" ") { + if let Some(exec_arg) = exec_line.0.split_once("=") { + format!("{}={} {}", exec_arg.0, appimage_path, exec_line.1) + } else { + line.to_string() + } + } else if let Some(exec_arg) = line.split_once("=") { + format!("{}={}", exec_arg.0, appimage_path) + } else { + line.to_string() + } + } else if line.contains("Icon=") { + if let Some(exec_arg) = line.split_once("=") { + format!("{}={}", exec_arg.0, icon_path) + } else { + line.to_string() + } + } else { + line.to_string() + } + }) + .collect(); + + fs::write(desktop_file_path, fixed_file_content.join("\n")).await?; + + Ok(()) + } + pub async fn integrate_desktop(&self) -> Result<()> { + let temp_dir = self.extract_assets().await?; + let squashfs = &temp_dir.join("squashfs-root"); + + fs::create_dir_all(desktops_dir()?).await?; + fs::create_dir_all(icons_dir()?).await?; + + let icon_path = icons_dir()?.join(format!("{}.png", self.executable)); + let desktop_file_paths = ( + desktops_dir()?.join(format!("{}.desktop", self.executable)), + PathBuf::from(std::env::var("HOME")?).join(format!( + ".local/share/applications/{}.desktop", + self.executable + )), + ); + + let mut squashfs_entries = fs::read_dir(&squashfs).await?; + while let Some(entry) = squashfs_entries.next_entry().await? { + if entry.path().extension() == Some("desktop".as_ref()) { + fs::copy(entry.path(), &desktop_file_paths.0).await?; + + self.fix_desktop(&desktop_file_paths.0).await?; + + fs::copy(&desktop_file_paths.0, &desktop_file_paths.1).await?; + } + } + + let mut squashfs_icon_entries = + fs::read_dir(&squashfs.join("usr/share/icons/hicolor/512x512/apps")).await?; + while let Some(entry) = squashfs_icon_entries.next_entry().await? { + if entry.path().extension() == Some("png".as_ref()) { + fs::copy(entry.path(), &icon_path).await?; + } + } + + // Clean up + fs::remove_dir_all(temp_dir).await?; + + Ok(()) + } } diff --git a/src/manager.rs b/src/manager.rs index 15f28d9..bdb152a 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -1,7 +1,12 @@ +use std::{ + io::{self, Write}, + path::PathBuf, +}; use tokio::fs; use crate::{ - AppImage, Downloader, Index, Result, SymlinkManager, get_github_release_url, index_dir, + AppImage, Downloader, Index, Result, SymlinkManager, desktops_dir, get_github_release_url, + icons_dir, index_dir, }; #[derive(Debug, Default)] @@ -44,6 +49,17 @@ impl PackageManager { self.index.add(appimage, appname).await?; self.symlink_manager.create(appimage).await?; + + print!("Do you want to integrate this appimage? (y/N) "); + io::stdout().flush()?; + + let mut input = String::new(); + io::stdin().read_line(&mut input)?; + + if input.to_lowercase().trim() == "y" || input.to_lowercase().trim() == "yes" { + appimage.integrate_desktop().await?; + } + Ok(()) } pub async fn remove(&self, appname: &str) -> Result<()> { @@ -53,6 +69,26 @@ impl PackageManager { self.symlink_manager.remove(&appimage.executable).await?; self.index.remove(appname).await?; + if fs::try_exists(desktops_dir()?.join(format!("{}.desktop", appimage.executable))).await? { + fs::remove_file(desktops_dir()?.join(format!("{}.desktop", appimage.executable))) + .await?; + } + if fs::try_exists(PathBuf::from(std::env::var("HOME")?).join(format!( + ".local/share/applications/{}.desktop", + appimage.executable + ))) + .await? + { + fs::remove_file(PathBuf::from(std::env::var("HOME")?).join(format!( + ".local/share/applications/{}.desktop", + appimage.executable + ))) + .await?; + } + if fs::try_exists(icons_dir()?.join(format!("{}.png", appimage.executable))).await? { + fs::remove_file(icons_dir()?.join(format!("{}.png", appimage.executable))).await?; + } + Ok(()) } pub async fn list(&self) -> Result<()> { diff --git a/src/paths.rs b/src/paths.rs index 7bbc8f8..c52a84e 100644 --- a/src/paths.rs +++ b/src/paths.rs @@ -14,3 +14,11 @@ pub fn index_dir() -> Result<PathBuf> { pub fn appimages_dir() -> Result<PathBuf> { Ok(zap_rs_home()?.join("appimages")) } + +pub fn desktops_dir() -> Result<PathBuf> { + Ok(zap_rs_home()?.join("desktops")) +} + +pub fn icons_dir() -> Result<PathBuf> { + Ok(zap_rs_home()?.join("icons")) +} |
