Skip to content

Commit 5e9aebd

Browse files
committed
Embed CLI plugin assets and share bundled plugin registration
1 parent a1e84c7 commit 5e9aebd

File tree

5 files changed

+70
-25
lines changed

5 files changed

+70
-25
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates-cli/yaak-cli/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dirs = "6"
1616
env_logger = "0.11"
1717
futures = "0.3"
1818
hex = { workspace = true }
19+
include_dir = "0.7"
1920
keyring = { workspace = true, features = ["apple-native", "windows-native", "sync-secret-service"] }
2021
log = { workspace = true }
2122
rand = "0.8"

crates-cli/yaak-cli/src/context.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use crate::plugin_events::CliPluginEventBridge;
2+
use include_dir::{Dir, include_dir};
3+
use std::fs;
24
use std::path::{Path, PathBuf};
35
use std::sync::Arc;
46
use tokio::sync::Mutex;
@@ -9,6 +11,13 @@ use yaak_models::query_manager::QueryManager;
911
use yaak_plugins::events::PluginContext;
1012
use yaak_plugins::manager::PluginManager;
1113

14+
const EMBEDDED_PLUGIN_RUNTIME: &str = include_str!(concat!(
15+
env!("CARGO_MANIFEST_DIR"),
16+
"/../../crates-tauri/yaak-app/vendored/plugin-runtime/index.cjs"
17+
));
18+
static EMBEDDED_VENDORED_PLUGINS: Dir<'_> =
19+
include_dir!("$CARGO_MANIFEST_DIR/../../crates-tauri/yaak-app/vendored/plugins");
20+
1221
pub struct CliContext {
1322
data_dir: PathBuf,
1423
query_manager: QueryManager,
@@ -33,10 +42,13 @@ impl CliContext {
3342
let installed_plugin_dir = data_dir.join("installed-plugins");
3443
let node_bin_path = PathBuf::from("node");
3544

45+
prepare_embedded_vendored_plugins(&vendored_plugin_dir)
46+
.expect("Failed to prepare bundled plugins");
47+
3648
let plugin_runtime_main =
3749
std::env::var("YAAK_PLUGIN_RUNTIME").map(PathBuf::from).unwrap_or_else(|_| {
38-
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
39-
.join("../../crates-tauri/yaak-app/vendored/plugin-runtime/index.cjs")
50+
prepare_embedded_plugin_runtime(&data_dir)
51+
.expect("Failed to prepare embedded plugin runtime")
4052
});
4153

4254
let plugin_manager = Arc::new(
@@ -50,6 +62,13 @@ impl CliContext {
5062
.await,
5163
);
5264

65+
{
66+
let db = query_manager.connect();
67+
if let Err(err) = plugin_manager.ensure_bundled_plugins_registered(&db).await {
68+
eprintln!("Warning: Failed to register bundled plugins: {err}");
69+
}
70+
}
71+
5372
let plugins = query_manager.connect().list_plugins().unwrap_or_default();
5473
if !plugins.is_empty() {
5574
let errors = plugin_manager
@@ -113,3 +132,17 @@ impl CliContext {
113132
}
114133
}
115134
}
135+
136+
fn prepare_embedded_plugin_runtime(data_dir: &Path) -> std::io::Result<PathBuf> {
137+
let runtime_dir = data_dir.join("vendored").join("plugin-runtime");
138+
fs::create_dir_all(&runtime_dir)?;
139+
let runtime_main = runtime_dir.join("index.cjs");
140+
fs::write(&runtime_main, EMBEDDED_PLUGIN_RUNTIME)?;
141+
Ok(runtime_main)
142+
}
143+
144+
fn prepare_embedded_vendored_plugins(vendored_plugin_dir: &Path) -> std::io::Result<()> {
145+
fs::create_dir_all(vendored_plugin_dir)?;
146+
EMBEDDED_VENDORED_PLUGINS.extract(vendored_plugin_dir)?;
147+
Ok(())
148+
}

crates-tauri/yaak-app/src/plugins_ext.rs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use tokio::sync::Mutex;
2323
use ts_rs::TS;
2424
use yaak_api::yaak_api_client;
2525
use yaak_models::models::Plugin;
26-
use yaak_models::util::UpdateSource;
2726
use yaak_plugins::api::{
2827
PluginNameVersion, PluginSearchResponse, PluginUpdatesResponse, check_plugin_updates,
2928
search_plugins,
@@ -281,28 +280,11 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
281280
)
282281
.await;
283282

284-
// Initialize all plugins after manager is created
285-
let bundled_dirs = manager
286-
.list_bundled_plugin_dirs()
287-
.await
288-
.expect("Failed to list bundled plugins");
289-
290-
// Ensure all bundled plugins make it into the database
291283
let db = app_handle_clone.db();
292-
for dir in &bundled_dirs {
293-
if db.get_plugin_by_directory(dir).is_none() {
294-
db.upsert_plugin(
295-
&Plugin {
296-
directory: dir.clone(),
297-
enabled: true,
298-
url: None,
299-
..Default::default()
300-
},
301-
&UpdateSource::Background,
302-
)
303-
.expect("Failed to upsert bundled plugin");
304-
}
305-
}
284+
manager
285+
.ensure_bundled_plugins_registered(&db)
286+
.await
287+
.expect("Failed to register bundled plugins");
306288

307289
// Get all plugins from database and initialize
308290
let plugins = db.list_plugins().expect("Failed to list plugins from database");

crates/yaak-plugins/src/manager.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ use tokio::net::TcpListener;
3333
use tokio::sync::mpsc::error::TrySendError;
3434
use tokio::sync::{Mutex, mpsc, oneshot};
3535
use tokio::time::{Instant, timeout};
36+
use yaak_models::db_context::DbContext;
3637
use yaak_models::models::Plugin;
37-
use yaak_models::util::generate_id;
38+
use yaak_models::util::{UpdateSource, generate_id};
3839
use yaak_templates::error::Error::RenderError;
3940
use yaak_templates::error::Result as TemplateResult;
4041

@@ -180,6 +181,33 @@ impl PluginManager {
180181
read_plugins_dir(&plugins_dir).await
181182
}
182183

184+
/// Ensure all bundled plugin directories are present in the plugins table.
185+
/// Returns a list of newly registered plugin directories.
186+
pub async fn ensure_bundled_plugins_registered(
187+
&self,
188+
db: &DbContext<'_>,
189+
) -> Result<Vec<String>> {
190+
let bundled_dirs = self.list_bundled_plugin_dirs().await?;
191+
let mut registered = Vec::new();
192+
193+
for dir in bundled_dirs {
194+
if db.get_plugin_by_directory(&dir).is_none() {
195+
db.upsert_plugin(
196+
&Plugin {
197+
directory: dir.clone(),
198+
enabled: true,
199+
url: None,
200+
..Default::default()
201+
},
202+
&UpdateSource::Background,
203+
)?;
204+
registered.push(dir);
205+
}
206+
}
207+
208+
Ok(registered)
209+
}
210+
183211
pub async fn uninstall(&self, plugin_context: &PluginContext, dir: &str) -> Result<()> {
184212
let plugin = self.get_plugin_by_dir(dir).await.ok_or(PluginNotFoundErr(dir.to_string()))?;
185213
self.remove_plugin(plugin_context, &plugin).await

0 commit comments

Comments
 (0)