Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Added auto updater
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixeluted committed Apr 21, 2024
1 parent 50eb7c0 commit b6f58a1
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
Binary file added src-tauri/binaries/krampui-updater.exe
Binary file not shown.
83 changes: 75 additions & 8 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use rdev::{listen, Event, EventType};
use reqwest::Client;
use serde::Serialize;
use serde_json::Value;
use tokio::io::AsyncWriteExt;
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;
use std::process::{exit, Command};
use std::sync::{Arc, Mutex};
use std::{
thread::{self, sleep},
Expand All @@ -22,7 +24,7 @@ use tokio::{
fs::{self, File},
io::AsyncReadExt,
};
use win_msgbox::{w, YesNo};
use win_msgbox::{w, Okay, YesNo};

#[derive(Clone, Serialize)]
struct Payload {
Expand Down Expand Up @@ -271,11 +273,9 @@ async fn validate_executable(executable_path: String) -> (bool, String) {
}
}

#[tokio::main]
async fn main() {
control::set_virtual_terminal(true).ok();

if let Some((latest_version, link)) = get_latest_release().await {
#[command]
async fn check_for_updates(auto_update_enabled: bool) {
if let Some((latest_version, _link)) = get_latest_release().await {
let current_version = env!("CARGO_PKG_VERSION");

let latest_version_number = match latest_version.replace(".", "").parse::<i32>() {
Expand All @@ -292,6 +292,11 @@ async fn main() {
&& current_version_number.is_some()
&& latest_version_number.unwrap() > current_version_number.unwrap()
{
if auto_update_enabled == true {
start_updater().await;
return;
}

let message = format!(
"Would you like to update?\nYou are on v{}, the latest is v{}.",
current_version, latest_version
Expand All @@ -306,11 +311,72 @@ async fn main() {
.unwrap();

if response == YesNo::Yes {
open::that(link).unwrap();
start_updater().await;
return;
}
}
}
}

async fn start_updater() {
let krampui_updater_exe: &[u8] = include_bytes!("../binaries/krampui-updater.exe");
let executable_directory = std::env::current_dir().unwrap();
let target_updater_path = executable_directory.join("krampui-updater.exe");

let mut file = match File::create(&target_updater_path).await {
Ok(file) => file,
Err(err) => { println!("Error when trying to open the file for updater: {}", err.to_string()); return; }
};

match file.write_all(krampui_updater_exe).await {
Ok(_) => {},
Err(err) => { println!("Error when trying to write the updater: {}", err.to_string()); return; }
}

match file.flush().await {
Ok(_) => {},
Err(err) => { println!("Error when trying to flush the updater: {}", err.to_string()); return; }
}

drop(file);
while !target_updater_path.exists() {
tokio::time::sleep(Duration::from_millis(100)).await;
}

match Command::new(&target_updater_path).spawn() {
Ok(child) => child,
Err(err) => { println!("Failed to start the updater: {}", err); return; }
};
exit(0);
}

async fn check_for_pending_update() {
let updater_path = std::env::current_dir().unwrap().join("krampui-updater.exe");

match fs::try_exists(&updater_path).await {
Ok(exists) => {
if exists == true {
fs::remove_file(updater_path).await.ok();
let message = "Sucessfully updated!";
let wide_message: Vec<u16> = OsString::from(&message)
.encode_wide()
.chain(Some(0))
.collect();
win_msgbox::information::<Okay>(wide_message.as_ptr())
.title(w!("KrampUI"))
.show()
.ok();
}
},
Err(_) => {}
}
}

#[tokio::main]
async fn main() {
control::set_virtual_terminal(true).ok();

check_for_pending_update().await;

let toggle = CustomMenuItem::new("toggle".to_string(), "Toggle");
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
Expand Down Expand Up @@ -365,7 +431,8 @@ async fn main() {
write_binary_file,
delete_directory,
delete_file,
validate_executable
validate_executable,
check_for_updates
])
.run(generate_context!())
.expect("Failed to launch application.");
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
},
"shell": {
"open": ".*",
"scope": [{ "name": "cmd", "cmd": "cmd", "args": true }]
"scope": [
{ "name": "cmd", "cmd": "cmd", "args": true }
]
}
},
"windows": [
Expand Down
17 changes: 15 additions & 2 deletions src/assets/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,17 @@ async function getSettings() {
topMost: json.topMost,
keyToggle: json.keyToggle,
editorFontSize: json.editorFontSize || 14,
injectionDelay: json.injectionDelay || 10
injectionDelay: json.injectionDelay || 10,
autoUpdate: json.autoUpdate || true
};
else {
const settings = {
autoInject: false,
topMost: true,
keyToggle: false,
editorFontSize: 14,
injectionDelay: 10
injectionDelay: 10,
autoUpdate: true
};

await saveSettings(settings);
Expand Down Expand Up @@ -2103,13 +2105,15 @@ async function main() {
const homeToggleButton = document.querySelector(".home-toggle");
const fontSizeValue = document.querySelector(".font-size");
const autoInjectDelayValue = document.querySelector(".auto-inject-delay");
const autoUpdateButton = document.querySelector(".auto-update");
const selectLoaderButton = document.querySelector(".kr-select-loader");
const deleteLoaderButton = document.querySelector(".kr-delete-loader");

async function updateSettingsUI() {
autoInjectButton.innerText = settings.autoInject ? "Enabled" : "Disabled";
topMostButton.innerText = settings.topMost ? "Enabled" : "Disabled";
homeToggleButton.innerText = settings.keyToggle ? "Enabled" : "Disabled";
autoUpdateButton.innerText = settings.autoUpdate ? "Enabled" : "Disabled";
fontSizeValue.value = settings.editorFontSize;
autoInjectDelayValue.value = settings.injectionDelay;

Expand Down Expand Up @@ -2140,6 +2144,12 @@ async function main() {
updateSettingsUI();
});

autoUpdateButton.addEventListener("click", function () {
settings.autoUpdate = !settings.autoUpdate;
saveSettings();
updateSettingsUI();
})

fontSizeValue.addEventListener("keydown", function (event) {
if (!(/[0-9]|Backspace|Tab|ArrowLeft|ArrowRight|Delete|Enter/.test(event.key))) {
event.preventDefault();
Expand Down Expand Up @@ -2228,6 +2238,9 @@ async function main() {

// Show
show();

console.log(settings.autoUpdate)
invoke("check_for_updates", { autoUpdateEnabled: settings.autoUpdate });
}

function error(e) {
Expand Down
7 changes: 7 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
</div>
<input class="kr-input no-spinner font-size" type="number">
</div>
<div class="setting">
<div class="setting-info">
<p class="setting-name">Auto Update</p>
<p class="setting-description">Automatically updates to latest version on startup</p>
</div>
<button class="kr-button auto-update">Enabled</button>
</div>
</div>
</div>
</body>
Expand Down

0 comments on commit b6f58a1

Please sign in to comment.