mirror of
https://github.com/nushell/nushell.git
synced 2025-04-30 08:04:25 +02:00
# Description Fixes #7301. # User-Facing Changes `return` can now be used in scripts without explicit `def main`. # Tests + Formatting Don't forget to add tests that cover your changes. (I'm not sure how to test this.) Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
76 lines
2.2 KiB
Rust
76 lines
2.2 KiB
Rust
use nu_engine::{eval_block_with_early_return, CallExt};
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{Category, Example, PipelineData, ShellError, Signature, SyntaxShape, Type};
|
|
|
|
/// Source a file for environment variables.
|
|
#[derive(Clone)]
|
|
pub struct Source;
|
|
|
|
impl Command for Source {
|
|
fn name(&self) -> &str {
|
|
"source"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("source")
|
|
.input_output_types(vec![(Type::Any, Type::Any)])
|
|
.required(
|
|
"filename",
|
|
SyntaxShape::Filepath,
|
|
"the filepath to the script file to source",
|
|
)
|
|
.category(Category::Core)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Runs a script file in the current context."
|
|
}
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
r#"This command is a parser keyword. For details, check:
|
|
https://www.nushell.sh/book/thinking_in_nu.html"#
|
|
}
|
|
|
|
fn is_parser_keyword(&self) -> bool {
|
|
true
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
// Note: this hidden positional is the block_id that corresponded to the 0th position
|
|
// it is put here by the parser
|
|
let block_id: i64 = call.req_parser_info(engine_state, stack, 0)?;
|
|
|
|
let block = engine_state.get_block(block_id as usize).clone();
|
|
eval_block_with_early_return(
|
|
engine_state,
|
|
stack,
|
|
&block,
|
|
input,
|
|
call.redirect_stdout,
|
|
call.redirect_stderr,
|
|
)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Runs foo.nu in the current context",
|
|
example: r#"source foo.nu"#,
|
|
result: None,
|
|
},
|
|
Example {
|
|
description: "Runs foo.nu in current context and call the command defined, suppose foo.nu has content: `def say-hi [] { echo 'Hi!' }`",
|
|
example: r#"source ./foo.nu; say-hi"#,
|
|
result: None,
|
|
},
|
|
]
|
|
}
|
|
}
|