mirror of
https://github.com/nushell/nushell.git
synced 2025-07-15 22:05:51 +02:00
# Description When implementing a `Command`, one must also import all the types present in the function signatures for `Command`. This makes it so that we often import the same set of types in each command implementation file. E.g., something like this: ```rust use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Signature, Span, Type, Value, }; ``` This PR adds the `nu_engine::command_prelude` module which contains the necessary and commonly used types to implement a `Command`: ```rust // command_prelude.rs pub use crate::CallExt; pub use nu_protocol::{ ast::{Call, CellPath}, engine::{Command, EngineState, Stack}, record, Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, IntoSpanned, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape, Type, Value, }; ``` This should reduce the boilerplate needed to implement a command and also gives us a place to track the breadth of the `Command` API. I tried to be conservative with what went into the prelude modules, since it might be hard/annoying to remove items from the prelude in the future. Let me know if something should be included or excluded.
77 lines
2.4 KiB
Rust
77 lines
2.4 KiB
Rust
use log::info;
|
|
use miette::Result;
|
|
use nu_engine::{convert_env_values, eval_block};
|
|
use nu_parser::parse;
|
|
use nu_protocol::{
|
|
debugger::WithoutDebug,
|
|
engine::{EngineState, Stack, StateWorkingSet},
|
|
report_error, PipelineData, Spanned, Value,
|
|
};
|
|
|
|
/// Run a command (or commands) given to us by the user
|
|
pub fn evaluate_commands(
|
|
commands: &Spanned<String>,
|
|
engine_state: &mut EngineState,
|
|
stack: &mut Stack,
|
|
input: PipelineData,
|
|
table_mode: Option<Value>,
|
|
) -> Result<Option<i64>> {
|
|
// Translate environment variables from Strings to Values
|
|
if let Some(e) = convert_env_values(engine_state, stack) {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &e);
|
|
std::process::exit(1);
|
|
}
|
|
|
|
// Parse the source code
|
|
let (block, delta) = {
|
|
if let Some(ref t_mode) = table_mode {
|
|
let mut config = engine_state.get_config().clone();
|
|
config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default();
|
|
engine_state.set_config(config);
|
|
}
|
|
|
|
let mut working_set = StateWorkingSet::new(engine_state);
|
|
|
|
let output = parse(&mut working_set, None, commands.item.as_bytes(), false);
|
|
if let Some(warning) = working_set.parse_warnings.first() {
|
|
report_error(&working_set, warning);
|
|
}
|
|
|
|
if let Some(err) = working_set.parse_errors.first() {
|
|
report_error(&working_set, err);
|
|
|
|
std::process::exit(1);
|
|
}
|
|
|
|
(output, working_set.render())
|
|
};
|
|
|
|
// Update permanent state
|
|
if let Err(err) = engine_state.merge_delta(delta) {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
report_error(&working_set, &err);
|
|
}
|
|
|
|
// Run the block
|
|
let exit_code = match eval_block::<WithoutDebug>(engine_state, stack, &block, input) {
|
|
Ok(pipeline_data) => {
|
|
let mut config = engine_state.get_config().clone();
|
|
if let Some(t_mode) = table_mode {
|
|
config.table_mode = t_mode.coerce_str()?.parse().unwrap_or_default();
|
|
}
|
|
crate::eval_file::print_table_or_error(engine_state, stack, pipeline_data, &mut config)
|
|
}
|
|
Err(err) => {
|
|
let working_set = StateWorkingSet::new(engine_state);
|
|
|
|
report_error(&working_set, &err);
|
|
std::process::exit(1);
|
|
}
|
|
};
|
|
|
|
info!("evaluate {}:{}:{}", file!(), line!(), column!());
|
|
|
|
Ok(exit_code)
|
|
}
|