#![doc(
html_logo_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png",
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
)]
use std::{
collections::HashMap,
ffi::OsStr,
path::Path,
sync::{Arc, Mutex},
};
use process::{Command, CommandChild};
use tauri::{
AppHandle, Manager, RunEvent, Runtime,
plugin::{Builder, TauriPlugin},
};
mod commands;
mod error;
pub mod process;
mod scope;
mod scope_entry;
pub use error::Error;
type Result<T> = std::result::Result<T, Error>;
type ChildStore = Arc<Mutex<HashMap<u32, CommandChild>>>;
pub struct Shell<R: Runtime> {
#[allow(dead_code)]
app: AppHandle<R>,
children: ChildStore,
}
impl<R: Runtime> Shell<R> {
pub fn command(&self, program: impl AsRef<OsStr>) -> Command {
Command::new(program)
}
pub fn sidecar(&self, program: impl AsRef<Path>) -> Result<Command> {
Command::new_sidecar(program)
}
}
pub trait ShellExt<R: Runtime> {
fn shell(&self) -> &Shell<R>;
}
impl<R: Runtime, T: Manager<R>> ShellExt<R> for T {
fn shell(&self) -> &Shell<R> {
self.state::<Shell<R>>().inner()
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("shell")
.initialization_script(include_str!("init-iife.js").to_string())
.invoke_handler(tauri::generate_handler![
commands::execute,
commands::spawn,
commands::stdin_write,
commands::kill,
])
.setup(|app, _api| {
app.manage(Shell {
app: app.clone(),
children: Default::default(),
});
Ok(())
})
.on_event(|app, event| {
if let RunEvent::Exit = event {
let shell = app.state::<Shell<R>>();
let children = {
let mut lock = shell.children.lock().unwrap();
std::mem::take(&mut *lock)
};
for child in children.into_values() {
let _ = child.kill();
}
}
})
.build()
}