Skip to content

Commit

Permalink
feat(updater): add Builder::default_version_comparator (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ght-hunter authored Dec 10, 2024
1 parent 501eae1 commit 829b632
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/add-global-updater-version-comparator.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 27 additions & 3 deletions plugins/updater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -69,7 +70,11 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
fn updater_builder(&self) -> UpdaterBuilder {
let app = self.app_handle();
let package_info = app.package_info();
let UpdaterState { config, target } = self.state::<UpdaterState>().inner();
let UpdaterState {
config,
target,
version_comparator,
} = self.state::<UpdaterState>().inner();

let mut builder = UpdaterBuilder::new(
package_info.name.clone(),
Expand All @@ -86,6 +91,8 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
builder = builder.current_exe_args(args);
}

builder.version_comparator = version_comparator.clone();

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
Expand Down Expand Up @@ -116,13 +123,15 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
struct UpdaterState {
target: Option<String>,
config: Config,
version_comparator: Option<VersionComparator>,
}

#[derive(Default)]
pub struct Builder {
target: Option<String>,
pubkey: Option<String>,
installer_args: Vec<OsString>,
default_version_comparator: Option<VersionComparator>,
}

impl Builder {
Expand Down Expand Up @@ -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<R: Runtime>(self) -> TauriPlugin<R, Config> {
let pubkey = self.pubkey;
let target = self.target;
let version_comparator = self.default_version_comparator;
let installer_args = self.installer_args;
PluginBuilder::<R, Config>::new("updater")
.setup(move |app, api| {
Expand All @@ -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![
Expand Down
7 changes: 4 additions & 3 deletions plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ impl RemoteRelease {
}

pub type OnBeforeExit = Arc<dyn Fn() + Send + Sync + 'static>;
pub type VersionComparator = Arc<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>;

pub struct UpdaterBuilder {
app_name: String,
current_version: Version,
config: Config,
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
pub(crate) version_comparator: Option<VersionComparator>,
executable_path: Option<PathBuf>,
target: Option<String>,
endpoints: Option<Vec<Url>>,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -283,7 +284,7 @@ pub struct Updater {
config: Config,
app_name: String,
current_version: Version,
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
version_comparator: Option<VersionComparator>,
timeout: Option<Duration>,
proxy: Option<Url>,
endpoints: Vec<Url>,
Expand Down

0 comments on commit 829b632

Please sign in to comment.