mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-27 11:56:05 +02:00
refactor: use builder style (#50)
* refactor(upload): use plugin builder style * refactor(sql): use builder style * refactor(fs-watch): use builder style * refactor(fs-extra): use builder style * refactor(auth): use builder style * fmt * fmt Co-authored-by: FabianLars <fabianlars@fabianlars.de>
This commit is contained in:
committed by
GitHub
parent
66dc508f34
commit
2d670f70d9
@@ -2,7 +2,7 @@ import { invoke } from "@tauri-apps/api/tauri";
|
||||
|
||||
export class Authenticator {
|
||||
async init(): Promise<void> {
|
||||
return await invoke("plugin:authenticator|init");
|
||||
return await invoke("plugin:authenticator|init_auth");
|
||||
}
|
||||
|
||||
async register(challenge: string, application: string): Promise<string> {
|
||||
|
||||
@@ -6,13 +6,16 @@ mod auth;
|
||||
mod error;
|
||||
mod u2f;
|
||||
|
||||
use tauri::{plugin::Plugin, Invoke, Runtime};
|
||||
use tauri::{
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
Runtime,
|
||||
};
|
||||
|
||||
pub use error::Error;
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[tauri::command]
|
||||
fn init() {
|
||||
fn init_auth() {
|
||||
auth::init_usb();
|
||||
}
|
||||
|
||||
@@ -60,30 +63,14 @@ fn verify_signature(
|
||||
)
|
||||
}
|
||||
|
||||
pub struct TauriAuthenticator<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for TauriAuthenticator<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![
|
||||
init,
|
||||
register,
|
||||
verify_registration,
|
||||
sign,
|
||||
verify_signature
|
||||
]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for TauriAuthenticator<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"authenticator"
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, invoke: Invoke<R>) {
|
||||
(self.invoke_handler)(invoke)
|
||||
}
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
PluginBuilder::new("authenticator")
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
init_auth,
|
||||
register,
|
||||
verify_registration,
|
||||
sign,
|
||||
verify_signature
|
||||
])
|
||||
.build()
|
||||
}
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
use tauri::{command, plugin::Plugin, Invoke, Runtime};
|
||||
use tauri::{
|
||||
command,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
Runtime,
|
||||
};
|
||||
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
@@ -121,25 +125,8 @@ async fn exists(path: PathBuf) -> bool {
|
||||
path.exists()
|
||||
}
|
||||
|
||||
/// Tauri plugin.
|
||||
pub struct FsExtra<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for FsExtra<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![exists, metadata]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for FsExtra<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"fs-extra"
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
PluginBuilder::new("fs-extra")
|
||||
.invoke_handler(tauri::generate_handler![exists, metadata])
|
||||
.build()
|
||||
}
|
||||
|
||||
+13
-28
@@ -3,8 +3,11 @@ use notify::{
|
||||
Watcher as _,
|
||||
};
|
||||
use serde::{ser::Serializer, Deserialize, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window};
|
||||
use tauri::{
|
||||
command,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
Manager, Runtime, State, Window,
|
||||
};
|
||||
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
@@ -160,30 +163,12 @@ async fn unwatch(watchers: State<'_, WatcherCollection>, id: Id) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tauri plugin.
|
||||
pub struct Watcher<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for Watcher<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![watch, unwatch]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for Watcher<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"fs-watch"
|
||||
}
|
||||
|
||||
fn initialize(&mut self, app: &AppHandle<R>, _config: JsonValue) -> tauri::plugin::Result<()> {
|
||||
app.manage(WatcherCollection::default());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
PluginBuilder::new("fs-watch")
|
||||
.invoke_handler(tauri::generate_handler![watch, unwatch])
|
||||
.setup(|app| {
|
||||
app.manage(WatcherCollection::default());
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
+52
-71
@@ -14,8 +14,8 @@ use sqlx::{
|
||||
};
|
||||
use tauri::{
|
||||
command,
|
||||
plugin::{Plugin, Result as PluginResult},
|
||||
AppHandle, Invoke, Manager, RunEvent, Runtime, State,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
AppHandle, Manager, RunEvent, Runtime, State,
|
||||
};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
@@ -92,7 +92,7 @@ struct DbInstances(Mutex<HashMap<String, Pool<Db>>>);
|
||||
struct Migrations(Mutex<HashMap<String, MigrationList>>);
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
struct PluginConfig {
|
||||
pub struct PluginConfig {
|
||||
#[serde(default)]
|
||||
preload: Vec<String>,
|
||||
}
|
||||
@@ -300,88 +300,69 @@ async fn select(
|
||||
Ok(values)
|
||||
}
|
||||
|
||||
/// Tauri SQL plugin.
|
||||
pub struct TauriSql<R: Runtime> {
|
||||
/// Tauri SQL plugin builder.
|
||||
pub struct Builder {
|
||||
migrations: Option<HashMap<String, MigrationList>>,
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for TauriSql<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
migrations: Some(Default::default()),
|
||||
invoke_handler: Box::new(tauri::generate_handler![load, execute, select, close]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> TauriSql<R> {
|
||||
impl Builder {
|
||||
/// Add migrations to a database.
|
||||
#[must_use]
|
||||
pub fn add_migrations(mut self, db_url: &str, migrations: Vec<Migration>) -> Self {
|
||||
self.migrations
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.get_or_insert(Default::default())
|
||||
.insert(db_url.to_string(), MigrationList(migrations));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for TauriSql<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"sql"
|
||||
}
|
||||
pub fn build<R: Runtime>(mut self) -> TauriPlugin<R, Option<PluginConfig>> {
|
||||
PluginBuilder::new("sql")
|
||||
.invoke_handler(tauri::generate_handler![load, execute, select, close])
|
||||
.setup_with_config(|app, config: Option<PluginConfig>| {
|
||||
let config = config.unwrap_or_default();
|
||||
|
||||
fn initialize(&mut self, app: &AppHandle<R>, config: serde_json::Value) -> PluginResult<()> {
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let config: PluginConfig = if config.is_null() {
|
||||
Default::default()
|
||||
} else {
|
||||
serde_json::from_value(config)?
|
||||
};
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
create_dir_all(app_path(app)).expect("problems creating App directory!");
|
||||
|
||||
let instances = DbInstances::default();
|
||||
let mut lock = instances.0.lock().await;
|
||||
for db in config.preload {
|
||||
#[cfg(feature = "sqlite")]
|
||||
let fqdb = path_mapper(app_path(app), &db);
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
let fqdb = db.clone();
|
||||
create_dir_all(app_path(app)).expect("problems creating App directory!");
|
||||
|
||||
if !Db::database_exists(&fqdb).await.unwrap_or(false) {
|
||||
Db::create_database(&fqdb).await?;
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let instances = DbInstances::default();
|
||||
let mut lock = instances.0.lock().await;
|
||||
for db in config.preload {
|
||||
#[cfg(feature = "sqlite")]
|
||||
let fqdb = path_mapper(app_path(app), &db);
|
||||
#[cfg(not(feature = "sqlite"))]
|
||||
let fqdb = db.clone();
|
||||
|
||||
if !Db::database_exists(&fqdb).await.unwrap_or(false) {
|
||||
Db::create_database(&fqdb).await?;
|
||||
}
|
||||
let pool = Pool::connect(&fqdb).await?;
|
||||
|
||||
if let Some(migrations) = self.migrations.as_mut().unwrap().remove(&db) {
|
||||
let migrator = Migrator::new(migrations).await?;
|
||||
migrator.run(&pool).await?;
|
||||
}
|
||||
lock.insert(db, pool);
|
||||
}
|
||||
drop(lock);
|
||||
|
||||
app.manage(instances);
|
||||
app.manage(Migrations(Mutex::new(self.migrations.take().unwrap())));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
})
|
||||
.on_event(|app, event| {
|
||||
if let RunEvent::Exit = event {
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let instances = &*app.state::<DbInstances>();
|
||||
let instances = instances.0.lock().await;
|
||||
for value in instances.values() {
|
||||
value.close().await;
|
||||
}
|
||||
});
|
||||
}
|
||||
let pool = Pool::connect(&fqdb).await?;
|
||||
|
||||
if let Some(migrations) = self.migrations.as_mut().unwrap().remove(&db) {
|
||||
let migrator = Migrator::new(migrations).await?;
|
||||
migrator.run(&pool).await?;
|
||||
}
|
||||
lock.insert(db, pool);
|
||||
}
|
||||
drop(lock);
|
||||
app.manage(instances);
|
||||
app.manage(Migrations(Mutex::new(self.migrations.take().unwrap())));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
|
||||
fn on_event(&mut self, app: &AppHandle<R>, event: &RunEvent) {
|
||||
if let RunEvent::Exit = event {
|
||||
tauri::async_runtime::block_on(async move {
|
||||
let instances = &*app.state::<DbInstances>();
|
||||
let instances = instances.0.lock().await;
|
||||
for value in instances.values() {
|
||||
value.close().await;
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
.build()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,11 @@
|
||||
|
||||
use futures::TryStreamExt;
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
use tauri::{command, plugin::Plugin, Invoke, Runtime, Window};
|
||||
use tauri::{
|
||||
command,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
Runtime, Window,
|
||||
};
|
||||
use tokio::fs::File;
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
|
||||
@@ -82,25 +86,8 @@ fn file_to_body<R: Runtime>(id: u32, window: Window<R>, file: File) -> reqwest::
|
||||
))
|
||||
}
|
||||
|
||||
/// Tauri plugin.
|
||||
pub struct Upload<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for Upload<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![upload]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for Upload<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"upload"
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
PluginBuilder::new("upload")
|
||||
.invoke_handler(tauri::generate_handler![upload])
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user