From f77fdf87e206814854b476b01018fa7385fa1047 Mon Sep 17 00:00:00 2001 From: AsynchronousMatrix Date: Wed, 19 Jul 2023 20:20:05 +0100 Subject: [PATCH] re-implement the 'strip_shebang' function --- packages/cli/src/cli.rs | 4 ++-- packages/cli/src/utils/files.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/cli.rs b/packages/cli/src/cli.rs index 8639995..9d26e06 100644 --- a/packages/cli/src/cli.rs +++ b/packages/cli/src/cli.rs @@ -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) => { diff --git a/packages/cli/src/utils/files.rs b/packages/cli/src/utils/files.rs index f8614af..5be816a 100644 --- a/packages/cli/src/utils/files.rs +++ b/packages/cli/src/utils/files.rs @@ -185,3 +185,21 @@ pub fn parse_lune_description_from_file(contents: &str) -> Option { Some(unindented_lines) } } + +pub fn strip_shebang(mut contents: Vec) -> Vec { + 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 +}