Skip to content

Commit

Permalink
Mutate state where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Oct 7, 2024
1 parent dd56bd3 commit b5c512b
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 114 deletions.
15 changes: 11 additions & 4 deletions crates/deno_task_shell/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1505,8 +1505,11 @@ fn parse_variable_expansion(part: Pair<Rule>) -> Result<WordPart> {
match modifier.as_rule() {
Rule::VAR_SUBSTRING => {
let mut numbers = modifier.into_inner();
let begin = numbers.next().and_then(|n| n.as_str().parse::<i64>().ok()).unwrap_or(0);

let begin = numbers
.next()
.and_then(|n| n.as_str().parse::<i64>().ok())
.unwrap_or(0);

let length = if let Some(len_word) = numbers.next() {
Some(len_word.as_str().parse::<i64>().into_diagnostic()?)
} else {
Expand All @@ -1524,11 +1527,15 @@ fn parse_variable_expansion(part: Pair<Rule>) -> Result<WordPart> {
}
Rule::VAR_ASSIGN_DEFAULT => {
let value = modifier.into_inner().next().unwrap();
Some(Box::new(VariableModifier::AssignDefault(parse_word(value)?)))
Some(Box::new(VariableModifier::AssignDefault(parse_word(
value,
)?)))
}
Rule::VAR_ALTERNATE_VALUE => {
let value = modifier.into_inner().next().unwrap();
Some(Box::new(VariableModifier::AlternateValue(parse_word(value)?)))
Some(Box::new(VariableModifier::AlternateValue(parse_word(
value,
)?)))
}
_ => {
return Err(miette!(
Expand Down
11 changes: 6 additions & 5 deletions crates/deno_task_shell/src/shell/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub fn execute_unresolved_command_name(
mut context: ShellCommandContext,
) -> FutureExecuteResult {
async move {
let args = context.args.clone();
let command =
match resolve_command(&command_name, &context, &context.args).await {
match resolve_command(&command_name, &mut context, &args).await {
Ok(command_path) => command_path,
Err(ResolveCommandError::CommandPath(err)) => {
let _ = context.stderr.write_line(&format!("{}", err));
Expand Down Expand Up @@ -108,7 +109,7 @@ impl FailedShebangError {

async fn resolve_command<'a>(
command_name: &UnresolvedCommandName,
context: &ShellCommandContext,
context: &mut ShellCommandContext,
original_args: &'a Vec<String>,
) -> Result<ResolvedCommand<'a>, ResolveCommandError> {
let command_path = match resolve_command_path(
Expand Down Expand Up @@ -160,10 +161,10 @@ async fn resolve_command<'a>(

async fn parse_shebang_args(
text: &str,
context: &ShellCommandContext,
context: &mut ShellCommandContext,
) -> Result<Vec<String>> {
fn err_unsupported(text: &str) -> Result<Vec<String>> {
miette::bail!("unsupported shebang. Please report this as a bug (https://github.com/denoland/deno).\n\nShebang: {}", text)
miette::bail!("unsupported shebang. Please report this as a bug (https://github.com/prefix.dev/shell).\n\nShebang: {}", text)
}

let mut args = crate::parser::parse(text)?;
Expand Down Expand Up @@ -204,7 +205,7 @@ async fn parse_shebang_args(

let result = super::execute::evaluate_args(
cmd.args,
&context.state,
&mut context.state,
context.stdin.clone(),
context.stderr.clone(),
)
Expand Down
Loading

0 comments on commit b5c512b

Please sign in to comment.