diff --git a/build/InstallGit.cmd b/build/InstallGit.cmd index 26f8088f35..e009e2594e 100644 --- a/build/InstallGit.cmd +++ b/build/InstallGit.cmd @@ -1,23 +1,33 @@ @ECHO OFF SETLOCAL -REM SETLOCAL is on, so changes to the path not persist to the actual user's path + :: SETLOCAL is on, so changes to the path not persist to the actual user's path git.exe --version -if %ERRORLEVEL%==9009 GOTO :trydefaultpath +IF %ERRORLEVEL%==9009 GOTO :trydefaultpath GOTO :EOF + :: Git is installed, no need to to anything else :trydefaultpath -path=C:\Program Files (x86)\Git\cmd;C:\Program Files\Git\cmd;%PATH% +PATH=C:\Program Files (x86)\Git\cmd;C:\Program Files\Git\cmd;%PATH% git.exe --version -if %ERRORLEVEL%==9009 GOTO :showerror +IF %ERRORLEVEL%==9009 GOTO :showerror GOTO :EOF + :: Git is installed, no need to to anything else :showerror ECHO Git is not in your path and could not be found in C:\Program Files (x86)\Git\cmd nor in C:\Program Files\Git\cmd -set /p install=" Do you want to install Git through Chocolatey [y/n]? " %=% -if %install%==y ( +SET /p install=" Do you want to install Git through Chocolatey [y/n]? " %=% +IF %install%==y ( + :: Create a temporary batch file to execute either after elevating to admin or as-is when the user is already admin + ECHO @ECHO OFF > "%temp%\ChocoInstallGit.cmd" + ECHO SETLOCAL >> "%temp%\ChocoInstallGit.cmd" + ECHO ECHO Installing Chocolatey first >> "%temp%\ChocoInstallGit.cmd" + ECHO @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" >> "%temp%\ChocoInstallGit.cmd" + ECHO SET PATH=%%PATH%%;%%ALLUSERSPROFILE%%\chocolatey\bin >> "%temp%\ChocoInstallGit.cmd" + ECHO choco install git -y >> "%temp%\ChocoInstallGit.cmd" + GOTO :installgit -) else ( +) ELSE ( GOTO :cantcontinue ) @@ -26,7 +36,28 @@ ECHO Can't complete the build without Git being in the path. Please add it to be GOTO :EOF :installgit -ECHO Installing Chocolatey first -@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin -ECHO Installing Git through Chocolatey -choco install git \ No newline at end of file +pushd %~dp0 + :: Running prompt elevated + +:: --> Check for permissions +>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" + +:: --> If error flag set, we do not have admin. +IF '%errorlevel%' NEQ '0' ( + GOTO UACPrompt +) ELSE ( GOTO gotAdmin ) + +:UACPrompt + ECHO You're not currently running this with admin privileges, we'll now try to execute the install of Git through Chocolatey after elevating to admin privileges + ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" + ECHO UAC.ShellExecute "%temp%\ChocoInstallGit.cmd", "", "", "runas", 1 >> "%temp%\getadmin.vbs" + + "%temp%\getadmin.vbs" + EXIT /B + +:gotAdmin + IF EXIST "%temp%\getadmin.vbs" ( DEL "%temp%\getadmin.vbs" ) + pushd "%CD%" + CD /D "%~dp0" + + CALL "%temp%\ChocoInstallGit.cmd" \ No newline at end of file