mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
This reverts commit 477f3be8df
.
This commit is contained in:
parent
477f3be8df
commit
dc0c5a9772
2 changed files with 14 additions and 70 deletions
|
@ -37,88 +37,50 @@ impl Command for If {
|
||||||
fn run(
|
fn run(
|
||||||
&self,
|
&self,
|
||||||
engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
caller_stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
let cond = &call.positional[0];
|
let cond = &call.positional[0];
|
||||||
let then_block: CaptureBlock = call.req(engine_state, caller_stack, 1)?;
|
let then_block: CaptureBlock = call.req(engine_state, stack, 1)?;
|
||||||
let else_case = call.positional.get(2);
|
let else_case = call.positional.get(2);
|
||||||
|
|
||||||
let result = eval_expression(engine_state, caller_stack, cond)?;
|
let result = eval_expression(engine_state, stack, cond)?;
|
||||||
match &result {
|
match &result {
|
||||||
Value::Bool { val, .. } => {
|
Value::Bool { val, .. } => {
|
||||||
if *val {
|
if *val {
|
||||||
let block = engine_state.get_block(then_block.block_id);
|
let block = engine_state.get_block(then_block.block_id);
|
||||||
let mut callee_stack = caller_stack.captures_to_stack(&then_block.captures);
|
let mut stack = stack.captures_to_stack(&then_block.captures);
|
||||||
let result = eval_block(
|
eval_block(
|
||||||
engine_state,
|
engine_state,
|
||||||
&mut callee_stack,
|
&mut stack,
|
||||||
block,
|
block,
|
||||||
input,
|
input,
|
||||||
call.redirect_stdout,
|
call.redirect_stdout,
|
||||||
call.redirect_stderr,
|
call.redirect_stderr,
|
||||||
);
|
)
|
||||||
let caller_env_vars = caller_stack.get_env_var_names(engine_state);
|
|
||||||
|
|
||||||
// remove env vars that are present in the caller but not in the callee
|
|
||||||
// (the callee hid them)
|
|
||||||
for var in caller_env_vars.iter() {
|
|
||||||
if !callee_stack.has_env_var(engine_state, var) {
|
|
||||||
caller_stack.remove_env_var(engine_state, var);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add new env vars from callee to caller
|
|
||||||
for env_vars in callee_stack.env_vars {
|
|
||||||
for (var, value) in env_vars {
|
|
||||||
caller_stack.add_env_var(var, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
} else if let Some(else_case) = else_case {
|
} else if let Some(else_case) = else_case {
|
||||||
if let Some(else_expr) = else_case.as_keyword() {
|
if let Some(else_expr) = else_case.as_keyword() {
|
||||||
if let Some(block_id) = else_expr.as_block() {
|
if let Some(block_id) = else_expr.as_block() {
|
||||||
let result = eval_expression(engine_state, caller_stack, else_expr)?;
|
let result = eval_expression(engine_state, stack, else_expr)?;
|
||||||
let else_block: CaptureBlock = FromValue::from_value(&result)?;
|
let else_block: CaptureBlock = FromValue::from_value(&result)?;
|
||||||
|
|
||||||
let mut callee_stack =
|
let mut stack = stack.captures_to_stack(&else_block.captures);
|
||||||
caller_stack.captures_to_stack(&else_block.captures);
|
|
||||||
let block = engine_state.get_block(block_id);
|
let block = engine_state.get_block(block_id);
|
||||||
let result = eval_block(
|
eval_block(
|
||||||
engine_state,
|
engine_state,
|
||||||
&mut callee_stack,
|
&mut stack,
|
||||||
block,
|
block,
|
||||||
input,
|
input,
|
||||||
call.redirect_stdout,
|
call.redirect_stdout,
|
||||||
call.redirect_stderr,
|
call.redirect_stderr,
|
||||||
);
|
)
|
||||||
|
|
||||||
let caller_env_vars = caller_stack.get_env_var_names(engine_state);
|
|
||||||
|
|
||||||
// remove env vars that are present in the caller but not in the callee
|
|
||||||
// (the callee hid them)
|
|
||||||
for var in caller_env_vars.iter() {
|
|
||||||
if !callee_stack.has_env_var(engine_state, var) {
|
|
||||||
caller_stack.remove_env_var(engine_state, var);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// add new env vars from callee to caller
|
|
||||||
for env_vars in callee_stack.env_vars {
|
|
||||||
for (var, value) in env_vars {
|
|
||||||
caller_stack.add_env_var(var, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
} else {
|
} else {
|
||||||
eval_expression(engine_state, caller_stack, else_expr)
|
eval_expression(engine_state, stack, else_expr)
|
||||||
.map(|x| x.into_pipeline_data())
|
.map(|x| x.into_pipeline_data())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
eval_expression(engine_state, caller_stack, else_case)
|
eval_expression(engine_state, stack, else_case)
|
||||||
.map(|x| x.into_pipeline_data())
|
.map(|x| x.into_pipeline_data())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -150,11 +112,6 @@ impl Command for If {
|
||||||
example: "if 5 < 3 { 'yes!' } else if 4 < 5 { 'no!' } else { 'okay!' }",
|
example: "if 5 < 3 { 'yes!' } else if 4 < 5 { 'no!' } else { 'okay!' }",
|
||||||
result: Some(Value::test_string("no!")),
|
result: Some(Value::test_string("no!")),
|
||||||
},
|
},
|
||||||
Example {
|
|
||||||
description: "Update the environment based on a condition",
|
|
||||||
example: r#"if true { let-env ENV_VAR = "new value" }"#,
|
|
||||||
result: None,
|
|
||||||
},
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -350,16 +350,3 @@ fn default_value11() -> TestResult {
|
||||||
fn default_value12() -> TestResult {
|
fn default_value12() -> TestResult {
|
||||||
fail_test(r#"def foo [--x:int = "a"] { $x }"#, "default value not int")
|
fail_test(r#"def foo [--x:int = "a"] { $x }"#, "default value not int")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn def_env_if() -> TestResult {
|
|
||||||
run_test(r#"if true { let-env FOO = "QUX" }; $env.FOO"#, "QUX")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn def_env_if2() -> TestResult {
|
|
||||||
run_test(
|
|
||||||
r#"if false { let-env FOO = "QUX" } else { let-env FOO = "QUUX" }; $env.FOO"#,
|
|
||||||
"QUUX",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue