diff --git a/crates/core/src/proto.rs b/crates/core/src/proto.rs index c70ce3233..a36c16432 100644 --- a/crates/core/src/proto.rs +++ b/crates/core/src/proto.rs @@ -1,6 +1,8 @@ use crate::helpers::{get_home_dir, get_proto_home}; +use once_cell::sync::OnceCell; use std::env; use std::path::{Path, PathBuf}; +use warpgate::PluginLoader; #[derive(Clone, Debug)] pub struct ProtoEnvironment { @@ -11,6 +13,8 @@ pub struct ProtoEnvironment { pub tools_dir: PathBuf, pub home: PathBuf, // ~ pub root: PathBuf, // ~/.proto + + loader: OnceCell, } impl ProtoEnvironment { @@ -36,6 +40,15 @@ impl ProtoEnvironment { tools_dir: root.join("tools"), home: get_home_dir()?, root: root.to_owned(), + loader: OnceCell::new(), + }) + } + + pub fn create_plugin_loader(&self) -> &PluginLoader { + self.loader.get_or_init(|| { + let mut loader = PluginLoader::new(&self.plugins_dir, &self.temp_dir); + loader.set_seed(env!("CARGO_PKG_VERSION")); + loader }) } } diff --git a/crates/core/src/tool_loader.rs b/crates/core/src/tool_loader.rs index 73860d7dd..3c2d4546e 100644 --- a/crates/core/src/tool_loader.rs +++ b/crates/core/src/tool_loader.rs @@ -14,7 +14,7 @@ use std::{ path::Path, }; use tracing::debug; -use warpgate::{to_virtual_path, Id, PluginLoader, PluginLocator}; +use warpgate::{to_virtual_path, Id, PluginLocator}; pub fn inject_default_manifest_config( id: &Id, @@ -60,10 +60,8 @@ pub async fn load_tool_from_locator( let proto = proto.as_ref(); let locator = locator.as_ref(); - let mut loader = PluginLoader::new(&proto.plugins_dir, &proto.temp_dir); - loader.set_seed(env!("CARGO_PKG_VERSION")); - - let plugin_path = loader.load_plugin(&id, locator).await?; + let plugin_loader = proto.create_plugin_loader(); + let plugin_path = plugin_loader.load_plugin(&id, locator).await?; // If a TOML plugin, we need to load the WASM plugin for it, // wrap it, and modify the plugin manifest. @@ -76,7 +74,11 @@ pub async fn load_tool_from_locator( let mut manifest = Tool::create_plugin_manifest( proto, - Wasm::file(loader.load_plugin(id, ToolsConfig::schema_plugin()).await?), + Wasm::file( + plugin_loader + .load_plugin(id, ToolsConfig::schema_plugin()) + .await?, + ), )?; manifest diff --git a/crates/warpgate/src/loader.rs b/crates/warpgate/src/loader.rs index 843247da4..5f76ce7a3 100644 --- a/crates/warpgate/src/loader.rs +++ b/crates/warpgate/src/loader.rs @@ -16,6 +16,7 @@ use tracing::trace; /// A system for loading plugins from a locator strategy, /// and caching the `.wasm` file to the host's file system. +#[derive(Clone, Debug)] pub struct PluginLoader { /// Location where downloaded `.wasm` plugins are stored. plugins_dir: PathBuf,