Getting closer to multiline scripts (#2738)

* Begin allowing comments and multiline scripts.

* clippy

* Finish moving to groups. Test pass
This commit is contained in:
Jonathan Turner
2020-11-10 16:52:42 +13:00
committed by GitHub
parent 3924e9d50a
commit e66bf70589
17 changed files with 262 additions and 160 deletions

View File

@ -290,8 +290,8 @@ pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Bo
Ok(context)
}
pub async fn run_vec_of_pipelines(
pipelines: Vec<String>,
pub async fn run_script_file(
file_contents: String,
redirect_stdin: bool,
) -> Result<(), Box<dyn Error>> {
let mut syncer = EnvironmentSyncer::new();
@ -313,9 +313,7 @@ pub async fn run_vec_of_pipelines(
let _ = run_startup_commands(&mut context, &config).await;
for pipeline in pipelines {
run_pipeline_standalone(pipeline, redirect_stdin, &mut context, true).await?;
}
run_script_standalone(file_contents, redirect_stdin, &mut context, true).await?;
Ok(())
}
@ -475,7 +473,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
}
let line = match convert_rustyline_result_to_string(readline) {
LineResult::Success(s) => process_line(&s, &mut context, false, true).await,
LineResult::Success(s) => process_script(&s, &mut context, false, true).await,
x => x,
};
@ -602,8 +600,7 @@ async fn run_startup_commands(
} => {
for pipeline in pipelines {
if let Ok(pipeline_string) = pipeline.as_string() {
let _ =
run_pipeline_standalone(pipeline_string, false, context, false).await;
let _ = run_script_standalone(pipeline_string, false, context, false).await;
}
}
}
@ -618,13 +615,13 @@ async fn run_startup_commands(
Ok(())
}
pub async fn run_pipeline_standalone(
pipeline: String,
pub async fn run_script_standalone(
script_text: String,
redirect_stdin: bool,
context: &mut EvaluationContext,
exit_on_error: bool,
) -> Result<(), Box<dyn Error>> {
let line = process_line(&pipeline, context, redirect_stdin, false).await;
let line = process_script(&script_text, context, redirect_stdin, false).await;
match line {
LineResult::Success(line) => {
@ -892,16 +889,16 @@ pub async fn parse_and_eval(line: &str, ctx: &mut EvaluationContext) -> Result<S
}
/// Process the line by parsing the text to turn it into commands, classify those commands so that we understand what is being called in the pipeline, and then run this pipeline
pub async fn process_line(
line: &str,
pub async fn process_script(
script_text: &str,
ctx: &mut EvaluationContext,
redirect_stdin: bool,
cli_mode: bool,
) -> LineResult {
if line.trim() == "" {
LineResult::Success(line.to_string())
if script_text.trim() == "" {
LineResult::Success(script_text.to_string())
} else {
let line = chomp_newline(line);
let line = chomp_newline(script_text);
ctx.raw_input = line.to_string();
let (result, err) = nu_parser::lite_parse(&line, 0);
@ -930,11 +927,12 @@ pub async fn process_line(
// ...then change to this directory
if cli_mode
&& classified_block.block.block.len() == 1
&& classified_block.block.block[0].list.len() == 1
&& classified_block.block.block[0].pipelines.len() == 1
&& classified_block.block.block[0].pipelines[0].list.len() == 1
{
if let ClassifiedCommand::Internal(InternalCommand {
ref name, ref args, ..
}) = classified_block.block.block[0].list[0]
}) = classified_block.block.block[0].pipelines[0].list[0]
{
let internal_name = name;
let name = args