feat(updater): refactor and improvements (#431)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr Bashir
2023-08-07 16:01:21 -03:00
committed by GitHub
co-authored by Lucas Nogueira Lucas Nogueira
parent 84133b57b8
commit 4ab90f048e
18 changed files with 1231 additions and 2728 deletions
+67 -22
View File
@@ -14,38 +14,26 @@
)]
use tauri::{
async_runtime::Mutex,
plugin::{Builder as PluginBuilder, TauriPlugin},
Manager, Runtime,
};
use tokio::sync::Mutex;
mod commands;
mod config;
mod error;
mod updater;
pub use config::Config;
pub use error::Error;
pub use error::{Error, Result};
pub use updater::*;
pub type Result<T> = std::result::Result<T, Error>;
struct UpdaterState {
target: Option<String>,
config: Config,
}
struct PendingUpdate<R: Runtime>(Mutex<Option<UpdateResponse<R>>>);
#[derive(Default)]
pub struct Builder {
target: Option<String>,
installer_args: Option<Vec<String>>,
}
struct PendingUpdate(Mutex<Option<Update>>);
/// Extension trait to use the updater on [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`].
pub trait UpdaterExt<R: Runtime> {
/// Gets the updater builder to manually check if an update is available.
/// Gets the updater builder to build and updater
/// that can manually check if an update is available.
///
/// # Examples
///
@@ -55,18 +43,75 @@ pub trait UpdaterExt<R: Runtime> {
/// .setup(|app| {
/// let handle = app.handle();
/// tauri::async_runtime::spawn(async move {
/// let response = handle.updater().check().await;
/// let response = handle.updater_builder().build().unwrap().check().await;
/// });
/// Ok(())
/// });
/// ```
fn updater(&self) -> updater::UpdateBuilder<R>;
fn updater_builder(&self) -> UpdaterBuilder;
/// Gets the updater to manually check if an update is available.
///
/// # Examples
///
/// ```no_run
/// use tauri_plugin_updater::UpdaterExt;
/// tauri::Builder::default()
/// .setup(|app| {
/// let handle = app.handle();
/// tauri::async_runtime::spawn(async move {
/// let response = handle.updater().unwrap().check().await;
/// });
/// Ok(())
/// });
/// ```
fn updater(&self) -> Result<Updater>;
}
impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
fn updater(&self) -> updater::UpdateBuilder<R> {
updater::builder(self.app_handle())
fn updater_builder(&self) -> UpdaterBuilder {
let app = self.app_handle();
let version = app.package_info().version.clone();
let updater_config = app.config().tauri.bundle.updater.clone();
let UpdaterState { config, target } = self.state::<UpdaterState>().inner();
let mut builder = UpdaterBuilder::new(version, config.clone(), updater_config);
if let Some(target) = target {
builder = builder.target(target);
}
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
{
let env = app.env();
if let Some(appimage) = env.appimage {
builder = builder.executable_path(appimage);
}
}
builder
}
fn updater(&self) -> Result<Updater> {
self.updater_builder().build()
}
}
struct UpdaterState {
target: Option<String>,
config: Config,
}
#[derive(Default)]
pub struct Builder {
target: Option<String>,
installer_args: Option<Vec<String>>,
}
impl Builder {
@@ -100,7 +145,7 @@ impl Builder {
config.installer_args = installer_args;
}
app.manage(UpdaterState { target, config });
app.manage(PendingUpdate::<R>(Default::default()));
app.manage(PendingUpdate(Default::default()));
Ok(())
})
.invoke_handler(tauri::generate_handler![