Install plugins from Yaak plugin registry (#230)

This commit is contained in:
Gregory Schier
2025-06-23 08:55:38 -07:00
committed by GitHub
parent b5620fcdf3
commit cb7c44cc65
27 changed files with 421 additions and 218 deletions

View File

@@ -1,23 +1,25 @@
use crate::api::download_plugin_archive;
use crate::api::{PluginVersion, download_plugin_archive, get_plugin};
use crate::checksum::compute_checksum;
use crate::commands::PluginVersion;
use crate::error::Error::PluginErr;
use crate::error::Result;
use crate::events::PluginWindowContext;
use crate::manager::PluginManager;
use chrono::Utc;
use log::info;
use std::fs::create_dir_all;
use std::fs::{create_dir_all, remove_dir_all};
use std::io::Cursor;
use tauri::{Manager, Runtime, WebviewWindow};
use yaak_models::models::Plugin;
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::{UpdateSource, generate_id};
use yaak_models::util::UpdateSource;
pub async fn download_and_install<R: Runtime>(
window: &WebviewWindow<R>,
plugin_version: &PluginVersion,
) -> Result<String> {
name: &str,
version: Option<String>,
) -> Result<PluginVersion> {
let plugin_manager = window.state::<PluginManager>();
let plugin_version = get_plugin(window.app_handle(), name, version).await?;
let resp = download_plugin_archive(window.app_handle(), &plugin_version).await?;
let bytes = resp.bytes().await?;
@@ -32,17 +34,19 @@ pub async fn download_and_install<R: Runtime>(
info!("Checksum matched {}", checksum);
let plugin_dir = window.path().app_data_dir()?.join("plugins").join(generate_id());
let plugin_dir = plugin_manager.installed_plugin_dir.join(name);
let plugin_dir_str = plugin_dir.to_str().unwrap().to_string();
// Re-create the plugin directory
let _ = remove_dir_all(&plugin_dir);
create_dir_all(&plugin_dir)?;
zip_extract::extract(Cursor::new(&bytes), &plugin_dir, true)?;
info!("Extracted plugin {} to {}", plugin_version.id, plugin_dir_str);
let plugin_manager = window.state::<PluginManager>();
plugin_manager.add_plugin_by_dir(&PluginWindowContext::new(&window), &plugin_dir_str).await?;
let p = window.db().upsert_plugin(
window.db().upsert_plugin(
&Plugin {
id: plugin_version.id.clone(),
checked_at: Some(Utc::now().naive_utc()),
@@ -56,5 +60,5 @@ pub async fn download_and_install<R: Runtime>(
info!("Installed plugin {} to {}", plugin_version.id, plugin_dir_str);
Ok(p.id)
Ok(plugin_version)
}