account for startup commands in the scope. (#3261)

* Revert "Impl one configurable function to run scripts (#3242)"
* pass config startup.
This commit is contained in:
Andrés N. Robalino
2021-04-04 00:14:58 -05:00
committed by GitHub
parent 4c09716ad8
commit 00acf22f5f
18 changed files with 220 additions and 306 deletions

View File

@ -505,8 +505,6 @@ mod tests {
};
#[cfg(feature = "which")]
use super::{run_external_command, InputStream};
#[cfg(feature = "which")]
use nu_engine::filesystem::filesystem_shell::FilesystemShellMode;
#[cfg(feature = "which")]
use futures::executor::block_on;
@ -535,8 +533,8 @@ mod tests {
let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build();
let input = InputStream::empty();
let mut ctx = basic_evaluation_context(FilesystemShellMode::Cli)
.expect("There was a problem creating a basic context.");
let mut ctx =
basic_evaluation_context().expect("There was a problem creating a basic context.");
assert!(
run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout)

View File

@ -1,13 +1,10 @@
use crate::prelude::*;
use nu_engine::basic_evaluation_context;
use nu_engine::whole_stream_command;
use nu_engine::{basic_evaluation_context, filesystem::filesystem_shell::FilesystemShellMode};
use std::error::Error;
pub fn create_default_context(
mode: FilesystemShellMode,
interactive: bool,
) -> Result<EvaluationContext, Box<dyn Error>> {
let context = basic_evaluation_context(mode)?;
pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Box<dyn Error>> {
let context = basic_evaluation_context()?;
{
use crate::commands::*;

View File

@ -1,11 +1,9 @@
use std::path::PathBuf;
use crate::prelude::*;
use nu_engine::{script, WholeStreamCommand};
use nu_errors::ShellError;
use nu_parser::expand_path;
use nu_protocol::{NuScript, RunScriptOptions, Signature, SyntaxShape};
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
pub struct Source;
@ -46,12 +44,27 @@ pub async fn source(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = EvaluationContext::from_args(&args);
let (SourceArgs { filename }, _) = args.process().await?;
let script = NuScript::File(PathBuf::from(expand_path(&filename.item).to_string()));
let options = RunScriptOptions::default()
.use_existing_scope(true)
.redirect_stdin(true)
.exit_on_error(false);
script::run_script(script, &options, &ctx).await;
// Note: this is a special case for setting the context from a command
// In this case, if we don't set it now, we'll lose the scope that this
// variable should be set into.
let contents = std::fs::read_to_string(expand_path(&filename.item).into_owned());
match contents {
Ok(contents) => {
let result = script::run_script_standalone(contents, true, &ctx, false).await;
Ok(OutputStream::empty())
if let Err(err) = result {
ctx.error(err.into());
}
Ok(OutputStream::empty())
}
Err(_) => {
ctx.error(ShellError::labeled_error(
"Can't load file to source",
"can't load file",
filename.span(),
));
Ok(OutputStream::empty())
}
}
}

View File

@ -6,7 +6,6 @@ mod stub_generate;
use double_echo::Command as DoubleEcho;
use double_ls::Command as DoubleLs;
use nu_engine::filesystem::filesystem_shell::FilesystemShellMode;
use stub_generate::{mock_path, Command as StubOpen};
use nu_engine::basic_evaluation_context;
@ -27,7 +26,7 @@ use futures::executor::block_on;
pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
// Command Doubles
@ -93,7 +92,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> {
pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
whole_stream_command(Echo {}),
@ -150,7 +149,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> {
pub fn test_anchors(cmd: Command) -> Result<(), ShellError> {
let examples = cmd.examples();
let base_context = basic_evaluation_context(FilesystemShellMode::Cli)?;
let base_context = basic_evaluation_context()?;
base_context.add_commands(vec![
// Minimal restricted commands to aid in testing