2021-05-17 20:44:12 -07:00
|
|
|
#[derive(Copy, Clone)]
|
2019-12-07 03:09:21 -08:00
|
|
|
pub(crate) struct Shebang<'line> {
|
|
|
|
pub(crate) interpreter: &'line str,
|
2021-09-16 06:44:40 -07:00
|
|
|
pub(crate) argument: Option<&'line str>,
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
impl<'line> Shebang<'line> {
|
|
|
|
pub(crate) fn new(line: &'line str) -> Option<Shebang<'line>> {
|
|
|
|
if !line.starts_with("#!") {
|
2017-11-16 23:30:08 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2019-12-07 03:09:21 -08:00
|
|
|
let mut pieces = line[2..]
|
2017-11-16 23:30:08 -08:00
|
|
|
.lines()
|
2020-03-13 21:20:41 -07:00
|
|
|
.next()
|
2017-11-16 23:30:08 -08:00
|
|
|
.unwrap_or("")
|
|
|
|
.trim()
|
|
|
|
.splitn(2, |c| c == ' ' || c == '\t');
|
|
|
|
|
|
|
|
let interpreter = pieces.next().unwrap_or("");
|
2018-12-08 14:29:41 -08:00
|
|
|
let argument = pieces.next();
|
2017-11-16 23:30:08 -08:00
|
|
|
|
2020-11-19 14:47:04 -08:00
|
|
|
if interpreter.is_empty() {
|
2017-11-16 23:30:08 -08:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-12-08 14:29:41 -08:00
|
|
|
Some(Shebang {
|
|
|
|
interpreter,
|
|
|
|
argument,
|
|
|
|
})
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2021-05-17 20:44:12 -07:00
|
|
|
|
|
|
|
fn interpreter_filename(&self) -> &str {
|
|
|
|
self
|
|
|
|
.interpreter
|
2021-09-16 07:51:45 -07:00
|
|
|
.split(|c| matches!(c, '/' | '\\'))
|
|
|
|
.last()
|
2021-05-17 20:44:12 -07:00
|
|
|
.unwrap_or(self.interpreter)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn script_filename(&self, recipe: &str) -> String {
|
|
|
|
match self.interpreter_filename() {
|
|
|
|
"cmd" | "cmd.exe" => format!("{}.bat", recipe),
|
2021-11-16 20:24:29 -08:00
|
|
|
"powershell" | "powershell.exe" | "pwsh" | "pwsh.exe" => format!("{}.ps1", recipe),
|
2021-05-17 20:44:12 -07:00
|
|
|
_ => recipe.to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn include_shebang_line(&self) -> bool {
|
|
|
|
!matches!(self.interpreter_filename(), "cmd" | "cmd.exe")
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-11-07 10:55:15 -08:00
|
|
|
mod tests {
|
2017-11-16 23:30:08 -08:00
|
|
|
use super::Shebang;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn split_shebang() {
|
|
|
|
fn check(text: &str, expected_split: Option<(&str, Option<&str>)>) {
|
|
|
|
let shebang = Shebang::new(text);
|
2018-12-08 14:29:41 -08:00
|
|
|
assert_eq!(
|
|
|
|
shebang.map(|shebang| (shebang.interpreter, shebang.argument)),
|
|
|
|
expected_split
|
|
|
|
);
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
|
|
|
|
2018-12-08 14:29:41 -08:00
|
|
|
check("#! ", None);
|
|
|
|
check("#!", None);
|
|
|
|
check("#!/bin/bash", Some(("/bin/bash", None)));
|
|
|
|
check("#!/bin/bash ", Some(("/bin/bash", None)));
|
|
|
|
check(
|
|
|
|
"#!/usr/bin/env python",
|
|
|
|
Some(("/usr/bin/env", Some("python"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#!/usr/bin/env python ",
|
|
|
|
Some(("/usr/bin/env", Some("python"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#!/usr/bin/env python -x",
|
|
|
|
Some(("/usr/bin/env", Some("python -x"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#!/usr/bin/env python -x",
|
|
|
|
Some(("/usr/bin/env", Some("python -x"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#!/usr/bin/env python \t-x\t",
|
|
|
|
Some(("/usr/bin/env", Some("python \t-x"))),
|
|
|
|
);
|
|
|
|
check("#/usr/bin/env python \t-x\t", None);
|
|
|
|
check("#! /bin/bash", Some(("/bin/bash", None)));
|
|
|
|
check("#!\t\t/bin/bash ", Some(("/bin/bash", None)));
|
|
|
|
check(
|
|
|
|
"#! \t\t/usr/bin/env python",
|
|
|
|
Some(("/usr/bin/env", Some("python"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#! /usr/bin/env python ",
|
|
|
|
Some(("/usr/bin/env", Some("python"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#! /usr/bin/env python -x",
|
|
|
|
Some(("/usr/bin/env", Some("python -x"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#! /usr/bin/env python -x",
|
|
|
|
Some(("/usr/bin/env", Some("python -x"))),
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"#! /usr/bin/env python \t-x\t",
|
|
|
|
Some(("/usr/bin/env", Some("python \t-x"))),
|
|
|
|
);
|
|
|
|
check("# /usr/bin/env python \t-x\t", None);
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|
2021-05-17 20:44:12 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interpreter_filename_with_forward_slash() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!/foo/bar/baz")
|
|
|
|
.unwrap()
|
|
|
|
.interpreter_filename(),
|
|
|
|
"baz"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn interpreter_filename_with_backslash() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!\\foo\\bar\\baz")
|
|
|
|
.unwrap()
|
|
|
|
.interpreter_filename(),
|
|
|
|
"baz"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn powershell_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!powershell").unwrap().script_filename("foo"),
|
|
|
|
"foo.ps1"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:24:29 -08:00
|
|
|
#[test]
|
|
|
|
fn pwsh_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!pwsh").unwrap().script_filename("foo"),
|
|
|
|
"foo.ps1"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-17 20:44:12 -07:00
|
|
|
#[test]
|
|
|
|
fn powershell_exe_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!powershell.exe")
|
|
|
|
.unwrap()
|
|
|
|
.script_filename("foo"),
|
|
|
|
"foo.ps1"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-16 20:24:29 -08:00
|
|
|
#[test]
|
|
|
|
fn pwsh_exe_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!pwsh.exe").unwrap().script_filename("foo"),
|
|
|
|
"foo.ps1"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-17 20:44:12 -07:00
|
|
|
#[test]
|
|
|
|
fn cmd_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!cmd").unwrap().script_filename("foo"),
|
|
|
|
"foo.bat"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn cmd_exe_script_filename() {
|
|
|
|
assert_eq!(
|
|
|
|
Shebang::new("#!cmd.exe").unwrap().script_filename("foo"),
|
|
|
|
"foo.bat"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn plain_script_filename() {
|
|
|
|
assert_eq!(Shebang::new("#!bar").unwrap().script_filename("foo"), "foo");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_include_shebang_line_cmd() {
|
|
|
|
assert!(!Shebang::new("#!cmd").unwrap().include_shebang_line());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_include_shebang_line_cmd_exe() {
|
|
|
|
assert!(!Shebang::new("#!cmd.exe /C").unwrap().include_shebang_line());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn include_shebang_line_other() {
|
|
|
|
assert!(Shebang::new("#!foo -c").unwrap().include_shebang_line());
|
|
|
|
}
|
2017-11-16 23:30:08 -08:00
|
|
|
}
|