Skip to content

Commit 829b632

Browse files
authored
feat(updater): add Builder::default_version_comparator (#1919)
1 parent 501eae1 commit 829b632

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'updater': 'minor'
3+
'updater-js': 'minor'
4+
---
5+
6+
Add `tauri_plugin_updater::Builder::default_version_comparator` method to set the default version comparator for the updater.

plugins/updater/src/lib.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
1212
)]
1313

14-
use std::ffi::OsString;
14+
use std::{ffi::OsString, sync::Arc};
1515

16+
use semver::Version;
1617
use tauri::{
1718
plugin::{Builder as PluginBuilder, TauriPlugin},
1819
Manager, Runtime,
@@ -69,7 +70,11 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
6970
fn updater_builder(&self) -> UpdaterBuilder {
7071
let app = self.app_handle();
7172
let package_info = app.package_info();
72-
let UpdaterState { config, target } = self.state::<UpdaterState>().inner();
73+
let UpdaterState {
74+
config,
75+
target,
76+
version_comparator,
77+
} = self.state::<UpdaterState>().inner();
7378

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

94+
builder.version_comparator = version_comparator.clone();
95+
8996
#[cfg(any(
9097
target_os = "linux",
9198
target_os = "dragonfly",
@@ -116,13 +123,15 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
116123
struct UpdaterState {
117124
target: Option<String>,
118125
config: Config,
126+
version_comparator: Option<VersionComparator>,
119127
}
120128

121129
#[derive(Default)]
122130
pub struct Builder {
123131
target: Option<String>,
124132
pubkey: Option<String>,
125133
installer_args: Vec<OsString>,
134+
default_version_comparator: Option<VersionComparator>,
126135
}
127136

128137
impl Builder {
@@ -163,9 +172,20 @@ impl Builder {
163172
self
164173
}
165174

175+
pub fn default_version_comparator<
176+
F: Fn(Version, RemoteRelease) -> bool + Send + Sync + 'static,
177+
>(
178+
mut self,
179+
f: F,
180+
) -> Self {
181+
self.default_version_comparator.replace(Arc::new(f));
182+
self
183+
}
184+
166185
pub fn build<R: Runtime>(self) -> TauriPlugin<R, Config> {
167186
let pubkey = self.pubkey;
168187
let target = self.target;
188+
let version_comparator = self.default_version_comparator;
169189
let installer_args = self.installer_args;
170190
PluginBuilder::<R, Config>::new("updater")
171191
.setup(move |app, api| {
@@ -176,7 +196,11 @@ impl Builder {
176196
if let Some(windows) = &mut config.windows {
177197
windows.installer_args.extend_from_slice(&installer_args);
178198
}
179-
app.manage(UpdaterState { target, config });
199+
app.manage(UpdaterState {
200+
target,
201+
config,
202+
version_comparator,
203+
});
180204
Ok(())
181205
})
182206
.invoke_handler(tauri::generate_handler![

plugins/updater/src/updater.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ impl RemoteRelease {
9393
}
9494

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

9798
pub struct UpdaterBuilder {
9899
app_name: String,
99100
current_version: Version,
100101
config: Config,
101-
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
102+
pub(crate) version_comparator: Option<VersionComparator>,
102103
executable_path: Option<PathBuf>,
103104
target: Option<String>,
104105
endpoints: Option<Vec<Url>>,
@@ -139,7 +140,7 @@ impl UpdaterBuilder {
139140
mut self,
140141
f: F,
141142
) -> Self {
142-
self.version_comparator = Some(Box::new(f));
143+
self.version_comparator = Some(Arc::new(f));
143144
self
144145
}
145146

@@ -283,7 +284,7 @@ pub struct Updater {
283284
config: Config,
284285
app_name: String,
285286
current_version: Version,
286-
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
287+
version_comparator: Option<VersionComparator>,
287288
timeout: Option<Duration>,
288289
proxy: Option<Url>,
289290
endpoints: Vec<Url>,

0 commit comments

Comments
 (0)