This commit is contained in:
uncenter 2024-08-05 17:45:28 +08:00 committed by GitHub
commit c09437d55f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 50 additions and 19 deletions

1
Cargo.lock generated
View File

@ -3253,6 +3253,7 @@ dependencies = [
"serde_json",
"tokio",
"toml_edit",
"url",
"vergen-gitcl",
"yazi-boot",
"yazi-dds",

View File

@ -21,6 +21,7 @@ md-5 = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true }
toml_edit = "0.22.20"
url = "2.5.2"
[build-dependencies]
yazi-shared = { path = "../yazi-shared", version = "0.3.0" }

View File

@ -9,7 +9,7 @@ impl Package {
let path = self.local();
if !must_exists(&path).await {
Git::clone(&self.remote(), &path).await?;
Git::clone(&self.remote, &path).await?;
} else {
Git::pull(&path).await?;
};

View File

@ -9,7 +9,7 @@ impl Package {
let path = self.local();
if !must_exists(&path).await {
Git::clone(&self.remote(), &path).await?;
Git::clone(&self.remote, &path).await?;
} else {
Git::fetch(&path).await?;
};

View File

@ -2,28 +2,59 @@ use std::{borrow::Cow, io::BufWriter, path::PathBuf};
use anyhow::Result;
use md5::{Digest, Md5};
use url::Url;
use yazi_shared::Xdg;
pub(crate) struct Package {
pub(crate) repo: String,
pub(crate) child: String,
pub(crate) remote: String,
pub(crate) commit: String,
pub(super) is_flavor: bool,
}
impl Package {
pub(super) fn new(url: &str, commit: Option<&str>) -> Self {
let mut parts = url.splitn(2, '#');
match Url::parse(url) {
Ok(mut url) => {
let repo = url.path().trim_start_matches('/').to_string();
let child = match url.fragment() {
Some(fragment) => format!("{fragment}.yazi"),
None => String::new(),
};
url.set_fragment(None);
let remote = url.to_string();
let mut repo = parts.next().unwrap_or_default().to_owned();
let child = if let Some(s) = parts.next() {
format!("{s}.yazi")
} else {
repo.push_str(".yazi");
String::new()
};
return Self {
repo,
child,
remote,
commit: commit.unwrap_or_default().to_owned(),
is_flavor: false,
};
}
Err(_) => {
let mut parts = url.splitn(2, '#');
Self { repo, child, commit: commit.unwrap_or_default().to_owned(), is_flavor: false }
let mut repo = parts.next().unwrap_or_default().to_owned();
let child = if let Some(s) = parts.next() {
format!("{s}.yazi")
} else {
repo.push_str(".yazi");
String::new()
};
let remote = format!("https://github.com/{}.git", repo);
return Self {
repo,
child,
remote,
commit: commit.unwrap_or_default().to_owned(),
is_flavor: false,
};
}
}
}
#[inline]
@ -50,13 +81,7 @@ impl Package {
pub(super) fn local(&self) -> PathBuf {
Xdg::state_dir()
.join("packages")
.join(format!("{:x}", Md5::new_with_prefix(self.remote()).finalize()))
}
#[inline]
pub(super) fn remote(&self) -> String {
// Support more Git hosting services in the future
format!("https://github.com/{}.git", self.repo)
.join(format!("{:x}", Md5::new_with_prefix(&self.remote).finalize()))
}
pub(super) fn header(&self, s: &str) -> Result<()> {

View File

@ -21,6 +21,9 @@ impl Package {
if !package.commit.is_empty() {
table.insert("commit", package.commit.into());
}
if !package.remote.starts_with("https://github.com") {
table.insert("remote", package.remote.into());
}
if package.is_flavor {
doc["flavor"]["deps"].as_array_mut().unwrap().push(table);
@ -47,9 +50,10 @@ impl Package {
for dep in deps.iter_mut() {
let dep = dep.as_inline_table_mut().context("Dependency must be an inline table")?;
let use_ = dep.get("use").and_then(|d| d.as_str()).context("Missing `use` field")?;
let remote = dep.get("remote").and_then(|d| d.as_str());
let commit = dep.get("commit").and_then(|d| d.as_str());
let mut package = Package::new(use_, commit);
let mut package = Package::new(if let Some(remote) = remote { remote } else { use_ }, commit);
if upgrade {
package.upgrade().await?;
} else {