feat: support ya for miscellaneous files in folder assets

This commit is contained in:
zooeywm 2024-11-30 11:30:20 +08:00 committed by sxyazi
parent 4194befb88
commit ea75fe2b5c
No known key found for this signature in database
2 changed files with 33 additions and 2 deletions

View file

@ -1,6 +1,6 @@
use anyhow::{Context, Result, bail};
use tokio::fs;
use yazi_shared::{Xdg, fs::{maybe_exists, must_exists}};
use yazi_shared::{Xdg, fs::{copy_dir_all, maybe_exists, must_exists}};
use super::Package;
@ -36,6 +36,8 @@ For safety, please manually delete it from your plugin/flavor directory and re-r
&["init.lua", "README.md", "LICENSE"][..]
};
let dirs = &["assets"][..];
for file in files {
let (from, to) = (from.join(file), to.join(file));
@ -44,6 +46,18 @@ 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()))?;
}
for dir in dirs {
let (from, to) = (from.join(dir), to.join(dir));
if !from.exists() {
continue;
}
copy_dir_all(&from, &to).await.with_context(|| {
format!("failed to copy dir `{}` to `{}`", from.display(), to.display())
})?;
}
println!("Done!");
Ok(())
}

View file

@ -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)
@ -298,6 +298,23 @@ pub async fn remove_dir_clean(dir: &Path) {
fs::remove_dir(dir).await.ok();
}
pub async fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
fs::create_dir_all(&dst).await?;
let mut entries = fs::read_dir(src).await?;
while let Some(entry) = entries.next_entry().await? {
let file_type = entry.file_type().await?;
let dest_path = dst.as_ref().join(entry.file_name());
if file_type.is_dir() {
Box::pin(copy_dir_all(entry.path(), dest_path)).await?;
} else {
fs::copy(entry.path(), dest_path).await.ok();
}
}
Ok(())
}
// Convert a file mode to a string representation
#[cfg(unix)]
#[allow(clippy::collapsible_else_if)]