From 2646ed08e70b67768b116eab8d1ed017d7a60238 Mon Sep 17 00:00:00 2001 From: Raunak Raj <71929976+bajrangCoder@users.noreply.github.com> Date: Mon, 25 Mar 2024 23:44:29 +0530 Subject: [PATCH] linux: Implement `restart` and `app_path` (#9681) Added `restart` and `app_path` method for linux platform which was marked as `//todo(linux)` Release Notes: - N/A --- crates/gpui/src/platform/linux/platform.rs | 48 +++++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/gpui/src/platform/linux/platform.rs b/crates/gpui/src/platform/linux/platform.rs index 3394c624f74318c7b68ff7d38e87a9d476e74e78..c19e2f47af92b3ae9ac40cd7ab9e79a8d2f02dae 100644 --- a/crates/gpui/src/platform/linux/platform.rs +++ b/crates/gpui/src/platform/linux/platform.rs @@ -4,6 +4,7 @@ use std::cell::RefCell; use std::env; use std::{ path::{Path, PathBuf}, + process::Command, rc::Rc, sync::Arc, time::Duration, @@ -148,8 +149,44 @@ impl Platform for LinuxPlatform { self.inner.loop_signal.stop(); } - // todo(linux) - fn restart(&self) {} + fn restart(&self) { + use std::os::unix::process::CommandExt as _; + + // get the process id of the current process + let app_pid = std::process::id().to_string(); + // get the path to the executable + let app_path = match self.app_path() { + Ok(path) => path, + Err(err) => { + log::error!("Failed to get app path: {:?}", err); + return; + } + }; + + // script to wait for the current process to exit and then restart the app + let script = format!( + r#" + while kill -O {pid} 2>/dev/null; do + sleep 0.1 + done + {app_path} + "#, + pid = app_pid, + app_path = app_path.display() + ); + + // execute the script using /bin/bash + let restart_process = Command::new("/bin/bash") + .arg("-c") + .arg(script) + .process_group(0) + .spawn(); + + match restart_process { + Ok(_) => self.quit(), + Err(e) => log::error!("failed to spawn restart script: {:?}", e), + } + } // todo(linux) fn activate(&self, ignoring_other_apps: bool) {} @@ -331,11 +368,10 @@ impl Platform for LinuxPlatform { }) } - //todo(linux) fn app_path(&self) -> Result { - Err(anyhow::Error::msg( - "Platform::app_path is not implemented yet", - )) + // get the path of the executable of the current process + let exe_path = std::env::current_exe()?; + Ok(exe_path) } // todo(linux)