From 952338a23378547913fa3b1033566d83d56c3f6f Mon Sep 17 00:00:00 2001 From: Tyler Arehart Date: Mon, 18 Jun 2018 22:27:11 -0700 Subject: [PATCH] Smoothing out the setup process. --- RefreshEnv.cmd | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ run-gui.bat | 3 +++ run.bat | 3 +++ 3 files changed, 72 insertions(+) create mode 100644 RefreshEnv.cmd diff --git a/RefreshEnv.cmd b/RefreshEnv.cmd new file mode 100644 index 0000000..567fd78 --- /dev/null +++ b/RefreshEnv.cmd @@ -0,0 +1,66 @@ +@echo off +:: This file is taken from chocolatey: +:: https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd +:: +:: RefreshEnv.cmd +:: +:: Batch file to read environment variables from registry and +:: set session variables to these values. +:: +:: With this batch file, there should be no need to reload command +:: environment every time you want environment changes to propagate + +::echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell" +echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. 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" + ) + goto :EOF + +:: Get a list of environment variables from registry +:GetRegEnv + "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp" + for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do ( + if /I not "%%~A"=="Path" ( + call :SetFromReg "%~1" "%%~A" "%%~A" + ) + ) + goto :EOF + +:main + echo/@echo off >"%TEMP%\_env.cmd" + + :: Slowly generating final file + call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd" + call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd" + + :: Special handling for PATH - mix both User and System + call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd" + 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" + + :: Cleanup + del /f /q "%TEMP%\_envset.tmp" 2>nul + del /f /q "%TEMP%\_envget.tmp" 2>nul + + :: capture user / architecture + SET "OriginalUserName=%USERNAME%" + SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%" + + :: Set these variables + call "%TEMP%\_env.cmd" + + :: reset user / architecture + SET "USERNAME=%OriginalUserName%" + SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%" + + echo | set /p dummy="Finished." + echo . \ No newline at end of file diff --git a/run-gui.bat b/run-gui.bat index 3a10936..f377b92 100644 --- a/run-gui.bat +++ b/run-gui.bat @@ -3,6 +3,9 @@ @rem Change the working directory to the location of this file so that relative paths will work cd /D "%~dp0" +@rem Make sure the environment variables are up-to-date. This is useful if the user installed python a moment ago. +call ./RefreshEnv.cmd + setlocal EnableDelayedExpansion @rem Run the is_safe_to_upgrade function and save the output to a temp file. diff --git a/run.bat b/run.bat index b9d0104..443cbb4 100644 --- a/run.bat +++ b/run.bat @@ -3,6 +3,9 @@ @rem Change the working directory to the location of this file so that relative paths will work cd /D "%~dp0" +@rem Make sure the environment variables are up-to-date. This is useful if the user installed python a moment ago. +call ./RefreshEnv.cmd + setlocal EnableDelayedExpansion @rem Run the is_safe_to_upgrade function and save the output to a temp file.