mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-11 22:40:26 +01:00
Restructure add plugin
This commit is contained in:
@@ -1282,7 +1282,7 @@ async fn cmd_install_plugin<R: Runtime>(
|
||||
)?;
|
||||
|
||||
plugin_manager
|
||||
.add_plugin_by_dir(&PluginContext::new(&window), &plugin.directory, plugin.enabled)
|
||||
.add_plugin(&PluginContext::new(&window), &plugin.directory, plugin.enabled)
|
||||
.await?;
|
||||
|
||||
Ok(plugin)
|
||||
|
||||
@@ -55,9 +55,7 @@ pub async fn download_and_install<R: Runtime>(
|
||||
zip_extract::extract(Cursor::new(&bytes), &plugin_dir, true)?;
|
||||
info!("Extracted plugin {} to {}", plugin_version.id, plugin_dir_str);
|
||||
|
||||
plugin_manager.add_plugin_by_dir(&PluginContext::new(&window), &plugin_dir_str, true).await?;
|
||||
|
||||
window.db().upsert_plugin(
|
||||
let plugin = window.db().upsert_plugin(
|
||||
&Plugin {
|
||||
id: plugin_version.id.clone(),
|
||||
checked_at: Some(Utc::now().naive_utc()),
|
||||
@@ -69,6 +67,8 @@ pub async fn download_and_install<R: Runtime>(
|
||||
&UpdateSource::Background,
|
||||
)?;
|
||||
|
||||
plugin_manager.add_plugin(&PluginContext::new(&window), &plugin).await?;
|
||||
|
||||
info!("Installed plugin {} to {}", plugin_version.id, plugin_dir_str);
|
||||
|
||||
Ok(plugin_version)
|
||||
|
||||
@@ -227,32 +227,27 @@ impl PluginManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn add_plugin_by_dir(
|
||||
&self,
|
||||
plugin_context: &PluginContext,
|
||||
dir: &str,
|
||||
enabled: bool,
|
||||
) -> Result<()> {
|
||||
info!("Adding plugin by dir {dir}");
|
||||
pub async fn add_plugin(&self, plugin_context: &PluginContext, plugin: &Plugin) -> Result<()> {
|
||||
info!("Adding plugin by dir {}", plugin.directory);
|
||||
|
||||
let maybe_tx = self.ws_service.app_to_plugin_events_tx.lock().await;
|
||||
let tx = match &*maybe_tx {
|
||||
None => return Err(ClientNotInitializedErr),
|
||||
Some(tx) => tx,
|
||||
};
|
||||
let plugin_handle = PluginHandle::new(dir, enabled, tx.clone())?;
|
||||
let dir_path = Path::new(dir);
|
||||
let plugin_handle = PluginHandle::new(&plugin.directory, plugin.enabled, tx.clone())?;
|
||||
let dir_path = Path::new(&plugin.directory);
|
||||
let is_vendored = dir_path.starts_with(self.vendored_plugin_dir.as_path());
|
||||
let is_installed = dir_path.starts_with(self.installed_plugin_dir.as_path());
|
||||
|
||||
// Boot the plugin if it's enabled
|
||||
if enabled {
|
||||
if plugin.enabled {
|
||||
let event = self
|
||||
.send_to_plugin_and_wait(
|
||||
plugin_context,
|
||||
&plugin_handle,
|
||||
&InternalEventPayload::BootRequest(BootRequest {
|
||||
dir: dir.to_string(),
|
||||
dir: plugin.directory.clone(),
|
||||
watch: !is_vendored && !is_installed,
|
||||
}),
|
||||
)
|
||||
@@ -264,7 +259,7 @@ impl PluginManager {
|
||||
}
|
||||
|
||||
let mut plugin_handles = self.plugin_handles.lock().await;
|
||||
plugin_handles.retain(|p| p.dir != dir);
|
||||
plugin_handles.retain(|p| p.dir != plugin.directory);
|
||||
plugin_handles.push(plugin_handle.clone());
|
||||
|
||||
Ok(())
|
||||
@@ -285,9 +280,7 @@ impl PluginManager {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if let Err(e) =
|
||||
self.add_plugin_by_dir(plugin_context, &plugin.directory, plugin.enabled).await
|
||||
{
|
||||
if let Err(e) = self.add_plugin(plugin_context, &plugin).await {
|
||||
warn!("Failed to add plugin {} {e:?}", plugin.directory);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user