diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/appimage.rs | 133 | ||||
| -rw-r--r-- | src/main.rs | 18 | ||||
| -rw-r--r-- | src/manager.rs | 38 | ||||
| -rw-r--r-- | src/paths.rs | 8 |
4 files changed, 180 insertions, 17 deletions
diff --git a/src/appimage.rs b/src/appimage.rs index 5d0f8e5..bf0de6d 100644 --- a/src/appimage.rs +++ b/src/appimage.rs @@ -1,5 +1,8 @@ use serde::{Deserialize, Serialize}; -use std::path::PathBuf; +use std::{path::PathBuf, process::Command}; +use tokio::fs; + +use crate::{Error, InstallArgs, Result, desktops_dir, icons_dir}; #[derive(Debug, Serialize, Deserialize)] pub struct AppImage { @@ -18,3 +21,131 @@ pub struct Source { pub struct SourceMetadata { pub url: String, } + +impl AppImage { + pub fn new(options: &InstallArgs) -> Self { + Self { + file_path: PathBuf::new(), + executable: options + .executable + .as_ref() + .unwrap_or(&options.appname) + .to_string(), + source: Source { + identifier: if options.github { + "git.github".to_string() + } else { + "raw_url".to_string() + }, + meta: SourceMetadata { + url: options.from.clone(), + }, + }, + } + } + 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/main.rs b/src/main.rs index e4bc296..3081598 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,7 @@ use clap::Parser; use colored::Colorize; -use std::path::PathBuf; -use zap_rs::{AppImage, Cli, Command, PackageManager, Result, Source, SourceMetadata}; +use zap_rs::{AppImage, Cli, Command, PackageManager, Result}; async fn run() -> Result<()> { let args = Cli::parse(); @@ -10,20 +9,9 @@ async fn run() -> Result<()> { match args.command { Command::Install(args) => { - let mut options = AppImage { - file_path: PathBuf::new(), - executable: args.executable.unwrap_or(args.appname.clone()), - source: Source { - identifier: if args.github { - "git.github".to_string() - } else { - "raw_url".to_string() - }, - meta: SourceMetadata { url: args.from }, - }, - }; + let mut appimage = AppImage::new(&args); - pm.install(&mut options, &args.appname).await?; + pm.install(&mut appimage, &args.appname).await?; } Command::Remove(args) => { pm.remove(&args.appname).await?; 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")) +} |
