Last active
September 1, 2023 13:40
-
-
Save marler8997/e44eacfd2bf40ce969658d9cfd697b51 to your computer and use it in GitHub Desktop.
NuShell Source Batch Environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A crude proof of concept to show how to import the environment variables created from | |
# a BATCH script. You can use this to make your NuShell into a Visual Studio Command Prompt. | |
# | |
# The current solution passes the input as a command to CMD.exe and appends a couple commands | |
# to dump a marker and then all environment variables which are then parsed and added to | |
# the caller environment. A better solution would be to not capture stdout/stderr in the | |
# child process and instead append a command that passes the resulting environment variables | |
# back to the NuShell process through a side-channel (i.e. named pipe maybe). It's probably | |
# best for NuShell to just support this natively as it's a common use case, maybe it can | |
# do so by just allowing the "source" command to accept BATCH files...and extra command-line | |
# arguments to to forward to the BATCH file. | |
# | |
# TODO: can we detect how we were run and report an error if it wasn't done using source? | |
def-env sourcebat [...cmd: string] { | |
let cmd = ($cmd | append ["&&" echo "___ENV_FOR_NUSHELL___" "&&" set]) | |
let result = do { cmd.exe /c $cmd } | complete | |
let offset = $result.stdout | str index-of "___ENV_FOR_NUSHELL__" | |
if $offset == -1 { | |
print $result.stdout | |
print $result.stderr | |
return $result.exit_code | |
} | |
print ($result.stdout | str substring 0..$offset) | |
print $result.stderr | |
let offset = $offset + 20 | |
let batenv = $result.stdout | str substring $offset.. | split row "\n" | |
for line in $batenv { | |
let eq_index = $line | str index-of "=" | |
if $eq_index == -1 { continue } | |
let name = $line | str substring ..$eq_index | |
let value_start = $eq_index + 1 | |
let value = $line | str substring $value_start.. | |
print $"Setting ($name) to ($value)" | |
if $name != PWD and $name != CURRENT_FILE and $name != FILE_PWD { | |
load-env { $name: $value } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment