Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global version_comparator #1919

Merged
merged 13 commits into from
Dec 10, 2024
30 changes: 27 additions & 3 deletions plugins/updater/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,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 @@ -71,7 +72,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 @@ -88,6 +93,10 @@ impl<R: Runtime, T: Manager<R>> UpdaterExt<R> for T {
builder = builder.current_exe_args(args);
}

if let Some(version_comparator) = version_comparator {
builder = builder.global_version_comparator(version_comparator.clone());
n1ght-hunter marked this conversation as resolved.
Show resolved Hide resolved
}

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

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

impl Builder {
Expand Down Expand Up @@ -165,9 +176,18 @@ impl Builder {
self
}

pub fn version_comparator<F: Fn(Version, RemoteRelease) -> bool + Send + Sync + 'static>(
n1ght-hunter marked this conversation as resolved.
Show resolved Hide resolved
mut self,
f: F,
) -> Self {
self.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.version_comparator;
let installer_args = self.installer_args;
PluginBuilder::<R, Config>::new("updater")
.setup(move |app, api| {
Expand All @@ -178,7 +198,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
20 changes: 19 additions & 1 deletion plugins/updater/src/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,14 @@ impl RemoteRelease {
}

pub type OnBeforeExit = Arc<dyn Fn() + Send + Sync + 'static>;
pub(crate) type GlobalVersionComparator = Arc<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>;
n1ght-hunter marked this conversation as resolved.
Show resolved Hide resolved

pub struct UpdaterBuilder {
app_name: String,
current_version: Version,
config: Config,
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
global_version_comparator: Option<GlobalVersionComparator>,
n1ght-hunter marked this conversation as resolved.
Show resolved Hide resolved
executable_path: Option<PathBuf>,
target: Option<String>,
endpoints: Option<Vec<Url>>,
Expand All @@ -125,6 +127,7 @@ impl UpdaterBuilder {
current_version,
config,
version_comparator: None,
global_version_comparator: None,
executable_path: None,
target: None,
endpoints: None,
Expand Down Expand Up @@ -221,6 +224,11 @@ impl UpdaterBuilder {
self
}

pub(crate) fn global_version_comparator(mut self, f: GlobalVersionComparator) -> Self {
self.global_version_comparator.replace(f);
self
}
n1ght-hunter marked this conversation as resolved.
Show resolved Hide resolved

pub fn build(self) -> Result<Updater> {
let endpoints = self
.endpoints
Expand Down Expand Up @@ -252,6 +260,7 @@ impl UpdaterBuilder {
app_name: self.app_name,
current_version: self.current_version,
version_comparator: self.version_comparator,
global_version_comparator: self.global_version_comparator,
timeout: self.timeout,
proxy: self.proxy,
endpoints,
Expand Down Expand Up @@ -284,6 +293,7 @@ pub struct Updater {
app_name: String,
current_version: Version,
version_comparator: Option<Box<dyn Fn(Version, RemoteRelease) -> bool + Send + Sync>>,
global_version_comparator: Option<GlobalVersionComparator>,
timeout: Option<Duration>,
proxy: Option<Url>,
endpoints: Vec<Url>,
Expand Down Expand Up @@ -394,7 +404,15 @@ impl Updater {

let should_update = match self.version_comparator.as_ref() {
Some(comparator) => comparator(self.current_version.clone(), release.clone()),
None => release.version > self.current_version,
None => {
match self.global_version_comparator.as_ref() {
Some(comparator) => comparator(self.current_version.clone(), release.clone()),
None => {
// default comparator
release.version > self.current_version
}
}
}
};

let update = if should_update {
Expand Down
Loading