Skip to content

Commit

Permalink
feat: add updater plugin 2.0.0 support (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm authored Oct 2, 2024
1 parent c826d18 commit 5ab6c70
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 58 deletions.
55 changes: 2 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
tauri-build = { version = "2.0.0", features = [] }

[target."cfg(not(any(target_os = \"android\", target_os = \"ios\")))".dependencies]
tauri-plugin-updater = "2.0.0"
tauri-plugin-updater = "2.0.1"

[dependencies]
clap = { version = "4.5.19", features = ["derive"] }
Expand All @@ -20,7 +20,6 @@ serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
tauri = { version = "2.0.0", features = [] }
tauri-plugin-log = "2.0.0"
tauri-plugin-shell = "2.0.0"

[lib]
name = "mdns_browser_lib"
Expand Down
51 changes: 49 additions & 2 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tauri::Emitter;
use tauri::Manager;
use tauri::{State, Window};
use tauri_plugin_log::{Target, TargetKind};
use tauri_plugin_updater::UpdaterExt;

type SharedServiceDaemon = Arc<Mutex<ServiceDaemon>>;

Expand Down Expand Up @@ -189,6 +190,7 @@ fn browse_types(window: Window, state: State<ManagedState>) {
});
}
}

#[tauri::command]
fn stop_browse(service_type: String, state: State<ManagedState>) {
if service_type.is_empty() {
Expand Down Expand Up @@ -289,6 +291,7 @@ fn send_metrics(window: Window, state: State<ManagedState>) {
}
}

#[cfg(desktop)]
#[tauri::command]
fn open(url: String) {
let _ = open::that(url.clone()).map_err(|e| log::error!("Failed to open {}: {}", url, e));
Expand Down Expand Up @@ -382,6 +385,43 @@ mod foreign_crate {
}
}

#[cfg(desktop)]
#[tauri::command]
fn is_desktop() -> bool {
true
}

#[cfg(not(desktop))]
#[tauri::command]
fn is_desktop() -> bool {
false
}

async fn update(app: tauri::AppHandle) -> tauri_plugin_updater::Result<()> {
if let Some(update) = app.updater()?.check().await? {
let mut downloaded = 0;

update
.download_and_install(
|chunk_length, content_length| {
downloaded += chunk_length;
log::info!("downloaded {downloaded} from {content_length:?}");
},
|| {
log::info!("download finished");
},
)
.await?;

log::info!("update installed, restarting");
app.restart();
} else {
log::info!("No updates are available");
}

Ok(())
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
#[cfg(target_os = "linux")]
Expand All @@ -398,11 +438,16 @@ pub fn run() {
.level(args.log_level)
.build(),
)
.plugin(tauri_plugin_updater::Builder::new().build())
.setup(|app| {
#[cfg(desktop)]
{
app.handle()
.plugin(tauri_plugin_updater::Builder::new().build())?;
let handle = app.handle().clone();
tauri::async_runtime::spawn(async move {
let _ = update(handle)
.await
.map_err(|err| log::error!("Failed to check for updates: {}", err));
});
let splashscreen_window = app.get_webview_window("splashscreen").unwrap();
let main_window = app.get_webview_window("main").unwrap();
tauri::async_runtime::spawn(async move {
Expand All @@ -420,6 +465,8 @@ pub fn run() {
.invoke_handler(tauri::generate_handler![
browse,
browse_types,
is_desktop,
#[cfg(desktop)]
open,
send_metrics,
stop_browse,
Expand Down
1 change: 0 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"installMode": "passive"
},
"endpoints": [
"https://mdns-browser-updates.knulp.duckdns.org/updates.json",
"https://mdns-browser-updates.knulp.home64.de/updates.json",
"https://mdns-browser-updates.knulp.v6.rocks/updates.json"
],
Expand Down

0 comments on commit 5ab6c70

Please sign in to comment.