Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Commit

Permalink
(GH-153)(GH-134) Update PATH on cmd.exe
Browse files Browse the repository at this point in the history
This adds a command RefreshEnv.cmd to the bin files, so that one can update
environment variables out of band. There is zero chance it will work once
chocolatey.cmd calls exit /b %ERRORLEVEL% as exiting the batch effectively
destroys any updates to local environmental changes. The way it currently
works is effectively the same as using setlocal due to the use of exit.

Since this command can be called out of band, it should be called out of
band, whenever the user wants to refresh their environment variables they
can call this. Note this only works in cmd.exe shell. There is no current way
to update a powershell session other than importing the helper functions as
a module and running Update-SessionEnvironment.
  • Loading branch information
ferventcoder committed Jun 16, 2014
1 parent 0001125 commit a09e158
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 17 deletions.
2 changes: 1 addition & 1 deletion nuget/tools/chocolateysetup.psm1
Expand Up @@ -113,7 +113,7 @@ param(
)

$redirectsPath = Join-Path $chocolateyInstallPath 'redirects'
$exeFiles = Get-ChildItem "$redirectsPath" -filter *.exe
$exeFiles = Get-ChildItem "$redirectsPath" -include @("*.exe","*.cmd") -recurse
foreach ($exeFile in $exeFiles) {
$exeFilePath = $exeFile.FullName
$exeFileName = [System.IO.Path]::GetFileName("$exeFilePath")
Expand Down
1 change: 0 additions & 1 deletion src/chocolatey.cmd
Expand Up @@ -15,7 +15,6 @@ IF NOT '%1'=='' SET PS_ARGS=%PS_ARGS:\\"=\"%

@PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%DIR%chocolatey.ps1' %PS_ARGS%"
SET ErrLvl=%ERRORLEVEL%
call "%DIR%RefreshEnv.cmd"
goto :exit

:exit
Expand Down
21 changes: 6 additions & 15 deletions src/RefreshEnv.cmd → src/redirects/RefreshEnv.cmd
Expand Up @@ -7,17 +7,16 @@
::
:: With this batch file, there should be no need to reload command
:: environment every time you want environment changes to propagate
::

:: A strange trick to print without newline
:: echo | set /p dummy="Reading environment variables from registry. Please wait... "
echo | set /p dummy="Reading environment variables from registry. Please wait... "

goto main

:: Set one environment variable from registry key
:SetFromReg
"%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
echo/set "%~3=%%B"
echo/set %~3=%%B
)
goto :EOF

Expand All @@ -43,11 +42,7 @@ goto main
call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"

:: Caution: do not insert space-chars before >> redirection sign
echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"

:: In final file: remove temp variables
echo/set "Path_HKLM=" >> "%TEMP%\_env.cmd"
echo/set "Path_HKCU=" >> "%TEMP%\_env.cmd"
echo/set Path=%%Path_HKLM%%;%%Path_HKCU%% >> "%TEMP%\_env.cmd"

:: Cleanup
del /f /q "%TEMP%\_envset.tmp" 2>nul
Expand All @@ -56,9 +51,5 @@ goto main
:: Set these variables
call "%TEMP%\_env.cmd"

:: Finish
:: echo | set /p dummy="Done"
::echo .


exit /b %ERRORLEVEL%
echo | set /p dummy="Done"
echo .

0 comments on commit a09e158

Please sign in to comment.