shell.rs

  1use std::{borrow::Cow, fmt, path::Path, sync::LazyLock};
  2
  3#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
  4pub enum ShellKind {
  5    #[default]
  6    Posix,
  7    Csh,
  8    Tcsh,
  9    Rc,
 10    Fish,
 11    PowerShell,
 12    Nushell,
 13    Cmd,
 14    Xonsh,
 15}
 16
 17pub fn get_system_shell() -> String {
 18    if cfg!(windows) {
 19        get_windows_system_shell()
 20    } else {
 21        std::env::var("SHELL").unwrap_or("/bin/sh".to_string())
 22    }
 23}
 24
 25pub fn get_default_system_shell() -> String {
 26    if cfg!(windows) {
 27        get_windows_system_shell()
 28    } else {
 29        "/bin/sh".to_string()
 30    }
 31}
 32
 33/// Get the default system shell, preferring git-bash on Windows.
 34pub fn get_default_system_shell_preferring_bash() -> String {
 35    if cfg!(windows) {
 36        get_windows_git_bash().unwrap_or_else(|| get_windows_system_shell())
 37    } else {
 38        "/bin/sh".to_string()
 39    }
 40}
 41
 42pub fn get_windows_git_bash() -> Option<String> {
 43    static GIT_BASH: LazyLock<Option<String>> = LazyLock::new(|| {
 44        // /path/to/git/cmd/git.exe/../../bin/bash.exe
 45        let git = which::which("git").ok()?;
 46        let git_bash = git.parent()?.parent()?.join("bin").join("bash.exe");
 47        if git_bash.is_file() {
 48            Some(git_bash.to_string_lossy().to_string())
 49        } else {
 50            None
 51        }
 52    });
 53
 54    (*GIT_BASH).clone()
 55}
 56
 57pub fn get_windows_system_shell() -> String {
 58    use std::path::PathBuf;
 59
 60    fn find_pwsh_in_programfiles(find_alternate: bool, find_preview: bool) -> Option<PathBuf> {
 61        #[cfg(target_pointer_width = "64")]
 62        let env_var = if find_alternate {
 63            "ProgramFiles(x86)"
 64        } else {
 65            "ProgramFiles"
 66        };
 67
 68        #[cfg(target_pointer_width = "32")]
 69        let env_var = if find_alternate {
 70            "ProgramW6432"
 71        } else {
 72            "ProgramFiles"
 73        };
 74
 75        let install_base_dir = PathBuf::from(std::env::var_os(env_var)?).join("PowerShell");
 76        install_base_dir
 77            .read_dir()
 78            .ok()?
 79            .filter_map(Result::ok)
 80            .filter(|entry| matches!(entry.file_type(), Ok(ft) if ft.is_dir()))
 81            .filter_map(|entry| {
 82                let dir_name = entry.file_name();
 83                let dir_name = dir_name.to_string_lossy();
 84
 85                let version = if find_preview {
 86                    let dash_index = dir_name.find('-')?;
 87                    if &dir_name[dash_index + 1..] != "preview" {
 88                        return None;
 89                    };
 90                    dir_name[..dash_index].parse::<u32>().ok()?
 91                } else {
 92                    dir_name.parse::<u32>().ok()?
 93                };
 94
 95                let exe_path = entry.path().join("pwsh.exe");
 96                if exe_path.exists() {
 97                    Some((version, exe_path))
 98                } else {
 99                    None
100                }
101            })
102            .max_by_key(|(version, _)| *version)
103            .map(|(_, path)| path)
104    }
105
106    fn find_pwsh_in_msix(find_preview: bool) -> Option<PathBuf> {
107        let msix_app_dir =
108            PathBuf::from(std::env::var_os("LOCALAPPDATA")?).join("Microsoft\\WindowsApps");
109        if !msix_app_dir.exists() {
110            return None;
111        }
112
113        let prefix = if find_preview {
114            "Microsoft.PowerShellPreview_"
115        } else {
116            "Microsoft.PowerShell_"
117        };
118        msix_app_dir
119            .read_dir()
120            .ok()?
121            .filter_map(|entry| {
122                let entry = entry.ok()?;
123                if !matches!(entry.file_type(), Ok(ft) if ft.is_dir()) {
124                    return None;
125                }
126
127                if !entry.file_name().to_string_lossy().starts_with(prefix) {
128                    return None;
129                }
130
131                let exe_path = entry.path().join("pwsh.exe");
132                exe_path.exists().then_some(exe_path)
133            })
134            .next()
135    }
136
137    fn find_pwsh_in_scoop() -> Option<PathBuf> {
138        let pwsh_exe =
139            PathBuf::from(std::env::var_os("USERPROFILE")?).join("scoop\\shims\\pwsh.exe");
140        pwsh_exe.exists().then_some(pwsh_exe)
141    }
142
143    static SYSTEM_SHELL: LazyLock<String> = LazyLock::new(|| {
144        find_pwsh_in_programfiles(false, false)
145            .or_else(|| find_pwsh_in_programfiles(true, false))
146            .or_else(|| find_pwsh_in_msix(false))
147            .or_else(|| find_pwsh_in_programfiles(false, true))
148            .or_else(|| find_pwsh_in_msix(true))
149            .or_else(|| find_pwsh_in_programfiles(true, true))
150            .or_else(find_pwsh_in_scoop)
151            .map(|p| p.to_string_lossy().into_owned())
152            .unwrap_or("powershell.exe".to_string())
153    });
154
155    (*SYSTEM_SHELL).clone()
156}
157
158impl fmt::Display for ShellKind {
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        match self {
161            ShellKind::Posix => write!(f, "sh"),
162            ShellKind::Csh => write!(f, "csh"),
163            ShellKind::Tcsh => write!(f, "tcsh"),
164            ShellKind::Fish => write!(f, "fish"),
165            ShellKind::PowerShell => write!(f, "powershell"),
166            ShellKind::Nushell => write!(f, "nu"),
167            ShellKind::Cmd => write!(f, "cmd"),
168            ShellKind::Rc => write!(f, "rc"),
169            ShellKind::Xonsh => write!(f, "xonsh"),
170        }
171    }
172}
173
174impl ShellKind {
175    pub fn system() -> Self {
176        Self::new(&get_system_shell())
177    }
178
179    pub fn new(program: impl AsRef<Path>) -> Self {
180        let program = program.as_ref();
181        let Some(program) = program.file_stem().and_then(|s| s.to_str()) else {
182            return if cfg!(windows) {
183                ShellKind::PowerShell
184            } else {
185                ShellKind::Posix
186            };
187        };
188        if program == "powershell" || program == "pwsh" {
189            ShellKind::PowerShell
190        } else if program == "cmd" {
191            ShellKind::Cmd
192        } else if program == "nu" {
193            ShellKind::Nushell
194        } else if program == "fish" {
195            ShellKind::Fish
196        } else if program == "csh" {
197            ShellKind::Csh
198        } else if program == "tcsh" {
199            ShellKind::Tcsh
200        } else if program == "rc" {
201            ShellKind::Rc
202        } else if program == "xonsh" {
203            ShellKind::Xonsh
204        } else if program == "sh" || program == "bash" {
205            ShellKind::Posix
206        } else {
207            if cfg!(windows) {
208                ShellKind::PowerShell
209            } else {
210                // Some other shell detected, the user might install and use a
211                // unix-like shell.
212                ShellKind::Posix
213            }
214        }
215    }
216
217    pub fn to_shell_variable(self, input: &str) -> String {
218        match self {
219            Self::PowerShell => Self::to_powershell_variable(input),
220            Self::Cmd => Self::to_cmd_variable(input),
221            Self::Posix => input.to_owned(),
222            Self::Fish => input.to_owned(),
223            Self::Csh => input.to_owned(),
224            Self::Tcsh => input.to_owned(),
225            Self::Rc => input.to_owned(),
226            Self::Nushell => Self::to_nushell_variable(input),
227            Self::Xonsh => input.to_owned(),
228        }
229    }
230
231    fn to_cmd_variable(input: &str) -> String {
232        if let Some(var_str) = input.strip_prefix("${") {
233            if var_str.find(':').is_none() {
234                // If the input starts with "${", remove the trailing "}"
235                format!("%{}%", &var_str[..var_str.len() - 1])
236            } else {
237                // `${SOME_VAR:-SOME_DEFAULT}`, we currently do not handle this situation,
238                // which will result in the task failing to run in such cases.
239                input.into()
240            }
241        } else if let Some(var_str) = input.strip_prefix('$') {
242            // If the input starts with "$", directly append to "$env:"
243            format!("%{}%", var_str)
244        } else {
245            // If no prefix is found, return the input as is
246            input.into()
247        }
248    }
249
250    fn to_powershell_variable(input: &str) -> String {
251        if let Some(var_str) = input.strip_prefix("${") {
252            if var_str.find(':').is_none() {
253                // If the input starts with "${", remove the trailing "}"
254                format!("$env:{}", &var_str[..var_str.len() - 1])
255            } else {
256                // `${SOME_VAR:-SOME_DEFAULT}`, we currently do not handle this situation,
257                // which will result in the task failing to run in such cases.
258                input.into()
259            }
260        } else if let Some(var_str) = input.strip_prefix('$') {
261            // If the input starts with "$", directly append to "$env:"
262            format!("$env:{}", var_str)
263        } else {
264            // If no prefix is found, return the input as is
265            input.into()
266        }
267    }
268
269    fn to_nushell_variable(input: &str) -> String {
270        let mut result = String::new();
271        let mut source = input;
272        let mut is_start = true;
273
274        loop {
275            match source.chars().next() {
276                None => return result,
277                Some('$') => {
278                    source = Self::parse_nushell_var(&source[1..], &mut result, is_start);
279                    is_start = false;
280                }
281                Some(_) => {
282                    is_start = false;
283                    let chunk_end = source.find('$').unwrap_or(source.len());
284                    let (chunk, rest) = source.split_at(chunk_end);
285                    result.push_str(chunk);
286                    source = rest;
287                }
288            }
289        }
290    }
291
292    fn parse_nushell_var<'a>(source: &'a str, text: &mut String, is_start: bool) -> &'a str {
293        if source.starts_with("env.") {
294            text.push('$');
295            return source;
296        }
297
298        match source.chars().next() {
299            Some('{') => {
300                let source = &source[1..];
301                if let Some(end) = source.find('}') {
302                    let var_name = &source[..end];
303                    if !var_name.is_empty() {
304                        if !is_start {
305                            text.push_str("(");
306                        }
307                        text.push_str("$env.");
308                        text.push_str(var_name);
309                        if !is_start {
310                            text.push_str(")");
311                        }
312                        &source[end + 1..]
313                    } else {
314                        text.push_str("${}");
315                        &source[end + 1..]
316                    }
317                } else {
318                    text.push_str("${");
319                    source
320                }
321            }
322            Some(c) if c.is_alphabetic() || c == '_' => {
323                let end = source
324                    .find(|c: char| !c.is_alphanumeric() && c != '_')
325                    .unwrap_or(source.len());
326                let var_name = &source[..end];
327                if !is_start {
328                    text.push_str("(");
329                }
330                text.push_str("$env.");
331                text.push_str(var_name);
332                if !is_start {
333                    text.push_str(")");
334                }
335                &source[end..]
336            }
337            _ => {
338                text.push('$');
339                source
340            }
341        }
342    }
343
344    pub fn args_for_shell(&self, interactive: bool, combined_command: String) -> Vec<String> {
345        match self {
346            ShellKind::PowerShell => vec!["-C".to_owned(), combined_command],
347            ShellKind::Cmd => vec!["/C".to_owned(), combined_command],
348            ShellKind::Posix
349            | ShellKind::Nushell
350            | ShellKind::Fish
351            | ShellKind::Csh
352            | ShellKind::Tcsh
353            | ShellKind::Rc
354            | ShellKind::Xonsh => interactive
355                .then(|| "-i".to_owned())
356                .into_iter()
357                .chain(["-c".to_owned(), combined_command])
358                .collect(),
359        }
360    }
361
362    pub const fn command_prefix(&self) -> Option<char> {
363        match self {
364            ShellKind::PowerShell => Some('&'),
365            ShellKind::Nushell => Some('^'),
366            _ => None,
367        }
368    }
369
370    pub const fn sequential_commands_separator(&self) -> char {
371        match self {
372            ShellKind::Cmd => '&',
373            _ => ';',
374        }
375    }
376
377    pub fn try_quote<'a>(&self, arg: &'a str) -> Option<Cow<'a, str>> {
378        shlex::try_quote(arg).ok().map(|arg| match self {
379            // If we are running in PowerShell, we want to take extra care when escaping strings.
380            // In particular, we want to escape strings with a backtick (`) rather than a backslash (\).
381            // TODO double escaping backslashes is not necessary in PowerShell and probably CMD
382            ShellKind::PowerShell => Cow::Owned(arg.replace("\\\"", "`\"")),
383            _ => arg,
384        })
385    }
386
387    pub const fn activate_keyword(&self) -> &'static str {
388        match self {
389            ShellKind::Cmd => "",
390            ShellKind::Nushell => "overlay use",
391            ShellKind::PowerShell => ".",
392            ShellKind::Fish => "source",
393            ShellKind::Csh => "source",
394            ShellKind::Tcsh => "source",
395            ShellKind::Posix | ShellKind::Rc => "source",
396            ShellKind::Xonsh => "source",
397        }
398    }
399
400    pub const fn clear_screen_command(&self) -> &'static str {
401        match self {
402            ShellKind::Cmd => "cls",
403            _ => "clear",
404        }
405    }
406}