fix: allow user keeping garbage directories in the package directory (#622)

This commit is contained in:
Myriad-Dreamin 2024-10-02 21:05:28 +08:00 committed by GitHub
parent 3e58f97866
commit 43796a3dce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,7 +1,10 @@
//! Package management tools.
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::OnceLock;
use parking_lot::Mutex;
use reflexo_typst::package::{PackageRegistry, PackageSpec};
use reflexo_typst::typst::prelude::*;
use tinymist_world::https::HttpsRegistry;
@ -66,27 +69,43 @@ pub fn list_package_by_namespace(
}
// namespace/package_name/version
// 2. package_name
let package_names = std::fs::read_dir(local_path).unwrap();
let Some(package_names) = once_log(std::fs::read_dir(local_path), "read local pacakge")
else {
continue;
};
for package in package_names {
let package = package.unwrap();
if !package.file_type().unwrap().is_dir() {
let Some(package) = once_log(package, "read package name") else {
continue;
};
if package.file_type().map_or(true, |ft| !ft.is_dir()) {
continue;
}
if package.file_name().to_string_lossy().starts_with('.') {
continue;
}
// 3. version
let versions = std::fs::read_dir(package.path()).unwrap();
let Some(versions) =
once_log(std::fs::read_dir(package.path()), "read package versions")
else {
continue;
};
for version in versions {
let version = version.unwrap();
if !version.file_type().unwrap().is_dir() {
let Some(version) = once_log(version, "read package version") else {
continue;
};
if version.file_type().map_or(true, |ft| !ft.is_dir()) {
continue;
}
if version.file_name().to_string_lossy().starts_with('.') {
continue;
}
let path = version.path();
let version = version.file_name().to_string_lossy().parse().unwrap();
let Some(version) = once_log(
version.file_name().to_string_lossy().parse(),
"parse package version",
) else {
continue;
};
let spec = PackageSpec {
namespace: ns.clone(),
name: package.file_name().to_string_lossy().into(),
@ -99,3 +118,18 @@ pub fn list_package_by_namespace(
packages
}
fn once_log<T, E: std::fmt::Display>(result: Result<T, E>, site: &'static str) -> Option<T> {
let err = match result {
Ok(value) => return Some(value),
Err(err) => err,
};
static ONCES: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
let mut onces = ONCES.get_or_init(Default::default).lock();
if onces.insert(site) {
log::error!("failed to perform {site}: {err}");
}
None
}