re-implement the 'strip_shebang' function

This commit is contained in:
AsynchronousMatrix 2023-07-19 20:20:05 +01:00
parent 3e84e8336d
commit f77fdf87e2
2 changed files with 20 additions and 2 deletions

View file

@ -12,7 +12,7 @@ use tokio::{
use crate::{
setup::run_setup,
utils::{
files::discover_script_file_path_including_lune_dirs,
files::{discover_script_file_path_including_lune_dirs, strip_shebang},
listing::{find_lune_scripts, sort_lune_scripts, write_lune_scripts_list},
},
};
@ -172,7 +172,7 @@ impl Cli {
// Create a new lune object with all globals & run the script
let result = Lune::new()
.with_args(self.script_args)
.run(&script_display_name, script_contents)
.run(&script_display_name, strip_shebang(script_contents))
.await;
Ok(match result {
Err(err) => {

View file

@ -185,3 +185,21 @@ pub fn parse_lune_description_from_file(contents: &str) -> Option<String> {
Some(unindented_lines)
}
}
pub fn strip_shebang(mut contents: Vec<u8>) -> Vec<u8> {
if contents.starts_with(b"#!") {
if let Some(first_newline_idx) =
contents
.iter()
.enumerate()
.find_map(|(idx, c)| if *c == b'\n' { Some(idx) } else { None })
{
// NOTE: We keep the newline here on purpose to preserve
// correct line numbers in stack traces, the only reason
// we strip the shebang is to get the lua script to parse
// and the extra newline is not really a problem for that
contents.drain(..first_newline_idx);
}
}
contents
}