diff --git a/.changes/add-global-updater-version-comparator.md b/.changes/add-global-updater-version-comparator.md new file mode 100644 index 0000000000..a612f55dac --- /dev/null +++ b/.changes/add-global-updater-version-comparator.md @@ -0,0 +1,6 @@ +--- +'updater': 'minor' +'updater-js': 'minor' +--- + +Add `tauri_plugin_updater::Builder::default_version_comparator` method to set the default version comparator for the updater. \ No newline at end of file diff --git a/plugins/updater/src/lib.rs b/plugins/updater/src/lib.rs index 4dbd26e19f..f5045bebe4 100644 --- a/plugins/updater/src/lib.rs +++ b/plugins/updater/src/lib.rs @@ -11,8 +11,9 @@ html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png" )] -use std::ffi::OsString; +use std::{ffi::OsString, sync::Arc}; +use semver::Version; use tauri::{ plugin::{Builder as PluginBuilder, TauriPlugin}, Manager, Runtime, @@ -69,7 +70,11 @@ impl> UpdaterExt for T { fn updater_builder(&self) -> UpdaterBuilder { let app = self.app_handle(); let package_info = app.package_info(); - let UpdaterState { config, target } = self.state::().inner(); + let UpdaterState { + config, + target, + version_comparator, + } = self.state::().inner(); let mut builder = UpdaterBuilder::new( package_info.name.clone(), @@ -86,6 +91,8 @@ impl> UpdaterExt for T { builder = builder.current_exe_args(args); } + builder.version_comparator = version_comparator.clone(); + #[cfg(any( target_os = "linux", target_os = "dragonfly", @@ -116,6 +123,7 @@ impl> UpdaterExt for T { struct UpdaterState { target: Option, config: Config, + version_comparator: Option, } #[derive(Default)] @@ -123,6 +131,7 @@ pub struct Builder { target: Option, pubkey: Option, installer_args: Vec, + default_version_comparator: Option, } impl Builder { @@ -163,9 +172,20 @@ impl Builder { self } + pub fn default_version_comparator< + F: Fn(Version, RemoteRelease) -> bool + Send + Sync + 'static, + >( + mut self, + f: F, + ) -> Self { + self.default_version_comparator.replace(Arc::new(f)); + self + } + pub fn build(self) -> TauriPlugin { let pubkey = self.pubkey; let target = self.target; + let version_comparator = self.default_version_comparator; let installer_args = self.installer_args; PluginBuilder::::new("updater") .setup(move |app, api| { @@ -176,7 +196,11 @@ impl Builder { if let Some(windows) = &mut config.windows { windows.installer_args.extend_from_slice(&installer_args); } - app.manage(UpdaterState { target, config }); + app.manage(UpdaterState { + target, + config, + version_comparator, + }); Ok(()) }) .invoke_handler(tauri::generate_handler![ diff --git a/plugins/updater/src/updater.rs b/plugins/updater/src/updater.rs index d66af3dc02..bb03e7ec52 100644 --- a/plugins/updater/src/updater.rs +++ b/plugins/updater/src/updater.rs @@ -93,12 +93,13 @@ impl RemoteRelease { } pub type OnBeforeExit = Arc; +pub type VersionComparator = Arc bool + Send + Sync>; pub struct UpdaterBuilder { app_name: String, current_version: Version, config: Config, - version_comparator: Option bool + Send + Sync>>, + pub(crate) version_comparator: Option, executable_path: Option, target: Option, endpoints: Option>, @@ -139,7 +140,7 @@ impl UpdaterBuilder { mut self, f: F, ) -> Self { - self.version_comparator = Some(Box::new(f)); + self.version_comparator = Some(Arc::new(f)); self } @@ -283,7 +284,7 @@ pub struct Updater { config: Config, app_name: String, current_version: Version, - version_comparator: Option bool + Send + Sync>>, + version_comparator: Option, timeout: Option, proxy: Option, endpoints: Vec,