use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, PoisonError};
use airsl::mlua::{self, Lua, Table, Value};
use buildl_core::{EvaluationFailure, IgnoreRules, PluginName, PluginRef};
use crate::module::{guarded, plugin_chunk_name};
use crate::plugin::environment;
use crate::plugin::manifest::read_manifest;
use crate::refusal::Refusal;
use crate::results::Results;
use crate::sources::Sources;
use crate::staging::Staging;
use crate::values;
#[derive(Debug, Clone)]
pub(crate) struct Plugins<I> {
shared: Arc<Shared<I>>,
}
#[derive(Debug)]
struct Shared<I> {
root: PathBuf,
directories: BTreeMap<PluginName, PathBuf>,
staging: Arc<Mutex<Staging>>,
sources: Sources<I>,
results: Results,
instances: Mutex<BTreeMap<Vec<PluginName>, Value>>,
}
enum Found {
Loaded(Value),
Fresh(Box<Fresh>),
}
struct Fresh {
plugin: PluginRef,
directory: PathBuf,
entry_file: PathBuf,
label: String,
chunk: String,
text: String,
}
impl<I: IgnoreRules + Clone + Send + 'static> Plugins<I> {
pub(crate) fn new(
root: &Path,
directories: BTreeMap<PluginName, PathBuf>,
staging: &Arc<Mutex<Staging>>,
sources: &Sources<I>,
results: &Results,
) -> Self {
Self {
shared: Arc::new(Shared {
root: root.to_path_buf(),
directories,
staging: Arc::clone(staging),
sources: sources.clone(),
results: results.clone(),
instances: Mutex::new(BTreeMap::new()),
}),
}
}
pub(super) fn staging(&self) -> &Arc<Mutex<Staging>> {
&self.shared.staging
}
pub(super) fn sources(&self) -> &Sources<I> {
&self.shared.sources
}
pub(super) fn results(&self) -> &Results {
&self.shared.results
}
pub(crate) fn bind_use(
&self,
lua: &Lua,
table: &Table,
chain: &[PluginRef],
) -> mlua::Result<()> {
let plugins = self.clone();
let chain = chain.to_vec();
let function =
lua.create_function(move |lua, name: Value| plugins.load(lua, &chain, &name))?;
table.set("use", function)
}
fn load(&self, lua: &Lua, chain: &[PluginRef], name: &Value) -> mlua::Result<Value> {
let found = guarded(lua, &self.shared.staging, "use", |_| self.find(chain, name))?;
let fresh = match found {
Found::Loaded(module) => return Ok(module),
Found::Fresh(fresh) => fresh,
};
let mut chain = chain.to_vec();
chain.push(fresh.plugin);
let environment = environment::build(
lua,
self,
&chain,
&fresh.directory,
&fresh.label,
&fresh.entry_file,
)?;
let module = lua
.load(fresh.text)
.set_name(plugin_chunk_name(&fresh.chunk))
.set_environment(environment)
.eval::<Value>()?;
self.shared
.instances
.lock()
.unwrap_or_else(PoisonError::into_inner)
.insert(
chain.into_iter().map(|plugin| plugin.name).collect(),
module.clone(),
);
Ok(module)
}
fn find(&self, chain: &[PluginRef], name: &Value) -> Result<Found, Refusal> {
let text = values::string(name, "name")?;
let refuse = |detail: String| Refusal::new(EvaluationFailure::Refused, detail).about(&text);
let Some((name, directory)) = PluginName::parse(&text)
.ok()
.and_then(|name| self.shared.directories.get_key_value(&name))
else {
return Err(refuse(
"the workspace's [plugins] names no such plugin".to_owned(),
));
};
if chain.iter().any(|plugin| &plugin.name == name) {
let path: Vec<String> = chain
.iter()
.map(|plugin| plugin.name.to_string())
.chain([name.to_string()])
.collect();
return Err(refuse(format!(
"use cycle: {} would use a plugin it is already inside",
path.join(" > ")
)));
}
let key: Vec<PluginName> = chain
.iter()
.map(|plugin| plugin.name.clone())
.chain([name.clone()])
.collect();
if let Some(module) = self
.shared
.instances
.lock()
.unwrap_or_else(PoisonError::into_inner)
.get(&key)
{
return Ok(Found::Loaded(module.clone()));
}
let canonical = fs::canonicalize(directory).map_err(|error| {
Refusal::new(
EvaluationFailure::Runtime,
format!("{}: {error}", directory.display()),
)
.about(&text)
})?;
let label = self.label(&canonical);
let (plugin, entry) = read_manifest(&canonical, &label, name)
.map_err(|diagnostic| refuse(diagnostic.to_string()))?;
let entry_file = fs::canonicalize(canonical.join(&entry))
.map_err(|error| refuse(format!("{}: {error}", directory.join(&entry).display())))?;
let source = fs::read_to_string(&entry_file).map_err(|error| {
Refusal::new(
EvaluationFailure::Runtime,
format!("{}: {error}", entry_file.display()),
)
.about(&text)
})?;
Ok(Found::Fresh(Box::new(Fresh {
plugin,
chunk: format!("{label}/{}", entry.display()),
label,
directory: canonical,
entry_file,
text: source,
})))
}
fn label(&self, directory: &Path) -> String {
directory
.strip_prefix(&self.shared.root)
.unwrap_or(directory)
.to_string_lossy()
.into_owned()
}
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
clippy::panic,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use airsl::mlua::{Lua, Value};
use buildl_core::{
Directory, EvaluationFailure, NothingIgnored, PluginName, PluginRef, PluginVersion,
};
use super::{Found, Plugins};
use crate::limits::DeclarationLimits;
use crate::refusal::Refusal;
use crate::results::Results;
use crate::sources::Sources;
use crate::staging::Staging;
fn workspace() -> (tempfile::TempDir, PathBuf, Plugins<NothingIgnored>) {
let dir = tempfile::tempdir().unwrap();
let root = fs::canonicalize(dir.path()).unwrap();
let plugin = root.join("plugins/p");
fs::create_dir_all(&plugin).unwrap();
fs::write(
plugin.join("extension.toml"),
"[extension]\nname = \"p\"\nversion = \"2.0.0\"\nentry = \"main.lua\"\napi = 1\n",
)
.unwrap();
fs::write(plugin.join("main.lua"), "return {}").unwrap();
let staging = Arc::new(Mutex::new(Staging::new(u64::MAX)));
let sources = Sources::new(
&root,
&Directory::root(),
DeclarationLimits::default().walk_entries(),
&[],
NothingIgnored,
);
let directories = BTreeMap::from([(PluginName::parse("p").unwrap(), plugin.clone())]);
let plugins = Plugins::new(&root, directories, &staging, &sources, &Results::none());
(dir, plugin, plugins)
}
fn name(lua: &Lua, text: &str) -> Value {
Value::String(lua.create_string(text).unwrap())
}
fn reference(name: &str) -> PluginRef {
PluginRef {
unkeyed: BTreeSet::new(),
name: PluginName::parse(name).unwrap(),
version: PluginVersion::parse("1.0.0").unwrap(),
run: BTreeSet::new(),
env: BTreeSet::new(),
}
}
fn refused(found: Result<Found, Refusal>) -> String {
match found {
Err(refusal) => {
assert_eq!(refusal.failure(), &EvaluationFailure::Refused);
refusal.diagnostic("use", None).to_string()
}
Ok(_) => panic!("expected a refusal"),
}
}
#[test]
fn a_name_the_workspace_does_not_list_is_refused() {
let (_dir, _plugin, plugins) = workspace();
let lua = Lua::new();
let said = refused(plugins.find(&[], &name(&lua, "nope")));
assert_eq!(
said,
"buildl.use 'nope': the workspace's [plugins] names no such plugin"
);
}
#[test]
fn text_that_is_not_a_plugin_name_is_refused_as_unknown() {
let (_dir, _plugin, plugins) = workspace();
let lua = Lua::new();
let said = refused(plugins.find(&[], &name(&lua, "not a name")));
assert!(said.contains("names no such plugin"), "{said}");
}
#[test]
fn a_value_that_is_not_text_is_refused() {
let (_dir, _plugin, plugins) = workspace();
let Err(refusal) = plugins.find(&[], &Value::Integer(1)) else {
panic!("expected a refusal");
};
assert!(matches!(
refusal.failure(),
EvaluationFailure::WrongFieldType { .. }
));
}
#[test]
fn a_plugin_already_in_the_chain_is_a_cycle() {
let (_dir, _plugin, plugins) = workspace();
let lua = Lua::new();
let chain = [reference("a"), reference("p")];
let said = refused(plugins.find(&chain, &name(&lua, "p")));
assert!(said.contains("use cycle: a > p > p"), "{said}");
}
#[test]
fn a_plugin_to_load_carries_its_manifest_and_a_workspace_relative_chunk() {
let (_dir, plugin, plugins) = workspace();
let lua = Lua::new();
let Ok(Found::Fresh(fresh)) = plugins.find(&[], &name(&lua, "p")) else {
panic!("the plugin is there to load");
};
assert_eq!(fresh.plugin.to_string(), "p@2.0.0");
assert_eq!(fresh.label, "plugins/p");
assert_eq!(fresh.chunk, "plugins/p/main.lua");
assert_eq!(fresh.text, "return {}");
assert_eq!(fresh.directory, plugin);
assert_eq!(fresh.entry_file, plugin.join("main.lua"));
}
#[test]
fn a_manifest_that_names_another_plugin_is_refused() {
let (_dir, plugin, plugins) = workspace();
let manifest = plugin.join("extension.toml");
let text = fs::read_to_string(&manifest).unwrap();
fs::write(&manifest, text.replace("\"p\"", "\"q\"")).unwrap();
let lua = Lua::new();
let said = refused(plugins.find(&[], &name(&lua, "p")));
assert!(said.contains("plugins/p/extension.toml: "), "{said}");
}
}