Skip to content

Instantly share code, notes, and snippets.

@Villenny
Last active June 25, 2023 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Villenny/03870173ec0f4615af81f093c8d01519 to your computer and use it in GitHub Desktop.
Save Villenny/03870173ec0f4615af81f093c8d01519 to your computer and use it in GitHub Desktop.
Chocolatey refreshenv for GIT Bash.
function refreshenv
{
powershell -NonInteractive - <<EOF
\$filename="C:\temp\refresh.env"
Write-Host("Updating environment session from chocolatey")
Import-Module "\$env:ChocolateyInstall\\helpers\\chocolateyProfile.psm1"
Update-SessionEnvironment
Write-Host("Scanning env")
# Round brackets in variable names cause problems with bash
\$results = Get-ChildItem env:* | %{
\$name = \$_.Name
if (!(\$name.Contains('(')) -And !(\$name.Contains('.'))) {
\$value = \$_.Value
if (\$name -eq 'PATH' -Or \$name -eq 'ORIGINAL_PATH') {
\$value = \$value -replace 'C:\\\\','/c/'
\$value = \$value -replace 'D:\\\\','/d/'
\$value = \$value -replace '\\\\','/'
\$value = \$value -replace ';',':'
}
if (\$name -eq 'PATH') {
\$hm = \$env:HOME
\$hm = \$hm -replace 'C:\\\\','/c/'
\$hm = \$hm -replace 'D:\\\\','/d/'
\$hm = \$hm -replace '\\\\','/'
\$hm = \$hm -replace ';',':'
\$value = "\$hm/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:\$value"
}
if ((\$name -ne 'HOME') -And \$name -ne 'SHELL' -And \$name -ne 'INFOPATH' -And \$name -ne 'MANPATH' -And \$name -ne 'PKG_CONFIG_PATH' -And \$name -ne 'PWD') {
Write-Output ("export \$name='\$value'")
}
}
}
# Write-Host("results = \$results")
\$results | Out-File -Encoding ascii \$filename
Write-Host("Wrote \$filename")
EOF
source "/c/temp/refresh.env"
if [[ -f ~/.bashrc ]]; then
source ~/.bashrc
fi
}
alias RefreshEnv=refreshenv
@earonesty
Copy link

thanks!

@mhd-hi
Copy link

mhd-hi commented Feb 24, 2023

Where are you using this code? 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment