mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: delete/uninstall packages
This commit is contained in:
parent
856f37b5b3
commit
0dbce06a6e
8 changed files with 74 additions and 18 deletions
|
|
@ -57,6 +57,9 @@ pub(super) struct CommandPack {
|
||||||
/// Add a package.
|
/// Add a package.
|
||||||
#[arg(short = 'a', long)]
|
#[arg(short = 'a', long)]
|
||||||
pub(super) add: Option<String>,
|
pub(super) add: Option<String>,
|
||||||
|
/// Delete a package.
|
||||||
|
#[arg(short = 'd', long)]
|
||||||
|
pub(super) delete: Option<String>,
|
||||||
/// Install all packages.
|
/// Install all packages.
|
||||||
#[arg(short = 'i', long)]
|
#[arg(short = 'i', long)]
|
||||||
pub(super) install: bool,
|
pub(super) install: bool,
|
||||||
|
|
|
||||||
|
|
@ -73,6 +73,8 @@ async fn run() -> anyhow::Result<()> {
|
||||||
package::Package::load().await?.install(true).await?;
|
package::Package::load().await?.install(true).await?;
|
||||||
} else if let Some(repo) = cmd.add {
|
} else if let Some(repo) = cmd.add {
|
||||||
package::Package::load().await?.add(&repo).await?;
|
package::Package::load().await?.add(&repo).await?;
|
||||||
|
} else if let Some(repo) = cmd.delete {
|
||||||
|
package::Package::load().await?.delete(&repo).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
26
yazi-cli/src/package/delete.rs
Normal file
26
yazi-cli/src/package/delete.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
use anyhow::{Result, bail};
|
||||||
|
use tokio::fs;
|
||||||
|
use yazi_fs::must_exists;
|
||||||
|
use yazi_macro::outln;
|
||||||
|
|
||||||
|
use super::Dependency;
|
||||||
|
|
||||||
|
impl Dependency {
|
||||||
|
pub(super) async fn delete(&self) -> Result<()> {
|
||||||
|
self.header("Deleting package `{name}`")?;
|
||||||
|
|
||||||
|
let path = self.deployed_directory();
|
||||||
|
|
||||||
|
if must_exists(&path).await {
|
||||||
|
fs::remove_dir_all(&path).await?;
|
||||||
|
} else {
|
||||||
|
bail!(
|
||||||
|
"The package.toml file states that `{}` exists, but the directory was not found. The entry will be removed from package.toml.",
|
||||||
|
self.name
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
outln!("Done!")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -27,6 +27,15 @@ impl Dependency {
|
||||||
.join(format!("{:x}", XxHash3_128::oneshot(self.remote().as_bytes())))
|
.join(format!("{:x}", XxHash3_128::oneshot(self.remote().as_bytes())))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(super) fn deployed_directory(&self) -> PathBuf {
|
||||||
|
return if self.is_flavor {
|
||||||
|
Xdg::config_dir().join(format!("flavors/{}", self.name))
|
||||||
|
} else {
|
||||||
|
Xdg::config_dir().join(format!("plugins/{}", self.name))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(super) fn remote(&self) -> String {
|
pub(super) fn remote(&self) -> String {
|
||||||
// Support more Git hosting services in the future
|
// Support more Git hosting services in the future
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use yazi_fs::{Xdg, copy_and_seal, maybe_exists, remove_dir_clean};
|
use yazi_fs::{copy_and_seal, maybe_exists, remove_dir_clean};
|
||||||
use yazi_macro::outln;
|
use yazi_macro::outln;
|
||||||
|
|
||||||
use super::Dependency;
|
use super::Dependency;
|
||||||
|
|
@ -13,11 +13,8 @@ impl Dependency {
|
||||||
|
|
||||||
self.header("Deploying package `{name}`")?;
|
self.header("Deploying package `{name}`")?;
|
||||||
self.is_flavor = maybe_exists(&from.join("flavor.toml")).await;
|
self.is_flavor = maybe_exists(&from.join("flavor.toml")).await;
|
||||||
let to = if self.is_flavor {
|
|
||||||
Xdg::config_dir().join(format!("flavors/{}", self.name))
|
let to = self.deployed_directory();
|
||||||
} else {
|
|
||||||
Xdg::config_dir().join(format!("plugins/{}", self.name))
|
|
||||||
};
|
|
||||||
|
|
||||||
if maybe_exists(&to).await && self.hash != self.hash().await? {
|
if maybe_exists(&to).await && self.hash != self.hash().await? {
|
||||||
bail!(
|
bail!(
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,13 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
use twox_hash::XxHash3_128;
|
use twox_hash::XxHash3_128;
|
||||||
use yazi_fs::{Xdg, ok_or_not_found};
|
use yazi_fs::ok_or_not_found;
|
||||||
|
|
||||||
use super::Dependency;
|
use super::Dependency;
|
||||||
|
|
||||||
impl Dependency {
|
impl Dependency {
|
||||||
pub(crate) async fn hash(&self) -> Result<String> {
|
pub(crate) async fn hash(&self) -> Result<String> {
|
||||||
let dir = if self.is_flavor {
|
let dir = self.deployed_directory();
|
||||||
Xdg::config_dir().join(format!("flavors/{}", self.name))
|
|
||||||
} else {
|
|
||||||
Xdg::config_dir().join(format!("plugins/{}", self.name))
|
|
||||||
};
|
|
||||||
|
|
||||||
let files = if self.is_flavor {
|
let files = if self.is_flavor {
|
||||||
&[
|
&[
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
#![allow(clippy::module_inception)]
|
#![allow(clippy::module_inception)]
|
||||||
|
|
||||||
yazi_macro::mod_flat!(add dependency deploy git hash install package upgrade);
|
yazi_macro::mod_flat!(add delete dependency deploy git hash install package upgrade);
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use yazi_fs::Xdg;
|
use yazi_fs::Xdg;
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,10 @@ impl Package {
|
||||||
|
|
||||||
pub(crate) async fn add(&mut self, use_: &str) -> Result<()> {
|
pub(crate) async fn add(&mut self, use_: &str) -> Result<()> {
|
||||||
let mut dep = Dependency::from_str(use_)?;
|
let mut dep = Dependency::from_str(use_)?;
|
||||||
if self.plugins.iter().any(|d| d.parent == dep.parent && d.child == dep.child) {
|
|
||||||
bail!("Plugin `{}` already exists in package.toml", dep.name);
|
if let Some(existing_dep) = self.find_dep_in_package(&dep) {
|
||||||
}
|
let package_type = if existing_dep.is_flavor { "Flavor" } else { "Plugin" };
|
||||||
if self.flavors.iter().any(|d| d.parent == dep.parent && d.child == dep.child) {
|
bail!("{} `{}` already exists in package.toml", package_type, dep.name)
|
||||||
bail!("Flavor `{}` already exists in package.toml", dep.name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dep.add().await?;
|
dep.add().await?;
|
||||||
|
|
@ -43,6 +42,25 @@ impl Package {
|
||||||
create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml")
|
create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn delete(&mut self, use_: &str) -> Result<()> {
|
||||||
|
let dep_to_find = Dependency::from_str(use_)?;
|
||||||
|
|
||||||
|
let dep = match self.find_dep_in_package(&dep_to_find) {
|
||||||
|
Some(d) => d,
|
||||||
|
None => bail!("`{}` was not found in package.toml", use_),
|
||||||
|
};
|
||||||
|
|
||||||
|
dep.delete().await?;
|
||||||
|
if dep.is_flavor {
|
||||||
|
self.flavors.retain(|f| f.use_ != use_);
|
||||||
|
} else {
|
||||||
|
self.plugins.retain(|f| f.use_ != use_);
|
||||||
|
}
|
||||||
|
|
||||||
|
let s = toml::to_string_pretty(self)?;
|
||||||
|
create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml")
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn install(&mut self, upgrade: bool) -> Result<()> {
|
pub(crate) async fn install(&mut self, upgrade: bool) -> Result<()> {
|
||||||
for d in &mut self.plugins {
|
for d in &mut self.plugins {
|
||||||
if upgrade {
|
if upgrade {
|
||||||
|
|
@ -165,6 +183,11 @@ impl Package {
|
||||||
create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml")
|
create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn find_dep_in_package(&self, dep: &Dependency) -> Option<&Dependency> {
|
||||||
|
return self.plugins.iter().chain(self.flavors.iter()).find(|d| d.use_ == dep.use_);
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn toml() -> PathBuf { Xdg::config_dir().join("package.toml") }
|
fn toml() -> PathBuf { Xdg::config_dir().join("package.toml") }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue