use crate::embedded_plugins::EmbeddedPlugin;
use crate::error::Result;
use mlua::Lua;
use std::collections::BTreeSet;
use std::path::Path;
pub struct HookFunc {
_name: &'static str,
pub filename: &'static str,
}
#[rustfmt::skip]
pub const HOOK_FUNCS: [HookFunc; 12] = [
HookFunc { _name: "Available", filename: "available" },
HookFunc { _name: "PreInstall", filename: "pre_install" },
HookFunc { _name: "EnvKeys", filename: "env_keys" },
HookFunc { _name: "PostInstall", filename: "post_install" },
HookFunc { _name: "PreUse", filename: "pre_use" },
HookFunc { _name: "ParseLegacyFile", filename: "parse_legacy_file" },
HookFunc { _name: "PreUninstall", filename: "pre_uninstall" },
HookFunc { _name: "BackendListVersions", filename: "backend_list_versions" },
HookFunc { _name: "BackendInstall", filename: "backend_install" },
HookFunc { _name: "BackendExecEnv", filename: "backend_exec_env" },
HookFunc { _name: "MiseEnv", filename: "mise_env" },
HookFunc { _name: "MisePath", filename: "mise_path" },
];
pub fn mod_hooks(lua: &Lua, root: &Path) -> Result<BTreeSet<&'static str>> {
let mut hooks = BTreeSet::new();
for hook in &HOOK_FUNCS {
let hook_path = root.join("hooks").join(format!("{}.lua", hook.filename));
if hook_path.exists() {
lua.load(hook_path).exec()?;
hooks.insert(hook.filename);
}
}
Ok(hooks)
}
pub fn hooks_embedded(lua: &Lua, embedded: &EmbeddedPlugin) -> Result<BTreeSet<&'static str>> {
let mut hooks = BTreeSet::new();
let package: mlua::Table = lua.globals().get("package")?;
let loaded: mlua::Table = package.get("loaded")?;
for (hook_name, hook_code) in embedded.hooks {
lua.load(*hook_code).exec()?;
let module_name = format!("hooks/{}", hook_name);
loaded.set(module_name, true)?;
for hook in &HOOK_FUNCS {
if hook.filename == *hook_name {
hooks.insert(hook.filename);
break;
}
}
}
Ok(hooks)
}