mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-19 23:31:21 +02:00
Unify plugin bootstrap and prep vendored assets in CLI release
This commit is contained in:
66
crates/yaak-plugins/src/bootstrap.rs
Normal file
66
crates/yaak-plugins/src/bootstrap.rs
Normal file
@@ -0,0 +1,66 @@
|
||||
use crate::error::{Error, Result};
|
||||
use crate::events::PluginContext;
|
||||
use crate::manager::PluginManager;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use yaak_models::models::Plugin;
|
||||
use yaak_models::query_manager::QueryManager;
|
||||
use yaak_models::util::UpdateSource;
|
||||
|
||||
/// Create a plugin manager and initialize all registered plugins.
|
||||
///
|
||||
/// This performs:
|
||||
/// 1. Plugin runtime startup (`PluginManager::new`)
|
||||
/// 2. Bundled plugin registration in DB (if missing)
|
||||
/// 3. Plugin initialization from DB
|
||||
pub async fn create_and_initialize_manager(
|
||||
vendored_plugin_dir: PathBuf,
|
||||
installed_plugin_dir: PathBuf,
|
||||
node_bin_path: PathBuf,
|
||||
plugin_runtime_main: PathBuf,
|
||||
query_manager: &QueryManager,
|
||||
plugin_context: &PluginContext,
|
||||
dev_mode: bool,
|
||||
) -> Result<Arc<PluginManager>> {
|
||||
let plugin_manager = Arc::new(
|
||||
PluginManager::new(
|
||||
vendored_plugin_dir,
|
||||
installed_plugin_dir,
|
||||
node_bin_path,
|
||||
plugin_runtime_main,
|
||||
dev_mode,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
|
||||
let bundled_dirs = plugin_manager.list_bundled_plugin_dirs().await?;
|
||||
let db = query_manager.connect();
|
||||
for dir in bundled_dirs {
|
||||
if db.get_plugin_by_directory(&dir).is_none() {
|
||||
db.upsert_plugin(
|
||||
&Plugin {
|
||||
directory: dir,
|
||||
enabled: true,
|
||||
url: None,
|
||||
..Default::default()
|
||||
},
|
||||
&UpdateSource::Background,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
let plugins = db.list_plugins()?;
|
||||
drop(db);
|
||||
|
||||
let init_errors = plugin_manager.initialize_all_plugins(plugins, plugin_context).await;
|
||||
if !init_errors.is_empty() {
|
||||
let joined = init_errors
|
||||
.into_iter()
|
||||
.map(|(dir, err)| format!("{dir}: {err}"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ");
|
||||
return Err(Error::PluginErr(format!("Failed to initialize plugin(s): {joined}")));
|
||||
}
|
||||
|
||||
Ok(plugin_manager)
|
||||
}
|
||||
@@ -7,6 +7,7 @@
|
||||
//! by yaak-app's plugins_ext module.
|
||||
|
||||
pub mod api;
|
||||
pub mod bootstrap;
|
||||
mod checksum;
|
||||
pub mod error;
|
||||
pub mod events;
|
||||
|
||||
@@ -33,9 +33,8 @@ use tokio::net::TcpListener;
|
||||
use tokio::sync::mpsc::error::TrySendError;
|
||||
use tokio::sync::{Mutex, mpsc, oneshot};
|
||||
use tokio::time::{Instant, timeout};
|
||||
use yaak_models::db_context::DbContext;
|
||||
use yaak_models::models::Plugin;
|
||||
use yaak_models::util::{UpdateSource, generate_id};
|
||||
use yaak_models::util::generate_id;
|
||||
use yaak_templates::error::Error::RenderError;
|
||||
use yaak_templates::error::Result as TemplateResult;
|
||||
|
||||
@@ -181,33 +180,6 @@ impl PluginManager {
|
||||
read_plugins_dir(&plugins_dir).await
|
||||
}
|
||||
|
||||
/// Ensure all bundled plugin directories are present in the plugins table.
|
||||
/// Returns a list of newly registered plugin directories.
|
||||
pub async fn ensure_bundled_plugins_registered(
|
||||
&self,
|
||||
db: &DbContext<'_>,
|
||||
) -> Result<Vec<String>> {
|
||||
let bundled_dirs = self.list_bundled_plugin_dirs().await?;
|
||||
let mut registered = Vec::new();
|
||||
|
||||
for dir in bundled_dirs {
|
||||
if db.get_plugin_by_directory(&dir).is_none() {
|
||||
db.upsert_plugin(
|
||||
&Plugin {
|
||||
directory: dir.clone(),
|
||||
enabled: true,
|
||||
url: None,
|
||||
..Default::default()
|
||||
},
|
||||
&UpdateSource::Background,
|
||||
)?;
|
||||
registered.push(dir);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(registered)
|
||||
}
|
||||
|
||||
pub async fn uninstall(&self, plugin_context: &PluginContext, dir: &str) -> Result<()> {
|
||||
let plugin = self.get_plugin_by_dir(dir).await.ok_or(PluginNotFoundErr(dir.to_string()))?;
|
||||
self.remove_plugin(plugin_context, &plugin).await
|
||||
|
||||
Reference in New Issue
Block a user