command.rs

  1use std::ffi::OsStr;
  2#[cfg(not(target_os = "macos"))]
  3use std::path::Path;
  4
  5#[cfg(target_os = "macos")]
  6mod darwin;
  7
  8#[cfg(target_os = "macos")]
  9pub use darwin::{Child, Command, Stdio};
 10
 11#[cfg(target_os = "windows")]
 12const CREATE_NO_WINDOW: u32 = 0x0800_0000_u32;
 13
 14pub fn new_command(program: impl AsRef<OsStr>) -> Command {
 15    Command::new(program)
 16}
 17
 18#[cfg(target_os = "windows")]
 19pub fn new_std_command(program: impl AsRef<OsStr>) -> std::process::Command {
 20    use std::os::windows::process::CommandExt;
 21
 22    let mut command = std::process::Command::new(program);
 23    command.creation_flags(CREATE_NO_WINDOW);
 24    command
 25}
 26
 27#[cfg(not(target_os = "windows"))]
 28pub fn new_std_command(program: impl AsRef<OsStr>) -> std::process::Command {
 29    std::process::Command::new(program)
 30}
 31
 32#[cfg(not(target_os = "macos"))]
 33pub type Child = smol::process::Child;
 34
 35#[cfg(not(target_os = "macos"))]
 36pub use std::process::Stdio;
 37
 38#[cfg(not(target_os = "macos"))]
 39#[derive(Debug)]
 40pub struct Command(smol::process::Command);
 41
 42#[cfg(not(target_os = "macos"))]
 43impl Command {
 44    #[inline]
 45    pub fn new(program: impl AsRef<OsStr>) -> Self {
 46        #[cfg(target_os = "windows")]
 47        {
 48            use smol::process::windows::CommandExt;
 49            let mut cmd = smol::process::Command::new(program);
 50            cmd.creation_flags(CREATE_NO_WINDOW);
 51            Self(cmd)
 52        }
 53        #[cfg(not(target_os = "windows"))]
 54        Self(smol::process::Command::new(program))
 55    }
 56
 57    pub fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
 58        self.0.arg(arg);
 59        self
 60    }
 61
 62    pub fn args<I, S>(&mut self, args: I) -> &mut Self
 63    where
 64        I: IntoIterator<Item = S>,
 65        S: AsRef<OsStr>,
 66    {
 67        self.0.args(args);
 68        self
 69    }
 70
 71    pub fn env(&mut self, key: impl AsRef<OsStr>, val: impl AsRef<OsStr>) -> &mut Self {
 72        self.0.env(key, val);
 73        self
 74    }
 75
 76    pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
 77    where
 78        I: IntoIterator<Item = (K, V)>,
 79        K: AsRef<OsStr>,
 80        V: AsRef<OsStr>,
 81    {
 82        self.0.envs(vars);
 83        self
 84    }
 85
 86    pub fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self {
 87        self.0.env_remove(key);
 88        self
 89    }
 90
 91    pub fn env_clear(&mut self) -> &mut Self {
 92        self.0.env_clear();
 93        self
 94    }
 95
 96    pub fn current_dir(&mut self, dir: impl AsRef<Path>) -> &mut Self {
 97        self.0.current_dir(dir);
 98        self
 99    }
100
101    pub fn stdin(&mut self, cfg: impl Into<Stdio>) -> &mut Self {
102        self.0.stdin(cfg.into());
103        self
104    }
105
106    pub fn stdout(&mut self, cfg: impl Into<Stdio>) -> &mut Self {
107        self.0.stdout(cfg.into());
108        self
109    }
110
111    pub fn stderr(&mut self, cfg: impl Into<Stdio>) -> &mut Self {
112        self.0.stderr(cfg.into());
113        self
114    }
115
116    pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Self {
117        self.0.kill_on_drop(kill_on_drop);
118        self
119    }
120
121    pub fn spawn(&mut self) -> std::io::Result<Child> {
122        self.0.spawn()
123    }
124
125    pub async fn output(&mut self) -> std::io::Result<std::process::Output> {
126        self.0.output().await
127    }
128
129    pub async fn status(&mut self) -> std::io::Result<std::process::ExitStatus> {
130        self.0.status().await
131    }
132}