From e97e07d8da16a175dfc60982a32822da10be892a Mon Sep 17 00:00:00 2001 From: Ludea Date: Thu, 17 Oct 2024 11:09:39 +0000 Subject: [PATCH 1/3] wait for child process --- plugins/shell/src/commands.rs | 15 +++++++++++++++ plugins/shell/src/error.rs | 2 ++ plugins/shell/src/process/mod.rs | 6 ++++++ 3 files changed, 23 insertions(+) diff --git a/plugins/shell/src/commands.rs b/plugins/shell/src/commands.rs index 3345bb3a6..ed043813a 100644 --- a/plugins/shell/src/commands.rs +++ b/plugins/shell/src/commands.rs @@ -17,6 +17,7 @@ use crate::{ scope::ExecuteArgs, Shell, }; +use std::process::ExitStatus; type ChildId = u32; @@ -302,6 +303,20 @@ pub fn kill( Ok(()) } +#[tauri::command] +pub fn wait( + _window: Window, + shell: State<'_, Shell>, + pid: ChildId, +) -> crate::Result { + if let Some(child) = shell.children.lock().unwrap().get(&pid) { + let exitstatus: ExitStatus = child.wait()?; + Ok(exitstatus) + } else { + Err(crate::Error::UnknowChildProcess) + } +} + #[tauri::command] pub async fn open( _window: Window, diff --git a/plugins/shell/src/error.rs b/plugins/shell/src/error.rs index 652421b8f..5ef03b81d 100644 --- a/plugins/shell/src/error.rs +++ b/plugins/shell/src/error.rs @@ -33,6 +33,8 @@ pub enum Error { /// Utf8 error. #[error(transparent)] Utf8(#[from] std::string::FromUtf8Error), + #[error("Child process does'nt exist")] + UnknowChildProcess, } impl Serialize for Error { diff --git a/plugins/shell/src/process/mod.rs b/plugins/shell/src/process/mod.rs index 44f037b01..376eadc43 100644 --- a/plugins/shell/src/process/mod.rs +++ b/plugins/shell/src/process/mod.rs @@ -84,6 +84,12 @@ impl CommandChild { pub fn pid(&self) -> u32 { self.inner.id() } + + /// Waits for the child to exit completely, returning the status that it exited with. + pub fn wait(self) -> crate::Result { + let exitstatus = self.inner.wait()?; + Ok(exitstatus) + } } /// Describes the result of a process after it has terminated. From 9ebfbf0296547999416028461dd76572bbcf9bbd Mon Sep 17 00:00:00 2001 From: Ludea Date: Thu, 17 Oct 2024 13:44:43 +0200 Subject: [PATCH 2/3] typescript api --- plugins/shell/guest-js/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/plugins/shell/guest-js/index.ts b/plugins/shell/guest-js/index.ts index 1ed2ac5d7..36ff695f0 100644 --- a/plugins/shell/guest-js/index.ts +++ b/plugins/shell/guest-js/index.ts @@ -339,6 +339,20 @@ class Child { pid: this.pid }) } + + /** + * Waits for the child to exit completely, returning the status that it exited with. + * + * @returns A promise with ExitStatus. + * + * @since 2.0.2 + */ + async wait(): Promise { + await invoke('plugin:shell|wait', { + cmd: 'waitChild', + pid: this.pid + }) + } } interface CommandEvents { From 8be767d3c31901c449c0c88e5003ab18e879311c Mon Sep 17 00:00:00 2001 From: Ludea Date: Thu, 17 Oct 2024 15:50:47 +0000 Subject: [PATCH 3/3] make wait async and use ExitStatus crate --- plugins/shell/src/commands.rs | 8 +++----- plugins/shell/src/process/mod.rs | 6 ++++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/shell/src/commands.rs b/plugins/shell/src/commands.rs index ed043813a..771788b0d 100644 --- a/plugins/shell/src/commands.rs +++ b/plugins/shell/src/commands.rs @@ -13,11 +13,10 @@ use tauri::{ use crate::{ open::Program, - process::{CommandEvent, TerminatedPayload}, + process::{CommandEvent, ExitStatus, TerminatedPayload}, scope::ExecuteArgs, Shell, }; -use std::process::ExitStatus; type ChildId = u32; @@ -304,14 +303,13 @@ pub fn kill( } #[tauri::command] -pub fn wait( +pub async fn wait( _window: Window, shell: State<'_, Shell>, pid: ChildId, ) -> crate::Result { if let Some(child) = shell.children.lock().unwrap().get(&pid) { - let exitstatus: ExitStatus = child.wait()?; - Ok(exitstatus) + child.wait().await } else { Err(crate::Error::UnknowChildProcess) } diff --git a/plugins/shell/src/process/mod.rs b/plugins/shell/src/process/mod.rs index 376eadc43..c0cc6d1e1 100644 --- a/plugins/shell/src/process/mod.rs +++ b/plugins/shell/src/process/mod.rs @@ -86,9 +86,11 @@ impl CommandChild { } /// Waits for the child to exit completely, returning the status that it exited with. - pub fn wait(self) -> crate::Result { + pub async fn wait(self) -> crate::Result { let exitstatus = self.inner.wait()?; - Ok(exitstatus) + Ok(ExitStatus { + code: exitstatus.code(), + }) } }