Skip to content

Commit

Permalink
fix: updater 🐛 (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
kareemmahlees authored Jul 23, 2024
1 parent 33db5b1 commit 3f84331
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/changeset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- name: Create Release Pull Request
uses: changesets/action@v1
with:
title: "chore: version pump 🔖"
commit: "chore: version pump 🔖"
publish: bunx changeset tag
env:
Expand Down
1 change: 1 addition & 0 deletions apps/core/src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"event:allow-unlisten",
"event:allow-emit",
"dialog:allow-open",
"updater:allow-check",
"clipboard-manager:allow-write-text",
{
"identifier":"fs:allow-read-file",
Expand Down
16 changes: 14 additions & 2 deletions apps/core/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
mod cli;
mod commands;
mod state;
#[cfg(not(debug_assertions))]
mod updater;

use commands::{
connection::{
Expand All @@ -15,6 +17,8 @@ use commands::{
row::{create_row, delete_rows, get_fk_relations, get_paginated_rows, update_row},
table::{execute_raw_query, get_columns_props, get_tables},
};
#[cfg(not(debug_assertions))]
use updater::check_for_update;

use specta::{
ts::{BigIntExportBehavior, ExportConfig},
Expand Down Expand Up @@ -111,11 +115,13 @@ fn main() {
.plugin(tauri_plugin_dialog::init())
.manage(Mutex::new(SharedState::default()))
.setup(|app| {
ensure_config_files_exist(app.app_handle())?;
let app_handle = app.app_handle();

ensure_config_files_exist(app_handle)?;
register_events(app);

let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(cli::handle_cli_args(app.app_handle(), args, cmd));
rt.block_on(cli::handle_cli_args(app_handle, args, cmd));

#[cfg(debug_assertions)]
{
Expand All @@ -124,6 +130,12 @@ fn main() {
main_window.close_devtools();
}

#[cfg(not(debug_assertions))]
{
app_handle.plugin(tauri_plugin_updater::Builder::new().build())?;
check_for_update(app_handle.clone())?;
}

Ok(())
})
.invoke_handler(invoke_handler)
Expand Down
101 changes: 101 additions & 0 deletions apps/core/src-tauri/src/updater.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use tauri::AppHandle;

#[cfg(target_os = "linux")]
use tauri::{Env, Manager};
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
use tauri_plugin_updater::{Update, UpdaterExt};

/// Button actions displayed in the new update dialog.
enum NewUpdate {
/// If the user would like to install the new update.
Yes,
/// If the user don't want to install the new update.
No,
}

impl From<NewUpdate> for String {
fn from(val: NewUpdate) -> Self {
match val {
NewUpdate::Yes => String::from("Yes"),
NewUpdate::No => String::from("No"),
}
}
}

/// Checks if there is a version later than the current version, if the user
/// wan't to install the new update, it downloads and installs it.
///
/// Inspired from [here](https://github.com/i-c-b/example-tauri-v2-updater-action/blob/master/src-tauri/src/main.rs)
pub fn check_for_update(app_handle: AppHandle) -> tauri::Result<()> {
app_handle.plugin(tauri_plugin_updater::Builder::new().build())?;

#[cfg(target_os = "linux")]
let updater_enabled = cfg!(dev) || app_handle.state::<Env>().appimage.is_some();
#[cfg(not(target_os = "linux"))]
let updater_enabled = true;

if updater_enabled {
tauri::async_runtime::spawn(async move {
let updater = app_handle.updater();
match updater {
Ok(updater) => {
let response = updater.check().await;
match response {
Ok(update_option) => {
if let Some(update) = update_option {
show_update_dialog(app_handle, update).await;
} else {
println!("running latest version");
}
}
Err(e) => {
println!("updater failed to check: {}", e);
}
}
}
Err(e) => {
println!("updater failed to build: {}", e);
}
}
});
} else {
println!("updater not enabled");
}
Ok(())
}

/// Dialog displayed when there is a new update, with `yes` and `no` actions to direct the behavior.
///
/// If the user selects `yes` it downloads, installs the new update and restart the application.
async fn show_update_dialog(app_handle: AppHandle, update: Update) {
let error_dialog = app_handle
.dialog()
.message("Something went wrong while installing new version")
.kind(MessageDialogKind::Error)
.title("Error");

let update_dialog = app_handle.dialog();
update_dialog
.message(format!(
"TableX v{} is now available -- you have v{}.\n\nWould you like to install it now?",
update.version, update.current_version
))
.ok_button_label(NewUpdate::Yes)
.cancel_button_label(NewUpdate::No)
.show(move |result| match result {
true => {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();

rt.block_on(async {
let status = update.download_and_install(|_, _| {}, || {}).await;
if status.is_err() {
error_dialog.blocking_show();
}
});
}
false => {}
});
}

0 comments on commit 3f84331

Please sign in to comment.