2022-08-31 20:32:56 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
use nu_engine::{eval_block, find_in_dirs_env, redirect_env, CallExt};
|
|
|
|
use nu_protocol::ast::Call;
|
2022-09-08 20:41:49 +00:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2022-08-31 20:32:56 +00:00
|
|
|
use nu_protocol::{
|
2022-09-08 20:41:49 +00:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Value,
|
2022-08-31 20:32:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Source a file for environment variables.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SourceEnv;
|
|
|
|
|
|
|
|
impl Command for SourceEnv {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"source-env"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("source-env")
|
|
|
|
.required(
|
|
|
|
"filename",
|
|
|
|
SyntaxShape::String, // type is string to avoid automatically canonicalizing the path
|
|
|
|
"the filepath to the script file to source the environment from",
|
|
|
|
)
|
|
|
|
.category(Category::Core)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Source the environment from a source file into the current environment."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
caller_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let source_filename: Spanned<String> = call.req(engine_state, caller_stack, 0)?;
|
|
|
|
|
2022-09-08 20:41:49 +00:00
|
|
|
// 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(engine_state, caller_stack, 1)?;
|
|
|
|
|
|
|
|
// Set the currently evaluated directory (file-relative PWD)
|
|
|
|
let mut parent = if let Some(path) =
|
|
|
|
find_in_dirs_env(&source_filename.item, engine_state, caller_stack)?
|
|
|
|
{
|
|
|
|
PathBuf::from(&path)
|
2022-08-31 20:32:56 +00:00
|
|
|
} else {
|
2022-09-08 20:41:49 +00:00
|
|
|
return Err(ShellError::FileNotFound(source_filename.span));
|
|
|
|
};
|
|
|
|
parent.pop();
|
|
|
|
|
Reduced LOC by replacing several instances of `Value::Int {}`, `Value::Float{}`, `Value::Bool {}`, and `Value::String {}` with `Value::int()`, `Value::float()`, `Value::boolean()` and `Value::string()` (#7412)
# Description
While perusing Value.rs, I noticed the `Value::int()`, `Value::float()`,
`Value::boolean()` and `Value::string()` constructors, which seem
designed to make it easier to construct various Values, but which aren't
used often at all in the codebase. So, using a few find-replaces
regexes, I increased their usage. This reduces overall LOC because
structures like this:
```
Value::Int {
val: a,
span: head
}
```
are changed into
```
Value::int(a, head)
```
and are respected as such by the project's formatter.
There are little readability concerns because the second argument to all
of these is `span`, and it's almost always extremely obvious which is
the span at every callsite.
# User-Facing Changes
None.
# Tests + Formatting
Don't forget to add tests that cover your changes.
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.
2022-12-09 16:37:51 +00:00
|
|
|
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
2022-09-08 20:41:49 +00:00
|
|
|
|
|
|
|
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
|
|
|
|
|
|
|
// Evaluate the block
|
|
|
|
let block = engine_state.get_block(block_id as usize).clone();
|
|
|
|
let mut callee_stack = caller_stack.gather_captures(&block.captures);
|
|
|
|
|
|
|
|
let result = eval_block(
|
|
|
|
engine_state,
|
|
|
|
&mut callee_stack,
|
|
|
|
&block,
|
|
|
|
input,
|
|
|
|
call.redirect_stdout,
|
|
|
|
call.redirect_stderr,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Merge the block's environment to the current stack
|
|
|
|
redirect_env(engine_state, caller_stack, &callee_stack);
|
|
|
|
|
|
|
|
// Remove the file-relative PWD
|
|
|
|
caller_stack.remove_env_var(engine_state, "FILE_PWD");
|
|
|
|
|
|
|
|
result
|
2022-08-31 20:32:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![Example {
|
|
|
|
description: "Sources the environment from foo.nu in the current context",
|
|
|
|
example: r#"source-env foo.nu"#,
|
|
|
|
result: None,
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|