diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml index 73bfb2d8b..6a06fc398 100644 --- a/.github/workflows/release-build.yml +++ b/.github/workflows/release-build.yml @@ -151,6 +151,13 @@ jobs: $hash = (Get-FileHash $asset -Algorithm SHA256).Hash.ToLower() "$hash $asset" | Out-File -Encoding ascii "$asset.sha256" gh release upload $env:TAG $asset "$asset.sha256" --clobber + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + if ($env:TAURI_SIGNING_PRIVATE_KEY) { + pnpm exec tauri signer sign --app-version $version $asset + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + gh release upload $env:TAG "$asset.sig" --clobber + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + } # The remote development server, per host platform it can run on. The desktop # app downloads the one for the host (ide-tools `server`) and installs it over SSH. @@ -233,6 +240,8 @@ jobs: [/_x64\.app\.tar\.gz$/, ["darwin-x86_64", "darwin-x86_64-app"]], ...windows("aarch64", /_arm64_en-US\.msi$/, /_arm64-setup\.exe$/), ...windows("x86_64", /_x64_en-US\.msi$/, /_x64-setup\.exe$/), + [/_windows_arm64_portable\.zip$/, ["windows-aarch64-portable"]], + [/_windows_x64_portable\.zip$/, ["windows-x86_64-portable"]], ]; const platforms = {}; for (const sig of sigs) { diff --git a/crates/ide-tools/Cargo.toml b/crates/ide-tools/Cargo.toml index dea069bd2..a3a792cfd 100644 --- a/crates/ide-tools/Cargo.toml +++ b/crates/ide-tools/Cargo.toml @@ -19,8 +19,6 @@ tar = "0.4" zip.workspace = true sha2 = "0.10" hex = "0.4" - -[dev-dependencies] tempfile.workspace = true [target.'cfg(unix)'.dev-dependencies] diff --git a/crates/ide-tools/src/lib.rs b/crates/ide-tools/src/lib.rs index 6fe4c2876..08b28d196 100644 --- a/crates/ide-tools/src/lib.rs +++ b/crates/ide-tools/src/lib.rs @@ -20,6 +20,8 @@ mod bundled; pub mod formatter; pub mod jdk; pub mod platform; +#[cfg(any(windows, test))] +pub mod portable_update; mod process; pub mod registry; pub mod server; diff --git a/crates/ide-tools/src/portable_update.rs b/crates/ide-tools/src/portable_update.rs new file mode 100644 index 000000000..20295fa92 --- /dev/null +++ b/crates/ide-tools/src/portable_update.rs @@ -0,0 +1,223 @@ +//! In-place updates of the Windows unpack-and-run distribution. + +use ide_core::{IdeError, IdeResult}; +use std::fs; +use std::io::{self, Cursor}; +use std::path::{Path, PathBuf}; + +/// `bytes` must already have passed the updater's signature verification. +pub fn install(exe: &Path, bytes: &[u8]) -> IdeResult<()> { + let directory = exe.parent().ok_or_else(|| IdeError::invalid("executable has no parent directory"))?; + let staging = tempfile::Builder::new().prefix(".openidea-update-").tempdir_in(directory)?; + let files = unpack(bytes, staging.path(), exe)?; + let mut replaced = Vec::new(); + for (source, target) in files { + match replace_file(&source, &target) { + Ok(backup) => replaced.push((target, backup)), + Err(error) => { + for (target, backup) in replaced.into_iter().rev() { + fs::remove_file(&target)?; + if let Some(backup) = backup { + fs::rename(&backup, &target).map_err(|restore| { + IdeError::internal(format!( + "{error}; restore {} from {}: {restore}", + target.display(), + backup.display() + )) + })?; + } + } + return Err(error); + } + } + } + Ok(()) +} + +/// Old executables stay locked until their process exits; retry cleanup on the next start. +pub fn cleanup(exe: &Path) { + for target in [exe.to_path_buf(), exe.with_file_name("WebView2Loader.dll")] { + let _ = fs::remove_file(backup_path(&target)); + } +} + +fn unpack(bytes: &[u8], staging: &Path, exe: &Path) -> IdeResult> { + let invalid = |error| IdeError::invalid(format!("portable update archive: {error}")); + let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).map_err(invalid)?; + let mut files = Vec::new(); + let mut has_exe = false; + for index in 0..archive.len() { + let mut entry = archive.by_index(index).map_err(invalid)?; + let name = entry.name().map_err(invalid)?; + let target = match name.as_ref() { + "openidea.exe" => { + has_exe = true; + exe.to_path_buf() + } + "WebView2Loader.dll" => exe.with_file_name("WebView2Loader.dll"), + name => return Err(IdeError::invalid(format!("unexpected portable update file: {name}"))), + }; + if !entry.is_file() || entry.is_symlink() || files.iter().any(|(_, path)| path == &target) { + return Err(IdeError::invalid("portable update contains a duplicate or non-regular file")); + } + let source = staging.join(name.as_ref()); + let mut file = fs::File::create(&source)?; + io::copy(&mut entry, &mut file)?; + file.sync_all()?; + files.push((source, target)); + } + if !has_exe { + return Err(IdeError::invalid("portable update is missing openidea.exe")); + } + Ok(files) +} + +fn replace_file(source: &Path, target: &Path) -> IdeResult> { + let backup = if target.try_exists()? { + if !target.is_file() { + return Err(IdeError::invalid(format!("not an application file: {}", target.display()))); + } + let backup = backup_path(target); + if backup.try_exists()? { + fs::remove_file(&backup)?; + } + // Windows forbids overwriting a running exe, but allows renaming it. + fs::rename(target, &backup)?; + Some(backup) + } else { + None + }; + if let Err(error) = fs::rename(source, target) { + if let Some(backup) = &backup { + fs::rename(backup, target).map_err(|restore| { + IdeError::internal(format!( + "{error}; restore {} from {}: {restore}", + target.display(), + backup.display() + )) + })?; + } + return Err(error.into()); + } + Ok(backup) +} + +fn backup_path(target: &Path) -> PathBuf { + let mut path = target.as_os_str().to_os_string(); + path.push(".openidea-old"); + PathBuf::from(path) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::io::Write; + + fn package(files: &[(&str, &[u8])]) -> Vec { + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + for (name, bytes) in files { + zip.start_file(*name, zip::write::SimpleFileOptions::default()).unwrap(); + zip.write_all(bytes).unwrap(); + } + zip.finish().unwrap().into_inner() + } + + #[test] + fn replaces_application_files_and_preserves_configuration() { + for name in ["openidea.exe", "我的 IDE.exe"] { + let tmp = tempfile::tempdir().unwrap(); + let exe = tmp.path().join(name); + fs::write(&exe, b"old exe").unwrap(); + fs::write(tmp.path().join("WebView2Loader.dll"), b"old dll").unwrap(); + fs::write(tmp.path().join("settings.toml"), b"my settings").unwrap(); + fs::create_dir(tmp.path().join("data")).unwrap(); + fs::write(tmp.path().join("data/plugins.json"), b"my plugins").unwrap(); + let bytes = package(&[("openidea.exe", b"new exe"), ("WebView2Loader.dll", b"new dll")]); + install(&exe, &bytes).unwrap(); + assert_eq!(fs::read(&exe).unwrap(), b"new exe"); + assert_eq!(fs::read(tmp.path().join("WebView2Loader.dll")).unwrap(), b"new dll"); + assert_eq!(fs::read(tmp.path().join("settings.toml")).unwrap(), b"my settings"); + assert_eq!(fs::read(tmp.path().join("data/plugins.json")).unwrap(), b"my plugins"); + cleanup(&exe); + assert!(!backup_path(&exe).exists()); + assert!(!backup_path(&tmp.path().join("WebView2Loader.dll")).exists()); + } + } + + #[test] + fn accepts_an_exe_only_package() { + let tmp = tempfile::tempdir().unwrap(); + let exe = tmp.path().join("openidea.exe"); + fs::write(&exe, b"old").unwrap(); + install(&exe, &package(&[("openidea.exe", b"new")])).unwrap(); + assert_eq!(fs::read(&exe).unwrap(), b"new"); + } + + #[test] + fn rejects_incomplete_or_unexpected_payloads_before_changing_files() { + for files in [ + vec![("WebView2Loader.dll", b"new".as_slice())], + vec![("openidea.exe", b"new"), ("settings.toml", b"overwrite")], + vec![("openidea.exe", b"new"), ("../outside.txt", b"escape")], + ] { + let tmp = tempfile::tempdir().unwrap(); + let exe = tmp.path().join("openidea.exe"); + fs::write(&exe, b"old").unwrap(); + assert!(install(&exe, &package(&files)).is_err()); + assert_eq!(fs::read(&exe).unwrap(), b"old"); + } + } + + #[cfg(windows)] + #[test] + fn updates_a_running_executable_and_restores_on_a_locked_companion() { + use std::os::windows::fs::OpenOptionsExt; + use std::process::{Command, Stdio}; + + let tmp = tempfile::tempdir().unwrap(); + let exe = tmp.path().join("openidea.exe"); + let ping = Path::new(&std::env::var_os("SystemRoot").unwrap()).join("System32/PING.EXE"); + fs::copy(ping, &exe).unwrap(); + let original = fs::read(&exe).unwrap(); + let dll = tmp.path().join("WebView2Loader.dll"); + fs::write(&dll, b"old dll").unwrap(); + let mut command = Command::new(&exe); + command.args(["-n", "30", "127.0.0.1"]).stdout(Stdio::null()).stderr(Stdio::null()); + ide_core::process::prepare(&mut command); + let mut child = command.spawn().unwrap(); + let result = { + assert!(fs::write(&exe, b"overwrite").is_err(), "fixture must hold the running exe"); + let held = fs::OpenOptions::new().read(true).share_mode(1).open(&dll).unwrap(); + let bytes = package(&[("openidea.exe", b"new exe"), ("WebView2Loader.dll", b"new dll")]); + assert!(install(&exe, &bytes).is_err()); + assert_eq!(fs::read(&exe).unwrap(), original); + assert_eq!(fs::read(&dll).unwrap(), b"old dll"); + drop(held); + install(&exe, &bytes) + }; + let _ = child.kill(); + let _ = child.wait(); + result.unwrap(); + assert_eq!(fs::read(&exe).unwrap(), b"new exe"); + assert_eq!(fs::read(&dll).unwrap(), b"new dll"); + cleanup(&exe); + assert!(!backup_path(&exe).exists()); + } + + #[cfg(windows)] + #[test] + fn restores_the_old_file_when_the_new_file_cannot_be_moved() { + use std::os::windows::fs::OpenOptionsExt; + + let tmp = tempfile::tempdir().unwrap(); + let exe = tmp.path().join("openidea.exe"); + let source = tmp.path().join("new.exe"); + fs::write(&exe, b"old").unwrap(); + fs::write(&source, b"new").unwrap(); + let _held = fs::OpenOptions::new().read(true).share_mode(1).open(&source).unwrap(); + assert!(replace_file(&source, &exe).is_err()); + assert_eq!(fs::read(&exe).unwrap(), b"old"); + assert_eq!(fs::read(&source).unwrap(), b"new"); + assert!(!backup_path(&exe).exists()); + } +} diff --git a/scripts/cnb-mirror.test.ts b/scripts/cnb-mirror.test.ts index ee59cc8c2..c48266cd3 100644 --- a/scripts/cnb-mirror.test.ts +++ b/scripts/cnb-mirror.test.ts @@ -11,6 +11,14 @@ describe("mirrorManifest", () => { platforms: { "darwin-aarch64": { signature: "sig-mac", url: `${gh}/OpenIDEA_aarch64.app.tar.gz` }, "windows-x86_64-nsis": { signature: "sig-win", url: `${gh}/OpenIDEA_0.1.38_x64-setup.exe` }, + "windows-x86_64-portable": { + signature: "sig-portable", + url: `${gh}/OpenIDEA_0.1.38_windows_x64_portable.zip`, + }, + "windows-aarch64-portable": { + signature: "sig-portable-arm", + url: `${gh}/OpenIDEA_0.1.38_windows_arm64_portable.zip`, + }, }, }; @@ -21,6 +29,14 @@ describe("mirrorManifest", () => { platforms: { "darwin-aarch64": { signature: "sig-mac", url: `${cnb}/OpenIDEA_aarch64.app.tar.gz` }, "windows-x86_64-nsis": { signature: "sig-win", url: `${cnb}/OpenIDEA_0.1.38_x64-setup.exe` }, + "windows-x86_64-portable": { + signature: "sig-portable", + url: `${cnb}/OpenIDEA_0.1.38_windows_x64_portable.zip`, + }, + "windows-aarch64-portable": { + signature: "sig-portable-arm", + url: `${cnb}/OpenIDEA_0.1.38_windows_arm64_portable.zip`, + }, }, }); }); diff --git a/scripts/updater-manifest.test.ts b/scripts/updater-manifest.test.ts new file mode 100644 index 000000000..a6c931f75 --- /dev/null +++ b/scripts/updater-manifest.test.ts @@ -0,0 +1,77 @@ +// @vitest-environment node +import { readFileSync } from "node:fs"; +import { afterEach, expect, it, vi } from "vitest"; + +const workflow = readFileSync(new URL("../.github/workflows/release-build.yml", import.meta.url), "utf8").replaceAll( + "\r\n", + "\n", +); +const job = workflow.split("\n updater-manifest:")[1]!; +const script = job.split(" script: |\n")[1]!.replace(/^ {12}/gm, ""); +const run = new Function("github", "context", "core", `return (async () => {${script}\n})();`); + +afterEach(() => vi.unstubAllEnvs()); + +it.each(["v0.1.42", "v0.1.43-nightly.20261112"])( + "publishes signed portable updates for both Windows architectures alongside the installers (%s)", + async (tag) => { + vi.stubEnv("TAG", tag); + vi.stubEnv("RELEASE_ID", "42"); + const version = tag.slice(1); + const nightly = tag.includes("-nightly."); + const bundles = [ + "OpenIDEA_aarch64.AppImage", + "OpenIDEA_arm64.deb", + "OpenIDEA.aarch64.rpm", + "OpenIDEA_amd64.AppImage", + "OpenIDEA_amd64.deb", + "OpenIDEA.x86_64.rpm", + "OpenIDEA_aarch64.app.tar.gz", + "OpenIDEA_x64.app.tar.gz", + "OpenIDEA_arm64_en-US.msi", + "OpenIDEA_arm64-setup.exe", + "OpenIDEA_x64_en-US.msi", + "OpenIDEA_x64-setup.exe", + `OpenIDEA_${version}_windows_x64_portable.zip`, + `OpenIDEA_${version}_windows_arm64_portable.zip`, + ].filter((name) => !nightly || (!name.endsWith(".msi") && !name.endsWith(".rpm"))); + const assets = bundles.flatMap((name, id) => [ + { name, id: id * 2 }, + { name: `${name}.sig`, id: id * 2 + 1 }, + ]); + const upload = vi.fn(); + const failed = vi.fn(); + await run( + { + paginate: async () => assets, + rest: { + repos: { + listReleaseAssets: vi.fn(), + getRelease: async () => ({ data: { body: "Release notes" } }), + getReleaseAsset: async ({ asset_id }: { asset_id: number }) => ({ + data: Buffer.from(`signature-${asset_id}\n`), + }), + uploadReleaseAsset: upload, + }, + }, + }, + { repo: { owner: "l0ng-ai", repo: "openidea" } }, + { notice: vi.fn(), info: vi.fn(), setFailed: failed }, + ); + expect(failed).not.toHaveBeenCalled(); + const manifest = JSON.parse(upload.mock.calls[0]![0].data); + for (const [arch, filename] of [ + ["x86_64", "x64"], + ["aarch64", "arm64"], + ]) { + const name = `OpenIDEA_${version}_windows_${filename}_portable.zip`; + const sig = assets.find((a) => a.name === `${name}.sig`)!; + expect(manifest.platforms[`windows-${arch}-portable`]).toEqual({ + signature: `signature-${sig.id}`, + url: `https://github.com/l0ng-ai/openidea/releases/download/${tag}/${name}`, + }); + expect(manifest.platforms[`windows-${arch}`].url).toContain(nightly ? "-setup.exe" : "_en-US.msi"); + expect(manifest.platforms[`windows-${arch}-nsis`].url).toContain("-setup.exe"); + } + }, +); diff --git a/src-tauri/src/commands/window.rs b/src-tauri/src/commands/window.rs index 6cc3f4ab8..b0a26e8b0 100644 --- a/src-tauri/src/commands/window.rs +++ b/src-tauri/src/commands/window.rs @@ -464,7 +464,15 @@ fn updater(app: &AppHandle, source: UpdateSource) -> IdeResult>>()?; let proxy = ide_core::proxy_settings::proxy(); - app.updater_builder() + let mut builder = app.updater_builder(); + if let Some(target) = crate::updates::portable_target( + std::env::consts::OS, + std::env::consts::ARCH, + tauri::utils::platform::bundle_type(), + ) { + builder = builder.target(target); + } + builder .configure_client(move |b| ide_http::client::with_proxy(b, &proxy)) .endpoints(endpoints) .map_err(|e| IdeError::invalid(e.to_string()))? @@ -611,10 +619,16 @@ pub async fn app_install_update( _ = token.cancelled() => return Err(IdeError::cancelled()), }; let _ = on_progress.send(UpdateProgress::Installing); - tauri::async_runtime::spawn_blocking(move || update.install(bytes)) - .await - .map_err(|e| IdeError::internal(e.to_string()))? - .map_err(|e| IdeError::internal(e.to_string())) + tauri::async_runtime::spawn_blocking(move || { + #[cfg(windows)] + if update.target.ends_with("-portable") { + let exe = tauri::utils::platform::current_exe()?; + return ide_tools::portable_update::install(&exe, &bytes); + } + update.install(bytes).map_err(|e| IdeError::internal(e.to_string())) + }) + .await + .map_err(|e| IdeError::internal(e.to_string()))? } .await; state.host.end_request(request); diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 6acd9bb04..962f87a70 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -176,6 +176,12 @@ pub fn run() { }) .setup(move |app| { builder.mount_events(app); + #[cfg(windows)] + tauri::async_runtime::spawn_blocking(|| { + if let Ok(exe) = tauri::utils::platform::current_exe() { + ide_tools::portable_update::cleanup(&exe); + } + }); // An unusable directory variable stops the start with a message, never a silent fallback. let identifier = app.config().identifier.clone(); let local_data_dir = local_data_dir.and_then(|dir| { diff --git a/src-tauri/src/updates.rs b/src-tauri/src/updates.rs index 5f1eb6d6c..fe714bcbd 100644 --- a/src-tauri/src/updates.rs +++ b/src-tauri/src/updates.rs @@ -11,6 +11,11 @@ use std::time::{Instant, SystemTime, UNIX_EPOCH}; use tauri::{AppHandle, Manager}; use tokio::sync::Notify; +/// Tauri patches the installer copies; the raw exe shipped in the portable zip keeps no bundle type. +pub fn portable_target(os: &str, arch: &str, bundle: Option) -> Option { + (os == "windows" && bundle.is_none()).then(|| format!("windows-{arch}-portable")) +} + #[derive(Default)] pub struct Updates { started: AtomicBool, @@ -163,6 +168,20 @@ fn ready_owner(preferred: Option<&str>, live: &[String], ready: &HashSet mod tests { use super::*; + #[test] + fn only_unbundled_windows_builds_select_portable_updates() { + use tauri::utils::config::BundleType; + for arch in ["x86_64", "aarch64"] { + assert_eq!(portable_target("windows", arch, None), Some(format!("windows-{arch}-portable"))); + for bundle in [BundleType::Msi, BundleType::Nsis] { + assert_eq!(portable_target("windows", arch, Some(bundle)), None); + } + for os in ["linux", "macos"] { + assert_eq!(portable_target(os, arch, None), None); + } + } + } + #[test] fn notifications_only_target_live_owners_that_have_subscribed() { let live = vec!["main".into(), "project-2".into()];