Skip to content

Commit

Permalink
feat: support assets installation for the ya pack subcommand (#1973)
Browse files Browse the repository at this point in the history
Co-authored-by: sxyazi <[email protected]>
  • Loading branch information
zooeywm and sxyazi authored Dec 1, 2024
1 parent 4194bef commit 91665b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion yazi-cli/src/package/deploy.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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(())
}
}
2 changes: 1 addition & 1 deletion yazi-shared/src/fs/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 91665b7

Please sign in to comment.