mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 09:45:50 +02:00
Some cleanups for cd/PWD (#667)
* Some cleanups for cd/PWD * Some cleanups for cd/PWD
This commit is contained in:
19
crates/nu-command/src/env/let_env.rs
vendored
19
crates/nu-command/src/env/let_env.rs
vendored
@ -1,7 +1,7 @@
|
||||
use nu_engine::eval_expression;
|
||||
use nu_engine::{current_dir, eval_expression};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape};
|
||||
use nu_protocol::{Category, PipelineData, Signature, SyntaxShape, Value};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LetEnv;
|
||||
@ -43,7 +43,20 @@ impl Command for LetEnv {
|
||||
|
||||
let rhs = eval_expression(engine_state, stack, keyword_expr)?;
|
||||
|
||||
stack.add_env_var(env_var, rhs);
|
||||
if env_var == "PWD" {
|
||||
let cwd = current_dir(engine_state, stack)?;
|
||||
let rhs = rhs.as_string()?;
|
||||
let rhs = nu_path::expand_path_with(rhs, cwd);
|
||||
stack.add_env_var(
|
||||
env_var,
|
||||
Value::String {
|
||||
val: rhs.to_string_lossy().to_string(),
|
||||
span: call.head,
|
||||
},
|
||||
);
|
||||
} else {
|
||||
stack.add_env_var(env_var, rhs);
|
||||
}
|
||||
Ok(PipelineData::new(call.head))
|
||||
}
|
||||
}
|
||||
|
@ -57,6 +57,22 @@ pub fn test_examples(cmd: impl Command + 'static) {
|
||||
}
|
||||
let start = std::time::Instant::now();
|
||||
|
||||
let mut stack = Stack::new();
|
||||
|
||||
// Set up PWD
|
||||
stack.add_env_var(
|
||||
"PWD".to_string(),
|
||||
Value::String {
|
||||
val: cwd.to_string_lossy().to_string(),
|
||||
span: Span::test_data(),
|
||||
},
|
||||
);
|
||||
let _ = engine_state.merge_delta(
|
||||
StateWorkingSet::new(&*engine_state).render(),
|
||||
Some(&mut stack),
|
||||
&cwd,
|
||||
);
|
||||
|
||||
let (block, delta) = {
|
||||
let mut working_set = StateWorkingSet::new(&*engine_state);
|
||||
let (output, err) = parse(&mut working_set, None, example.example.as_bytes(), false);
|
||||
|
@ -2,7 +2,7 @@ use std::path::Path;
|
||||
|
||||
use nu_engine::env::current_dir_str;
|
||||
use nu_engine::CallExt;
|
||||
use nu_path::{canonicalize_with, expand_path};
|
||||
use nu_path::{canonicalize_with, expand_path_with};
|
||||
use nu_protocol::{engine::Command, Example, ShellError, Signature, Span, SyntaxShape, Value};
|
||||
|
||||
use super::PathSubcommandArguments;
|
||||
@ -82,7 +82,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Expand a relative path",
|
||||
example: r"'foo\..\bar' | path expand",
|
||||
result: Some(Value::test_string("bar")),
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -103,7 +103,7 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Expand a relative path",
|
||||
example: "'foo/../bar' | path expand",
|
||||
result: Some(Value::test_string("bar")),
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
}
|
||||
@ -123,7 +123,7 @@ fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||
),
|
||||
}
|
||||
} else {
|
||||
Value::string(expand_path(path).to_string_lossy(), span)
|
||||
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,11 +38,7 @@ impl Command for SubCommand {
|
||||
"Optionally operate by column path",
|
||||
Some('c'),
|
||||
)
|
||||
.optional(
|
||||
"append",
|
||||
SyntaxShape::Filepath,
|
||||
"Path to append to the input",
|
||||
)
|
||||
.optional("append", SyntaxShape::String, "Path to append to the input")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
|
@ -30,7 +30,7 @@ impl Command for SubCommand {
|
||||
Signature::build("path relative-to")
|
||||
.required(
|
||||
"path",
|
||||
SyntaxShape::Filepath,
|
||||
SyntaxShape::String,
|
||||
"Parent shared with the input path",
|
||||
)
|
||||
.named(
|
||||
@ -116,7 +116,9 @@ path."#
|
||||
fn relative_to(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||
match path.strip_prefix(Path::new(&args.path.item)) {
|
||||
Ok(p) => Value::string(p.to_string_lossy(), span),
|
||||
Err(_) => todo!(),
|
||||
Err(e) => Value::Error {
|
||||
error: ShellError::CantConvert(e.to_string(), "string".into(), span),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,15 +105,16 @@ impl<'call> ExternalCommand<'call> {
|
||||
input: PipelineData,
|
||||
config: Config,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let mut process = self.create_command();
|
||||
let head = self.name.span;
|
||||
|
||||
let ctrlc = engine_state.ctrlc.clone();
|
||||
|
||||
// TODO. We don't have a way to know the current directory
|
||||
// This should be information from the EvaluationContex or EngineState
|
||||
if let Some(d) = self.env_vars.get("PWD") {
|
||||
let mut process = if let Some(d) = self.env_vars.get("PWD") {
|
||||
let mut process = self.create_command(d);
|
||||
process.current_dir(d);
|
||||
process
|
||||
} else {
|
||||
return Err(ShellError::SpannedLabeledErrorHelp(
|
||||
"Current directory not found".to_string(),
|
||||
@ -124,7 +125,7 @@ impl<'call> ExternalCommand<'call> {
|
||||
"It is required to define the current directory when running an external command."
|
||||
).to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
process.envs(&self.env_vars);
|
||||
|
||||
@ -239,7 +240,7 @@ impl<'call> ExternalCommand<'call> {
|
||||
}
|
||||
}
|
||||
|
||||
fn create_command(&self) -> CommandSys {
|
||||
fn create_command(&self, cwd: &str) -> CommandSys {
|
||||
// in all the other cases shell out
|
||||
if cfg!(windows) {
|
||||
//TODO. This should be modifiable from the config file.
|
||||
@ -248,22 +249,24 @@ impl<'call> ExternalCommand<'call> {
|
||||
if self.name.item.ends_with(".cmd") || self.name.item.ends_with(".bat") {
|
||||
self.spawn_cmd_command()
|
||||
} else {
|
||||
self.spawn_simple_command()
|
||||
self.spawn_simple_command(cwd)
|
||||
}
|
||||
} else if self.name.item.ends_with(".sh") {
|
||||
self.spawn_sh_command()
|
||||
} else {
|
||||
self.spawn_simple_command()
|
||||
self.spawn_simple_command(cwd)
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a command without shelling out to an external shell
|
||||
fn spawn_simple_command(&self) -> std::process::Command {
|
||||
fn spawn_simple_command(&self, cwd: &str) -> std::process::Command {
|
||||
let mut process = std::process::Command::new(&self.name.item);
|
||||
|
||||
for arg in &self.args {
|
||||
let arg = trim_enclosing_quotes(arg);
|
||||
let arg = nu_path::expand_path(arg).to_string_lossy().to_string();
|
||||
let arg = nu_path::expand_path_with(arg, cwd)
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
|
||||
let arg = arg.replace("\\", "\\\\");
|
||||
|
||||
|
Reference in New Issue
Block a user