forked from extern/nushell
Fix main in scripts with captures (#4468)
* Fix main in scripts with captures * Remove old comments
This commit is contained in:
parent
63a2c2bc2d
commit
f3d3e819fb
@ -2,6 +2,7 @@ use crate::is_perf_true;
|
|||||||
use crate::utils::{eval_source, report_error};
|
use crate::utils::{eval_source, report_error};
|
||||||
use log::info;
|
use log::info;
|
||||||
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
|
use nu_protocol::engine::{EngineState, Stack, StateDelta, StateWorkingSet};
|
||||||
|
use nu_protocol::{PipelineData, Span};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const NUSHELL_FOLDER: &str = "nushell";
|
const NUSHELL_FOLDER: &str = "nushell";
|
||||||
@ -22,8 +23,14 @@ pub(crate) fn read_plugin_file(engine_state: &mut EngineState, stack: &mut Stack
|
|||||||
|
|
||||||
let plugin_filename = plugin_path.to_string_lossy().to_owned();
|
let plugin_filename = plugin_path.to_string_lossy().to_owned();
|
||||||
|
|
||||||
if let Ok(contents) = std::fs::read_to_string(&plugin_path) {
|
if let Ok(contents) = std::fs::read(&plugin_path) {
|
||||||
eval_source(engine_state, stack, &contents, &plugin_filename);
|
eval_source(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
&contents,
|
||||||
|
&plugin_filename,
|
||||||
|
PipelineData::new(Span::new(0, 0)),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if is_perf_true() {
|
if is_perf_true() {
|
||||||
@ -49,8 +56,14 @@ pub(crate) fn read_config_file(engine_state: &mut EngineState, stack: &mut Stack
|
|||||||
//println!("Loading config from: {:?}", config_path);
|
//println!("Loading config from: {:?}", config_path);
|
||||||
let config_filename = config_path.to_string_lossy().to_owned();
|
let config_filename = config_path.to_string_lossy().to_owned();
|
||||||
|
|
||||||
if let Ok(contents) = std::fs::read_to_string(&config_path) {
|
if let Ok(contents) = std::fs::read(&config_path) {
|
||||||
eval_source(engine_state, stack, &contents, &config_filename);
|
eval_source(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
&contents,
|
||||||
|
&config_filename,
|
||||||
|
PipelineData::new(Span::new(0, 0)),
|
||||||
|
);
|
||||||
// Merge the delta in case env vars changed in the config
|
// Merge the delta in case env vars changed in the config
|
||||||
match nu_engine::env::current_dir(engine_state, stack) {
|
match nu_engine::env::current_dir(engine_state, stack) {
|
||||||
Ok(cwd) => {
|
Ok(cwd) => {
|
||||||
|
111
src/eval_file.rs
111
src/eval_file.rs
@ -1,48 +1,27 @@
|
|||||||
use crate::is_perf_true;
|
use crate::is_perf_true;
|
||||||
use crate::utils::{gather_parent_env_vars, report_error};
|
use crate::utils::{eval_source, gather_parent_env_vars, report_error};
|
||||||
use log::info;
|
use log::info;
|
||||||
use log::trace;
|
use log::trace;
|
||||||
use miette::{IntoDiagnostic, Result};
|
use miette::{IntoDiagnostic, Result};
|
||||||
use nu_engine::{convert_env_values, eval_block};
|
use nu_engine::convert_env_values;
|
||||||
use nu_parser::parse;
|
use nu_parser::parse;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{EngineState, Stack, StateDelta, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
Config, PipelineData, Span, Value, CONFIG_VARIABLE_ID,
|
Config, PipelineData, Span, Value, CONFIG_VARIABLE_ID,
|
||||||
};
|
};
|
||||||
use std::{io::Write, path::PathBuf};
|
use std::io::Write;
|
||||||
|
|
||||||
/// Main function used when a file path is found as argument for nu
|
/// Main function used when a file path is found as argument for nu
|
||||||
pub(crate) fn evaluate(
|
pub(crate) fn evaluate(
|
||||||
path: String,
|
path: String,
|
||||||
args: &[String],
|
args: &[String],
|
||||||
init_cwd: PathBuf,
|
|
||||||
engine_state: &mut EngineState,
|
engine_state: &mut EngineState,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// First, set up env vars as strings only
|
// First, set up env vars as strings only
|
||||||
gather_parent_env_vars(engine_state);
|
gather_parent_env_vars(engine_state);
|
||||||
|
|
||||||
let file = std::fs::read(&path).into_diagnostic()?;
|
|
||||||
|
|
||||||
let (block, delta) = {
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
|
||||||
trace!("parsing file: {}", path);
|
|
||||||
|
|
||||||
let (output, err) = parse(&mut working_set, Some(&path), &file, false);
|
|
||||||
if let Some(err) = err {
|
|
||||||
report_error(&working_set, &err);
|
|
||||||
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
(output, working_set.render())
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Err(err) = engine_state.merge_delta(delta, None, &init_cwd) {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
report_error(&working_set, &err);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut stack = nu_protocol::engine::Stack::new();
|
let mut stack = nu_protocol::engine::Stack::new();
|
||||||
|
|
||||||
// Set up our initial config to start from
|
// Set up our initial config to start from
|
||||||
@ -65,20 +44,6 @@ pub(crate) fn evaluate(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Merge the delta in case env vars changed in the config
|
|
||||||
match nu_engine::env::current_dir(engine_state, &stack) {
|
|
||||||
Ok(cwd) => {
|
|
||||||
if let Err(e) = engine_state.merge_delta(StateDelta::new(), Some(&mut stack), cwd) {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
report_error(&working_set, &e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
report_error(&working_set, &e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Translate environment variables from Strings to Values
|
// Translate environment variables from Strings to Values
|
||||||
if let Some(e) = convert_env_values(engine_state, &stack, &config) {
|
if let Some(e) = convert_env_values(engine_state, &stack, &config) {
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
@ -86,54 +51,32 @@ pub(crate) fn evaluate(
|
|||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Next, let's check if there are any flags we want to pass to the main function
|
let file = std::fs::read(&path).into_diagnostic()?;
|
||||||
if args.is_empty() && engine_state.find_decl(b"main").is_none() {
|
|
||||||
// We don't have a main, so evaluate the whole file
|
|
||||||
match eval_block(engine_state, &mut stack, &block, input) {
|
|
||||||
Ok(pipeline_data) => {
|
|
||||||
print_table_or_error(engine_state, &mut stack, pipeline_data, &config)
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
|
|
||||||
report_error(&working_set, &err);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
|
trace!("parsing file: {}", path);
|
||||||
|
|
||||||
std::process::exit(1);
|
let _ = parse(&mut working_set, Some(&path), &file, false);
|
||||||
}
|
|
||||||
}
|
if working_set.find_decl(b"main").is_some() {
|
||||||
|
let args = format!("main {}", args.join(" "));
|
||||||
|
|
||||||
|
eval_source(
|
||||||
|
engine_state,
|
||||||
|
&mut stack,
|
||||||
|
&file,
|
||||||
|
&path,
|
||||||
|
PipelineData::new(Span::new(0, 0)),
|
||||||
|
);
|
||||||
|
eval_source(
|
||||||
|
engine_state,
|
||||||
|
&mut stack,
|
||||||
|
args.as_bytes(),
|
||||||
|
"<commandline>",
|
||||||
|
input,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
let args = format!("main {}", args.join(" ")).as_bytes().to_vec();
|
eval_source(engine_state, &mut stack, &file, &path, input);
|
||||||
|
|
||||||
let (block, delta) = {
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
|
||||||
let (output, err) = parse(&mut working_set, Some("<cmdline>"), &args, false);
|
|
||||||
if let Some(err) = err {
|
|
||||||
report_error(&working_set, &err);
|
|
||||||
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
(output, working_set.render())
|
|
||||||
};
|
|
||||||
|
|
||||||
let cwd = nu_engine::env::current_dir_str(engine_state, &stack)?;
|
|
||||||
|
|
||||||
if let Err(err) = engine_state.merge_delta(delta, Some(&mut stack), &cwd) {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
report_error(&working_set, &err);
|
|
||||||
}
|
|
||||||
|
|
||||||
match eval_block(engine_state, &mut stack, &block, input) {
|
|
||||||
Ok(pipeline_data) => {
|
|
||||||
print_table_or_error(engine_state, &mut stack, pipeline_data, &config)
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
|
||||||
|
|
||||||
report_error(&working_set, &err);
|
|
||||||
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if is_perf_true() {
|
if is_perf_true() {
|
||||||
|
@ -188,13 +188,8 @@ fn main() -> Result<()> {
|
|||||||
|
|
||||||
ret_val
|
ret_val
|
||||||
} else if !script_name.is_empty() && binary_args.interactive_shell.is_none() {
|
} else if !script_name.is_empty() && binary_args.interactive_shell.is_none() {
|
||||||
let ret_val = eval_file::evaluate(
|
let ret_val =
|
||||||
script_name,
|
eval_file::evaluate(script_name, &args_to_script, &mut engine_state, input);
|
||||||
&args_to_script,
|
|
||||||
init_cwd,
|
|
||||||
&mut engine_state,
|
|
||||||
input,
|
|
||||||
);
|
|
||||||
if is_perf_true() {
|
if is_perf_true() {
|
||||||
info!("eval_file execution {}:{}:{}", file!(), line!(), column!());
|
info!("eval_file execution {}:{}:{}", file!(), line!(), column!());
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ use nu_cli::{NuCompleter, NuHighlighter, NuValidator, NushellPrompt};
|
|||||||
use nu_color_config::get_color_config;
|
use nu_color_config::get_color_config;
|
||||||
use nu_engine::convert_env_values;
|
use nu_engine::convert_env_values;
|
||||||
use nu_parser::lex;
|
use nu_parser::lex;
|
||||||
|
use nu_protocol::PipelineData;
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
engine::{EngineState, StateWorkingSet},
|
engine::{EngineState, StateWorkingSet},
|
||||||
Config, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
Config, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
||||||
@ -300,8 +301,9 @@ pub(crate) fn evaluate(engine_state: &mut EngineState) -> Result<()> {
|
|||||||
eval_source(
|
eval_source(
|
||||||
engine_state,
|
engine_state,
|
||||||
&mut stack,
|
&mut stack,
|
||||||
&s,
|
s.as_bytes(),
|
||||||
&format!("entry #{}", entry_num),
|
&format!("entry #{}", entry_num),
|
||||||
|
PipelineData::new(Span::new(0, 0)),
|
||||||
);
|
);
|
||||||
|
|
||||||
stack.add_env_var(
|
stack.add_env_var(
|
||||||
|
12
src/utils.rs
12
src/utils.rs
@ -257,8 +257,9 @@ fn print_pipeline_data(
|
|||||||
pub(crate) fn eval_source(
|
pub(crate) fn eval_source(
|
||||||
engine_state: &mut EngineState,
|
engine_state: &mut EngineState,
|
||||||
stack: &mut Stack,
|
stack: &mut Stack,
|
||||||
source: &str,
|
source: &[u8],
|
||||||
fname: &str,
|
fname: &str,
|
||||||
|
input: PipelineData,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
trace!("eval_source");
|
trace!("eval_source");
|
||||||
|
|
||||||
@ -267,7 +268,7 @@ pub(crate) fn eval_source(
|
|||||||
let (output, err) = parse(
|
let (output, err) = parse(
|
||||||
&mut working_set,
|
&mut working_set,
|
||||||
Some(fname), // format!("entry #{}", entry_num)
|
Some(fname), // format!("entry #{}", entry_num)
|
||||||
source.as_bytes(),
|
source,
|
||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
if let Some(err) = err {
|
if let Some(err) = err {
|
||||||
@ -292,12 +293,7 @@ pub(crate) fn eval_source(
|
|||||||
report_error(&working_set, &err);
|
report_error(&working_set, &err);
|
||||||
}
|
}
|
||||||
|
|
||||||
match eval_block(
|
match eval_block(engine_state, stack, &block, input) {
|
||||||
engine_state,
|
|
||||||
stack,
|
|
||||||
&block,
|
|
||||||
PipelineData::new(Span::new(0, 0)),
|
|
||||||
) {
|
|
||||||
Ok(pipeline_data) => {
|
Ok(pipeline_data) => {
|
||||||
if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) {
|
if let Err(err) = print_pipeline_data(pipeline_data, engine_state, stack) {
|
||||||
let working_set = StateWorkingSet::new(engine_state);
|
let working_set = StateWorkingSet::new(engine_state);
|
||||||
|
Loading…
Reference in New Issue
Block a user