mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 00:13:21 +01:00
Add support for multiline script files (#1386)
* Add support for multiline script files * clippy
This commit is contained in:
parent
473e9f9422
commit
8ae8ebd107
25
src/cli.rs
25
src/cli.rs
@ -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,7 +394,9 @@ pub async fn run_pipeline_standalone(
|
||||
};
|
||||
|
||||
context.maybe_print_errors(Text::from(line));
|
||||
std::process::exit(error_code);
|
||||
if error_code != 0 {
|
||||
std::process::exit(error_code);
|
||||
}
|
||||
}
|
||||
|
||||
LineResult::Error(line, err) => {
|
||||
|
@ -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>>>>,
|
||||
}
|
||||
|
2
src/env/environment.rs
vendored
2
src/env/environment.rs
vendored
@ -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>,
|
||||
|
6
src/env/environment_syncer.rs
vendored
6
src/env/environment_syncer.rs
vendored
@ -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 {
|
||||
|
@ -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;
|
||||
|
33
src/main.rs
33
src/main.rs
@ -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,
|
||||
))?;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
2
tests/fixtures/formats/script_multiline.nu
vendored
Normal file
2
tests/fixtures/formats/script_multiline.nu
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
echo [1 4] | count
|
||||
echo [2 3 5] | count
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user