From 006171d66951ab4ee9aa5b55ef9ccacdb7b1d3f8 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 27 Apr 2020 18:10:34 +1200 Subject: [PATCH] Fix per-item run_alias (#1669) * Fix per-item run_alias * Fix 1609 also --- crates/nu-cli/src/commands/command.rs | 4 -- crates/nu-cli/src/commands/each.rs | 5 +- crates/nu-cli/src/commands/run_alias.rs | 85 +++++++++++------------- crates/nu-cli/src/commands/skip_while.rs | 6 +- crates/nu-cli/src/commands/where_.rs | 11 ++- 5 files changed, 50 insertions(+), 61 deletions(-) diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index 5e0297ea8a..dde8457034 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -489,10 +489,6 @@ impl Command { scope: raw_args.call_info.scope.clone().set_it(x.clone()), } .evaluate(®istry); - // let call_info = raw_args - // .clone() - // .call_info - // .evaluate(®istry, &Scope::it_value(x.clone())); match call_info { Ok(call_info) => match command.run(&call_info, ®istry, &raw_args, x) { diff --git a/crates/nu-cli/src/commands/each.rs b/crates/nu-cli/src/commands/each.rs index cc07a6b4b7..70da9df932 100644 --- a/crates/nu-cli/src/commands/each.rs +++ b/crates/nu-cli/src/commands/each.rs @@ -6,7 +6,7 @@ use crate::prelude::*; use futures::stream::once; use nu_errors::ShellError; -use nu_protocol::{hir::Block, ReturnSuccess, Scope, Signature, SyntaxShape}; +use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape}; pub struct Each; @@ -47,6 +47,7 @@ fn each( raw_args: RawCommandArgs, ) -> Result { let block = each_args.block; + let scope = raw_args.call_info.scope.clone(); let registry = context.registry.clone(); let mut input_stream = context.input; let stream = async_stream! { @@ -59,7 +60,7 @@ fn each( &block, &mut context, input_stream, - &Scope::new(input_clone), + &scope.clone().set_it(input_clone), ).await; match result { diff --git a/crates/nu-cli/src/commands/run_alias.rs b/crates/nu-cli/src/commands/run_alias.rs index bb33a098d1..4f06502e48 100644 --- a/crates/nu-cli/src/commands/run_alias.rs +++ b/crates/nu-cli/src/commands/run_alias.rs @@ -4,7 +4,7 @@ use crate::prelude::*; use derive_new::new; use nu_errors::ShellError; -use nu_protocol::{hir::Block, ReturnSuccess, Scope, Signature, SyntaxShape}; +use nu_protocol::{hir::Block, ReturnSuccess, Signature, SyntaxShape}; #[derive(new, Clone)] pub struct AliasCommand { @@ -43,67 +43,58 @@ impl WholeStreamCommand for AliasCommand { let block = self.block.clone(); let alias_command = self.clone(); let mut context = Context::from_args(&args, ®istry); - let mut input = args.input; + let input = args.input; let stream = async_stream! { - while let Some(input) = input.next().await { - let call_info = call_info.clone(); - let tag = tag.clone(); - let evaluated = call_info.evaluate_with_new_it(®istry, &input)?; - let mut scope = Scope::it_value(input.clone()); - if let Some(positional) = &evaluated.args.positional { - for (pos, arg) in positional.iter().enumerate() { - scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone()); - } + let mut scope = call_info.scope.clone(); + let evaluated = call_info.evaluate(®istry)?; + if let Some(positional) = &evaluated.args.positional { + for (pos, arg) in positional.iter().enumerate() { + scope = scope.set_var(alias_command.args[pos].to_string(), arg.clone()); } + } - let input_clone = Ok(input.clone()); - let input_stream = futures::stream::once(async { input_clone }).boxed().to_input_stream(); + let result = run_block( + &block, + &mut context, + input, + &scope, + ).await; - let result = run_block( - &block, - &mut context, - input_stream, - &scope - ).await; - - match result { - Ok(stream) if stream.is_empty() => { - yield Err(ShellError::labeled_error( - "Expected a block", - "alias needs a block", - tag, - )); + match result { + Ok(stream) if stream.is_empty() => { + yield Err(ShellError::labeled_error( + "Expected a block", + "alias needs a block", + tag, + )); + } + Ok(mut stream) => { + // We collect first to ensure errors are put into the context + while let Some(result) = stream.next().await { + yield Ok(ReturnSuccess::Value(result)); } - Ok(mut stream) => { - // We collect first to ensure errors are put into the context - while let Some(result) = stream.next().await { - yield Ok(ReturnSuccess::Value(result)); - } - let errors = context.get_errors(); - if let Some(x) = errors.first() { - //yield Err(error.clone()); - yield Err(ShellError::labeled_error_with_secondary( - "Alias failed to run", - "alias failed to run", - tag.clone(), - x.to_string(), - tag - )); - } - } - Err(e) => { - //yield Err(e); + let errors = context.get_errors(); + if let Some(x) = errors.first() { yield Err(ShellError::labeled_error_with_secondary( "Alias failed to run", "alias failed to run", tag.clone(), - e.to_string(), + x.to_string(), tag )); } } + Err(e) => { + yield Err(ShellError::labeled_error_with_secondary( + "Alias failed to run", + "alias failed to run", + tag.clone(), + e.to_string(), + tag + )); + } } }; diff --git a/crates/nu-cli/src/commands/skip_while.rs b/crates/nu-cli/src/commands/skip_while.rs index 187351e9af..d5585cd0ca 100644 --- a/crates/nu-cli/src/commands/skip_while.rs +++ b/crates/nu-cli/src/commands/skip_while.rs @@ -3,7 +3,7 @@ use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use log::trace; use nu_errors::ShellError; -use nu_protocol::{hir::ClassifiedCommand, Scope, Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; pub struct SkipWhile; @@ -32,6 +32,7 @@ impl WholeStreamCommand for SkipWhile { registry: &CommandRegistry, ) -> Result { let registry = registry.clone(); + let scope = args.call_info.scope.clone(); let call_info = args.evaluate_once(®istry)?; let block = call_info.args.expect_nth(0)?.clone(); @@ -80,7 +81,8 @@ impl WholeStreamCommand for SkipWhile { let objects = call_info.input.skip_while(move |item| { let condition = condition.clone(); trace!("ITEM = {:?}", item); - let result = evaluate_baseline_expr(&*condition, ®istry, &Scope::new(item.clone())); + let result = + evaluate_baseline_expr(&*condition, ®istry, &scope.clone().set_it(item.clone())); trace!("RESULT = {:?}", result); let return_value = match result { diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 9a87b3d5fa..079510e75f 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -3,9 +3,7 @@ use crate::context::CommandRegistry; use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{ - hir::Block, hir::ClassifiedCommand, ReturnSuccess, Scope, Signature, SyntaxShape, -}; +use nu_protocol::{hir::Block, hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape}; pub struct Where; @@ -36,7 +34,7 @@ impl WholeStreamCommand for Where { args: CommandArgs, registry: &CommandRegistry, ) -> Result { - args.process(registry, where_command)?.run() + Ok(args.process_raw(registry, where_command)?.run()) } } fn where_command( @@ -47,6 +45,7 @@ fn where_command( input, .. }: RunnableContext, + raw_args: RawCommandArgs, ) -> Result { let condition = { if block.block.len() != 1 { @@ -78,12 +77,12 @@ fn where_command( }; let mut input = input; - + let scope = raw_args.call_info.scope; let stream = async_stream! { while let Some(input) = input.next().await { //FIXME: should we use the scope that's brought in as well? - let condition = evaluate_baseline_expr(&condition, ®istry, &Scope::new(input.clone()))?; + let condition = evaluate_baseline_expr(&condition, ®istry, &scope.clone().set_it(input.clone()))?; match condition.as_bool() { Ok(b) => {