From 91665b77137b8ea3fbac6d1ade60a38d112cfc91 Mon Sep 17 00:00:00 2001 From: zooeywm Date: Sun, 1 Dec 2024 18:49:07 +0800 Subject: [PATCH] feat: support `assets` installation for the `ya pack` subcommand (#1973) Co-authored-by: sxyazi --- yazi-cli/src/package/deploy.rs | 39 +++++++++++++++++++++++++++++++++- yazi-shared/src/fs/fns.rs | 2 +- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/yazi-cli/src/package/deploy.rs b/yazi-cli/src/package/deploy.rs index 6bdc1056..cf4b27cc 100644 --- a/yazi-cli/src/package/deploy.rs +++ b/yazi-cli/src/package/deploy.rs @@ -1,6 +1,8 @@ +use std::path::PathBuf; + use anyhow::{Context, Result, bail}; use tokio::fs; -use yazi_shared::{Xdg, fs::{maybe_exists, must_exists}}; +use yazi_shared::{Xdg, fs::{maybe_exists, must_exists, remove_dir_clean}}; use super::Package; @@ -44,7 +46,42 @@ For safety, please manually delete it from your plugin/flavor directory and re-r .with_context(|| format!("failed to copy `{}` to `{}`", from.display(), to.display()))?; } + Self::deploy_assets(from.join("assets"), to.join("assets")).await?; + println!("Done!"); Ok(()) } + + async fn deploy_assets(from: PathBuf, to: PathBuf) -> Result<()> { + use std::io::ErrorKind::NotFound; + + match fs::read_dir(&to).await { + Ok(mut it) => { + while let Some(entry) = it.next_entry().await? { + fs::remove_file(entry.path()) + .await + .with_context(|| format!("failed to remove `{}`", entry.path().display()))?; + } + } + Err(e) if e.kind() == NotFound => {} + Err(e) => Err(e).context(format!("failed to read `{}`", to.display()))?, + }; + + remove_dir_clean(&to).await; + match fs::read_dir(&from).await { + Ok(mut it) => { + fs::create_dir_all(&to).await?; + while let Some(entry) = it.next_entry().await? { + let (src, dist) = (entry.path(), to.join(entry.file_name())); + fs::copy(&src, &dist).await.with_context(|| { + format!("failed to copy `{}` to `{}`", src.display(), dist.display()) + })?; + } + } + Err(e) if e.kind() == NotFound => {} + Err(e) => Err(e).context(format!("failed to read `{}`", from.display()))?, + } + + Ok(()) + } } diff --git a/yazi-shared/src/fs/fns.rs b/yazi-shared/src/fs/fns.rs index 0ce8a9b5..3487afc4 100644 --- a/yazi-shared/src/fs/fns.rs +++ b/yazi-shared/src/fs/fns.rs @@ -258,7 +258,7 @@ async fn _copy_with_progress(from: PathBuf, to: PathBuf, cha: Cha) -> io::Result tokio::task::spawn_blocking(move || { let mut reader = std::fs::File::open(from)?; let mut writer = std::fs::OpenOptions::new() - .mode(cha.mode as u32) + .mode(cha.mode) .write(true) .create(true) .truncate(true)