From 700784fac0eeaf6d296d357687442f12ca93fa7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Thu, 29 Sep 2022 20:17:14 +0200 Subject: [PATCH] Rename exe instead of deletion (#1395) --- lapce-data/src/update.rs | 30 ++++++++++++++++++++++++++++-- lapce-ui/src/app.rs | 3 +++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lapce-data/src/update.rs b/lapce-data/src/update.rs index a24079de..26d07f6b 100644 --- a/lapce-data/src/update.rs +++ b/lapce-data/src/update.rs @@ -55,6 +55,7 @@ pub fn download_release(release: &ReleaseInfo) -> Result { let name = match std::env::consts::OS { "macos" => "Lapce-macos.dmg", "linux" => "Lapce-linux.tar.gz", + // TODO(dbuga): download installer for non-portable "windows" => "Lapce-windows-portable.zip", _ => return Err(anyhow!("os not supported")), }; @@ -114,13 +115,23 @@ pub fn extract(src: &Path, process_path: &Path) -> Result { #[cfg(target_os = "windows")] pub fn extract(src: &Path, process_path: &Path) -> Result { - let parent = src.parent().ok_or_else(|| anyhow::anyhow!("no parent"))?; + let parent = src + .parent() + .ok_or_else(|| anyhow::anyhow!("src has no parent"))?; + let dst_parent = process_path + .parent() + .ok_or_else(|| anyhow::anyhow!("process_path has no parent"))?; + { let mut archive = zip::ZipArchive::new(std::fs::File::open(src)?)?; archive.extract(parent)?; } - std::fs::remove_file(process_path)?; + + // TODO(dbuga): instead of replacing the exe, run the msi installer for non-portable + // TODO(dbuga): there's a very slight chance the user might end up with a backup file without a working .exe + std::fs::rename(process_path, dst_parent.join("lapce.exe.bak"))?; std::fs::copy(parent.join("lapce.exe"), process_path)?; + Ok(process_path.to_path_buf()) } @@ -158,3 +169,18 @@ pub fn restart(path: &Path) -> Result<()> { .spawn()?; Ok(()) } + +#[cfg(target_os = "windows")] +pub fn cleanup() { + // Clean up backup exe after an update + if let Ok(process_path) = std::env::current_exe() { + if let Some(dst_parent) = process_path.parent() { + let _ = std::fs::remove_file(dst_parent.join("lapce.exe.bak")); + } + } +} + +#[cfg(not(target_os = "windows"))] +pub fn cleanup() { + // Nothing to do yet +} diff --git a/lapce-ui/src/app.rs b/lapce-ui/src/app.rs index 0e0e46a4..e47ac9e2 100644 --- a/lapce-ui/src/app.rs +++ b/lapce-ui/src/app.rs @@ -43,6 +43,9 @@ pub fn launch() { return; } + #[cfg(feature = "updater")] + lapce_data::update::cleanup(); + let mut log_dispatch = fern::Dispatch::new() .format(|out, message, record| { out.finish(format_args!(