Prototype shebang support (#1368)

* Add shebang support to nu.

* Move test file

* Add test for scripts

Co-authored-by: Jason Gedge <jason.gedge@shopify.com>
This commit is contained in:
Jonathan Turner 2020-02-10 08:49:45 -08:00 committed by GitHub
parent a29d52158e
commit fb532f3f4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 6 deletions

View File

@ -1,6 +1,8 @@
use clap::{App, Arg};
use log::LevelFilter;
use std::error::Error;
use std::fs::File;
use std::io::{prelude::*, BufReader};
fn main() -> Result<(), Box<dyn Error>> {
let matches = App::new("nushell")
@ -38,6 +40,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.multiple(false)
.takes_value(false),
)
.arg(
Arg::with_name("script")
.help("the nu script to run")
.index(1),
)
.get_matches();
let loglevel = match matches.value_of("loglevel") {
@ -76,6 +83,8 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
builder.try_init()?;
match matches.values_of("commands") {
None => {}
Some(values) => {
@ -89,12 +98,30 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
builder.try_init()?;
match matches.value_of("script") {
Some(script) => {
let file = File::open(script)?;
let reader = BufReader::new(file);
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"),
))?;
}
}
return Ok(());
}
None => {
println!(
"Welcome to Nushell {} (type 'help' for more info)",
clap::crate_version!()
);
futures::executor::block_on(nu::cli())?;
}
}
println!(
"Welcome to Nushell {} (type 'help' for more info)",
clap::crate_version!()
);
futures::executor::block_on(nu::cli())?;
Ok(())
}

View File

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

2
tests/fixtures/formats/script.nu vendored Executable file
View File

@ -0,0 +1,2 @@
#!/Users/gedge/src/github.com/nushell/nushell/target/debug/nu
echo "done"

View File

@ -122,6 +122,19 @@ mod nu_commands {
}
}
mod nu_script {
use super::nu;
#[test]
fn run_nu_script() {
let actual = nu!(cwd: "tests/fixtures/formats", r#"
nu script.nu
"#);
assert_eq!(actual, "done");
}
}
mod tilde_expansion {
use super::nu;