tinymist_query/
package.rsuse std::collections::HashSet;
use std::path::PathBuf;
use std::sync::OnceLock;
use ecow::{eco_format, eco_vec, EcoVec};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use tinymist_world::package::registry::HttpRegistry;
use tinymist_world::package::PackageSpec;
use typst::diag::{EcoString, StrResult};
use typst::syntax::package::PackageManifest;
use typst::syntax::{FileId, VirtualPath};
use typst::World;
use crate::LocalContext;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageInfo {
pub path: PathBuf,
pub namespace: EcoString,
pub name: EcoString,
pub version: String,
}
impl From<(PathBuf, PackageSpec)> for PackageInfo {
fn from((path, spec): (PathBuf, PackageSpec)) -> Self {
Self {
path,
namespace: spec.namespace,
name: spec.name,
version: spec.version.to_string(),
}
}
}
pub fn get_manifest_id(spec: &PackageInfo) -> StrResult<FileId> {
Ok(FileId::new(
Some(PackageSpec {
namespace: spec.namespace.clone(),
name: spec.name.clone(),
version: spec.version.parse()?,
}),
VirtualPath::new("typst.toml"),
))
}
pub fn get_manifest(world: &dyn World, toml_id: FileId) -> StrResult<PackageManifest> {
let toml_data = world
.file(toml_id)
.map_err(|err| eco_format!("failed to read package manifest ({})", err))?;
let string = std::str::from_utf8(&toml_data)
.map_err(|err| eco_format!("package manifest is not valid UTF-8 ({})", err))?;
toml::from_str(string)
.map_err(|err| eco_format!("package manifest is malformed ({})", err.message()))
}
pub fn check_package(ctx: &mut LocalContext, spec: &PackageInfo) -> StrResult<()> {
let toml_id = get_manifest_id(spec)?;
let manifest = ctx.get_manifest(toml_id)?;
let entry_point = toml_id.join(&manifest.package.entrypoint);
ctx.shared_().preload_package(entry_point);
Ok(())
}
pub fn list_package_by_namespace(
registry: &HttpRegistry,
ns: EcoString,
) -> EcoVec<(PathBuf, PackageSpec)> {
let mut packages = eco_vec![];
log::info!(
"searching for packages in namespace {ns} in paths {:?}",
registry.paths()
);
for dir in registry.paths() {
let local_path = dir.join(ns.as_str());
if !local_path.exists() || !local_path.is_dir_follow_links() {
continue;
}
let Some(package_names) = once_log(std::fs::read_dir(local_path), "read local package")
else {
continue;
};
for package in package_names {
let Some(package) = once_log(package, "read package name") else {
continue;
};
if package.file_name().to_string_lossy().starts_with('.') {
continue;
}
let package_path = package.path();
if !package_path.is_dir_follow_links() {
continue;
}
let Some(versions) = once_log(std::fs::read_dir(package_path), "read package versions")
else {
continue;
};
for version in versions {
let Some(version) = once_log(version, "read package version") else {
continue;
};
if version.file_name().to_string_lossy().starts_with('.') {
continue;
}
let package_version_path = version.path();
if !package_version_path.is_dir_follow_links() {
continue;
}
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(),
version,
};
packages.push((package_version_path, spec));
}
}
}
packages
}
trait IsDirFollowLinks {
fn is_dir_follow_links(&self) -> bool;
}
impl IsDirFollowLinks for PathBuf {
fn is_dir_follow_links(&self) -> bool {
self.canonicalize()
.map(|meta| meta.is_dir())
.unwrap_or(false)
}
}
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 ONCE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();
let mut once = ONCE.get_or_init(Default::default).lock();
if once.insert(site) {
log::error!("failed to perform {site}: {err}");
}
None
}