Add support for multiline script files (#1386)

* Add support for multiline script files

* clippy
This commit is contained in:
Jonathan Turner 2020-02-13 21:24:18 -08:00 committed by GitHub
parent 473e9f9422
commit 8ae8ebd107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 64 additions and 22 deletions

View File

@ -134,7 +134,7 @@ fn search_paths() -> Vec<std::path::PathBuf> {
search_paths
}
fn load_plugins(context: &mut Context) -> Result<(), ShellError> {
pub fn load_plugins(context: &mut Context) -> Result<(), ShellError> {
let opts = glob::MatchOptions {
case_sensitive: false,
require_literal_separator: false,
@ -376,24 +376,9 @@ pub fn create_default_context(
pub async fn run_pipeline_standalone(
pipeline: String,
redirect_stdin: bool,
context: &mut Context,
) -> Result<(), Box<dyn Error>> {
let mut syncer = crate::env::environment_syncer::EnvironmentSyncer::new();
let mut context = create_default_context(&mut syncer)?;
let _ = load_plugins(&mut context);
let cc = context.ctrl_c.clone();
ctrlc::set_handler(move || {
cc.store(true, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
if context.ctrl_c.load(Ordering::SeqCst) {
context.ctrl_c.store(false, Ordering::SeqCst);
}
let line = process_line(Ok(pipeline), &mut context, redirect_stdin).await;
let line = process_line(Ok(pipeline), context, redirect_stdin).await;
match line {
LineResult::Success(line) => {
@ -409,8 +394,10 @@ pub async fn run_pipeline_standalone(
};
context.maybe_print_errors(Text::from(line));
if error_code != 0 {
std::process::exit(error_code);
}
}
LineResult::Error(line, err) => {
context.with_host(|host| {

View File

@ -12,7 +12,7 @@ use std::error::Error;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct CommandRegistry {
registry: Arc<Mutex<IndexMap<String, Arc<Command>>>>,
}

View File

@ -30,7 +30,7 @@ impl Env for Box<dyn Env> {
}
}
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct Environment {
environment_vars: Option<Value>,
path_vars: Option<Value>,

View File

@ -9,6 +9,12 @@ pub struct EnvironmentSyncer {
pub config: Arc<Box<dyn Conf>>,
}
impl Default for EnvironmentSyncer {
fn default() -> Self {
Self::new()
}
}
impl EnvironmentSyncer {
pub fn new() -> EnvironmentSyncer {
EnvironmentSyncer {

View File

@ -20,10 +20,11 @@ mod shell;
mod stream;
mod utils;
pub use crate::cli::{cli, run_pipeline_standalone};
pub use crate::cli::{cli, create_default_context, load_plugins, run_pipeline_standalone};
pub use crate::data::dict::TaggedListBuilder;
pub use crate::data::primitive;
pub use crate::data::value;
pub use crate::env::environment_syncer::EnvironmentSyncer;
pub use crate::env::host::BasicHost;
pub use nu_parser::TokenTreeBuilder;
pub use nu_value_ext::ValueExt;

View File

@ -3,6 +3,7 @@ use log::LevelFilter;
use std::error::Error;
use std::fs::File;
use std::io::{prelude::*, BufReader};
use std::sync::atomic::Ordering;
fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("nushell")
@ -88,10 +89,26 @@ fn main() -> Result<(), Box<dyn Error>> {
match matches.values_of("commands") {
None => {}
Some(values) => {
let mut syncer = nu::EnvironmentSyncer::new();
let mut context = nu::create_default_context(&mut syncer)?;
let _ = nu::load_plugins(&mut context);
let cc = context.ctrl_c.clone();
ctrlc::set_handler(move || {
cc.store(true, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
if context.ctrl_c.load(Ordering::SeqCst) {
context.ctrl_c.store(false, Ordering::SeqCst);
}
for item in values {
futures::executor::block_on(nu::run_pipeline_standalone(
item.into(),
matches.is_present("stdin"),
&mut context,
))?;
}
return Ok(());
@ -102,12 +119,28 @@ fn main() -> Result<(), Box<dyn Error>> {
Some(script) => {
let file = File::open(script)?;
let reader = BufReader::new(file);
let mut syncer = nu::EnvironmentSyncer::new();
let mut context = nu::create_default_context(&mut syncer)?;
let _ = nu::load_plugins(&mut context);
let cc = context.ctrl_c.clone();
ctrlc::set_handler(move || {
cc.store(true, Ordering::SeqCst);
})
.expect("Error setting Ctrl-C handler");
if context.ctrl_c.load(Ordering::SeqCst) {
context.ctrl_c.store(false, Ordering::SeqCst);
}
for line in reader.lines() {
let line = line?;
if !line.starts_with('#') {
futures::executor::block_on(nu::run_pipeline_standalone(
line,
matches.is_present("stdin"),
&mut context,
))?;
}
}

View File

@ -324,6 +324,10 @@ mod tests {
loc: fixtures().join("script.nu"),
at: 0
},
Res {
loc: fixtures().join("script_multiline.nu"),
at: 0
},
Res {
loc: fixtures().join("sgml_description.json"),
at: 0

View File

@ -0,0 +1,2 @@
echo [1 4] | count
echo [2 3 5] | count

View File

@ -156,6 +156,15 @@ mod nu_script {
assert_eq!(actual, "done");
}
#[test]
fn run_nu_script_multiline() {
let actual = nu!(cwd: "tests/fixtures/formats", r#"
nu script_multiline.nu
"#);
assert_eq!(actual, "23");
}
}
mod tilde_expansion {