mirror of
https://github.com/nushell/nushell
synced 2025-01-12 21:29:07 +00:00
Add $env.CURRENT_FILE variable (#8861)
Co-authored-by: Jelle Besseling <jelle@bigbridge.nl>
This commit is contained in:
parent
b2d7427d2d
commit
8ddebcb932
7 changed files with 48 additions and 22 deletions
|
@ -93,6 +93,10 @@ pub fn evaluate_file(
|
||||||
"FILE_PWD".to_string(),
|
"FILE_PWD".to_string(),
|
||||||
Value::string(parent.to_string_lossy(), Span::unknown()),
|
Value::string(parent.to_string_lossy(), Span::unknown()),
|
||||||
);
|
);
|
||||||
|
stack.add_env_var(
|
||||||
|
"CURRENT_FILE".to_string(),
|
||||||
|
Value::string(file_path.to_string_lossy(), Span::unknown()),
|
||||||
|
);
|
||||||
|
|
||||||
let mut working_set = StateWorkingSet::new(engine_state);
|
let mut working_set = StateWorkingSet::new(engine_state);
|
||||||
trace!("parsing file: {}", file_path_str);
|
trace!("parsing file: {}", file_path_str);
|
||||||
|
|
|
@ -134,6 +134,11 @@ impl Command for OverlayUse {
|
||||||
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(file_path) = &maybe_path {
|
||||||
|
let file_path = Value::string(file_path.to_string_lossy(), call.head);
|
||||||
|
callee_stack.add_env_var("CURRENT_FILE".to_string(), file_path);
|
||||||
|
}
|
||||||
|
|
||||||
let _ = eval_block(
|
let _ = eval_block(
|
||||||
engine_state,
|
engine_state,
|
||||||
&mut callee_stack,
|
&mut callee_stack,
|
||||||
|
|
|
@ -72,16 +72,16 @@ impl Command for Use {
|
||||||
let module_arg_str = String::from_utf8_lossy(
|
let module_arg_str = String::from_utf8_lossy(
|
||||||
engine_state.get_span_contents(&import_pattern.head.span),
|
engine_state.get_span_contents(&import_pattern.head.span),
|
||||||
);
|
);
|
||||||
let maybe_parent = if let Some(path) = find_in_dirs_env(
|
|
||||||
|
let maybe_file_path = find_in_dirs_env(
|
||||||
&module_arg_str,
|
&module_arg_str,
|
||||||
engine_state,
|
engine_state,
|
||||||
caller_stack,
|
caller_stack,
|
||||||
get_dirs_var_from_call(call),
|
get_dirs_var_from_call(call),
|
||||||
)? {
|
)?;
|
||||||
path.parent().map(|p| p.to_path_buf()).or(None)
|
let maybe_parent = maybe_file_path
|
||||||
} else {
|
.as_ref()
|
||||||
None
|
.and_then(|path| path.parent().map(|p| p.to_path_buf()));
|
||||||
};
|
|
||||||
|
|
||||||
let mut callee_stack = caller_stack.gather_captures(&block.captures);
|
let mut callee_stack = caller_stack.gather_captures(&block.captures);
|
||||||
|
|
||||||
|
@ -91,6 +91,11 @@ impl Command for Use {
|
||||||
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
callee_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(file_path) = maybe_file_path {
|
||||||
|
let file_path = Value::string(file_path.to_string_lossy(), call.head);
|
||||||
|
callee_stack.add_env_var("CURRENT_FILE".to_string(), file_path);
|
||||||
|
}
|
||||||
|
|
||||||
// Run the block (discard the result)
|
// Run the block (discard the result)
|
||||||
let _ = eval_block(
|
let _ = eval_block(
|
||||||
engine_state,
|
engine_state,
|
||||||
|
|
2
crates/nu-command/src/env/let_env.rs
vendored
2
crates/nu-command/src/env/let_env.rs
vendored
|
@ -51,7 +51,7 @@ impl Command for LetEnv {
|
||||||
.0
|
.0
|
||||||
.into_value(call.head);
|
.into_value(call.head);
|
||||||
|
|
||||||
if env_var.item == "FILE_PWD" || env_var.item == "PWD" {
|
if env_var.item == "FILE_PWD" || env_var.item == "CURRENT_FILE" || env_var.item == "PWD" {
|
||||||
return Err(ShellError::AutomaticEnvVarSetManually {
|
return Err(ShellError::AutomaticEnvVarSetManually {
|
||||||
envvar_name: env_var.item,
|
envvar_name: env_var.item,
|
||||||
span: env_var.span,
|
span: env_var.span,
|
||||||
|
|
16
crates/nu-command/src/env/load_env.rs
vendored
16
crates/nu-command/src/env/load_env.rs
vendored
|
@ -42,28 +42,22 @@ impl Command for LoadEnv {
|
||||||
match arg {
|
match arg {
|
||||||
Some((cols, vals)) => {
|
Some((cols, vals)) => {
|
||||||
for (env_var, rhs) in cols.into_iter().zip(vals) {
|
for (env_var, rhs) in cols.into_iter().zip(vals) {
|
||||||
if env_var == "FILE_PWD" {
|
let env_var_ = env_var.as_str();
|
||||||
|
if ["FILE_PWD", "CURRENT_FILE", "PWD"].contains(&env_var_) {
|
||||||
return Err(ShellError::AutomaticEnvVarSetManually {
|
return Err(ShellError::AutomaticEnvVarSetManually {
|
||||||
envvar_name: env_var,
|
envvar_name: env_var,
|
||||||
span: call.head,
|
span: call.head,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
stack.add_env_var(env_var, rhs);
|
||||||
if env_var == "PWD" {
|
|
||||||
return Err(ShellError::AutomaticEnvVarSetManually {
|
|
||||||
envvar_name: env_var,
|
|
||||||
span: call.head,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
stack.add_env_var(env_var, rhs);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(PipelineData::empty())
|
Ok(PipelineData::empty())
|
||||||
}
|
}
|
||||||
None => match input {
|
None => match input {
|
||||||
PipelineData::Value(Value::Record { cols, vals, .. }, ..) => {
|
PipelineData::Value(Value::Record { cols, vals, .. }, ..) => {
|
||||||
for (env_var, rhs) in cols.into_iter().zip(vals) {
|
for (env_var, rhs) in cols.into_iter().zip(vals) {
|
||||||
if env_var == "FILE_PWD" {
|
let env_var_ = env_var.as_str();
|
||||||
|
if ["FILE_PWD", "CURRENT_FILE"].contains(&env_var_) {
|
||||||
return Err(ShellError::AutomaticEnvVarSetManually {
|
return Err(ShellError::AutomaticEnvVarSetManually {
|
||||||
envvar_name: env_var,
|
envvar_name: env_var,
|
||||||
span: call.head,
|
span: call.head,
|
||||||
|
|
15
crates/nu-command/src/env/source_env.rs
vendored
15
crates/nu-command/src/env/source_env.rs
vendored
|
@ -47,7 +47,7 @@ impl Command for SourceEnv {
|
||||||
let block_id: i64 = call.req_parser_info(engine_state, caller_stack, "block_id")?;
|
let block_id: i64 = call.req_parser_info(engine_state, caller_stack, "block_id")?;
|
||||||
|
|
||||||
// Set the currently evaluated directory (file-relative PWD)
|
// Set the currently evaluated directory (file-relative PWD)
|
||||||
let mut parent = if let Some(path) = find_in_dirs_env(
|
let file_path = if let Some(path) = find_in_dirs_env(
|
||||||
&source_filename.item,
|
&source_filename.item,
|
||||||
engine_state,
|
engine_state,
|
||||||
caller_stack,
|
caller_stack,
|
||||||
|
@ -57,11 +57,17 @@ impl Command for SourceEnv {
|
||||||
} else {
|
} else {
|
||||||
return Err(ShellError::FileNotFound(source_filename.span));
|
return Err(ShellError::FileNotFound(source_filename.span));
|
||||||
};
|
};
|
||||||
parent.pop();
|
|
||||||
|
|
||||||
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
if let Some(parent) = file_path.parent() {
|
||||||
|
let file_pwd = Value::string(parent.to_string_lossy(), call.head);
|
||||||
|
|
||||||
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
caller_stack.add_env_var("FILE_PWD".to_string(), file_pwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
caller_stack.add_env_var(
|
||||||
|
"CURRENT_FILE".to_string(),
|
||||||
|
Value::string(file_path.to_string_lossy(), call.head),
|
||||||
|
);
|
||||||
|
|
||||||
// Evaluate the block
|
// Evaluate the block
|
||||||
let block = engine_state.get_block(block_id as usize).clone();
|
let block = engine_state.get_block(block_id as usize).clone();
|
||||||
|
@ -81,6 +87,7 @@ impl Command for SourceEnv {
|
||||||
|
|
||||||
// Remove the file-relative PWD
|
// Remove the file-relative PWD
|
||||||
caller_stack.remove_env_var(engine_state, "FILE_PWD");
|
caller_stack.remove_env_var(engine_state, "FILE_PWD");
|
||||||
|
caller_stack.remove_env_var(engine_state, "CURRENT_FILE");
|
||||||
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
|
@ -126,6 +126,17 @@ fn has_file_pwd() {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn has_file_loc() {
|
||||||
|
Playground::setup("has_file_pwd", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![FileWithContent("spam.nu", "$env.CURRENT_FILE")]);
|
||||||
|
|
||||||
|
let actual = nu!(cwd: dirs.test(), "nu spam.nu");
|
||||||
|
|
||||||
|
assert!(actual.out.ends_with("spam.nu"));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// FIXME: autoenv not currently implemented
|
// FIXME: autoenv not currently implemented
|
||||||
#[ignore]
|
#[ignore]
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in a new issue