use std::cell::RefCell;
use std::collections::BTreeSet;
use std::marker::PhantomData;
use std::path::PathBuf;
use crate::cache::{now_unix, Index, IndexEntry, InstallSource};
use crate::config::{KitConfig, KitPaths};
use crate::error::{KitError, KitResult};
use crate::install::Installed;
use crate::lock::FileLock;
use crate::manifest::PluginManifest;
use crate::registry::Registry;
pub(super) mod install;
pub(super) mod list;
pub(super) mod load;
#[derive(Debug, Clone)]
pub struct PluginInfo {
pub name: String,
pub crate_name: String,
pub version: String,
pub family: Option<String>,
pub abi: Option<u32>,
pub dir: PathBuf,
pub source: InstallSource,
pub extra: toml::Table,
}
pub struct CratePluginKit<T> {
cfg: KitConfig,
paths: KitPaths,
registry: Registry,
loaded: RefCell<BTreeSet<String>>,
_host: PhantomData<fn() -> T>,
}
impl<T> CratePluginKit<T> {
pub fn new(cfg: KitConfig) -> KitResult<Self> {
let paths = cfg.paths()?;
std::fs::create_dir_all(&paths.plugins)?;
std::fs::create_dir_all(&paths.build)?;
let registry = Registry::new(&cfg);
Ok(Self {
cfg,
paths,
registry,
loaded: RefCell::new(BTreeSet::new()),
_host: PhantomData,
})
}
pub fn config(&self) -> &KitConfig {
&self.cfg
}
pub fn paths(&self) -> &KitPaths {
&self.paths
}
pub fn registry(&self) -> &Registry {
&self.registry
}
pub(super) fn lock(&self) -> KitResult<FileLock> {
FileLock::acquire(&self.paths.lock_file, self.cfg.lock_timeout)
}
pub(super) fn resolve_version(
&self,
crate_name: &str,
version: Option<&str>,
) -> KitResult<String> {
if let Some(v) = version {
return Ok(v.to_string());
}
match self.registry.view(crate_name)? {
Some(info) if !info.version.is_empty() => Ok(info.version),
Some(_) => Err(KitError::Registry(format!(
"no usable version of {crate_name} in the registry"
))),
None => Err(KitError::Registry(format!(
"{crate_name} was not found in the registry"
))),
}
}
pub(super) fn record(&self, installed: &Installed) -> KitResult<()> {
let mut idx = Index::load(&self.paths.index_file);
let abi = PluginManifest::read(
&self
.paths
.manifest_path(&installed.crate_name, &self.cfg.manifest_name),
)
.ok()
.and_then(|m| m.plugin.abi);
idx.insert(
&installed.crate_name,
IndexEntry {
version: installed.version.clone(),
abi,
root: installed.dir.clone(),
source: installed.source.clone(),
installed_at: now_unix(),
},
);
idx.save(&self.paths.index_file)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
use std::time::Duration;
#[repr(C)]
pub(super) struct FakeEntry {
abi_version: u32,
}
pub(super) const MANIFEST: &str = r#"
[plugin]
name = "foo"
version = "0.1.0"
abi = 1
family = "node"
[detect]
strong = ["fake.lock"]
"#;
pub(super) fn kit(root: &Path) -> CratePluginKit<FakeEntry> {
let cfg = KitConfig::new("myapp")
.with_data_dir(root)
.with_lock_timeout(Duration::from_millis(500));
CratePluginKit::new(cfg).expect("should build")
}
pub(super) fn place_plugin(root: &Path, crate_name: &str) -> PathBuf {
let dir = root.join("plugins").join(crate_name);
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("myapp-plugin.toml"), MANIFEST).unwrap();
dir
}
#[test]
fn new_creates_the_data_directories() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().join("store");
let k = kit(&root);
assert!(
k.paths().plugins.is_dir(),
"the plugins dir should have been created"
);
assert!(
k.paths().build.is_dir(),
"the build dir should have been created"
);
}
#[test]
fn config_is_exposed() {
let tmp = tempfile::tempdir().unwrap();
let k = kit(&tmp.path().join("store"));
assert_eq!(k.config().id, "myapp");
assert_eq!(k.config().crate_prefix, "myapp-plugin-");
}
}