mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 02:55:07 +02:00
Run exitscripts in original dir (#2352)
* Modify testcase * Run exitscript in the folder it was specified * Update documentation * Add comment * Borrow instead of clone * Does this just... work on windows? * fmt * as_str * Collapse if by order of clippy * Support windows * fmt * refactor tests * fmt * This time it will work on windows FOR SURE * Remove debug prints * Comment * Refactor tests * fmt * fix spelling * update comment
This commit is contained in:
@ -55,7 +55,7 @@ impl WholeStreamCommand for Autoenv {
|
||||
The file can contain several optional sections:
|
||||
env: environment variables to set when visiting the directory. The variables are unset after leaving the directory and any overwritten values are restored.
|
||||
scriptvars: environment variables that should be set to the return value of a script. After they have been set, they behave in the same way as variables set in the env section.
|
||||
scripts: scripts to run when entering the directory or leaving it. Note that exitscripts are not run in the directory they are declared in."#
|
||||
scripts: scripts to run when entering the directory or leaving it."#
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
|
@ -112,7 +112,7 @@ impl DirectorySpecificEnvironment {
|
||||
|
||||
if let Some(es) = nu_env_doc.entryscripts {
|
||||
for s in es {
|
||||
run(s.as_str())?;
|
||||
run(s.as_str(), None)?;
|
||||
}
|
||||
}
|
||||
|
||||
@ -147,7 +147,7 @@ impl DirectorySpecificEnvironment {
|
||||
new_exitscripts.insert(dir, scripts);
|
||||
} else {
|
||||
for s in scripts {
|
||||
run(s.as_str())?;
|
||||
run(s.as_str(), Some(&dir))?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -215,9 +215,23 @@ impl DirectorySpecificEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
fn run(cmd: &str) -> Result<(), ShellError> {
|
||||
fn run(cmd: &str, dir: Option<&PathBuf>) -> Result<(), ShellError> {
|
||||
if cfg!(target_os = "windows") {
|
||||
Command::new("cmd").args(&["/C", cmd]).output()?
|
||||
if let Some(dir) = dir {
|
||||
let command = format!("cd {} & {}", dir.to_string_lossy(), cmd);
|
||||
Command::new("cmd")
|
||||
.args(&["/C", command.as_str()])
|
||||
.output()?
|
||||
} else {
|
||||
Command::new("cmd").args(&["/C", cmd]).output()?
|
||||
}
|
||||
} else if let Some(dir) = dir {
|
||||
// FIXME: When nu scripting is added, cding like might not be a good idea. If nu decides to execute entryscripts when entering the dir this way, it will cause troubles.
|
||||
// For now only standard shell scripts are used, so this is an issue for the future.
|
||||
Command::new("sh")
|
||||
.arg("-c")
|
||||
.arg(format!("cd {:?}; {}", dir, cmd))
|
||||
.output()?
|
||||
} else {
|
||||
Command::new("sh").arg("-c").arg(&cmd).output()?
|
||||
};
|
||||
|
Reference in New Issue
Block a user