Merge pull request #11660 from CarolEidt/LSRACleanup
[platform/upstream/coreclr.git] / init-tools.cmd
1 @if not defined _echo @echo off
2 setlocal
3
4 set INIT_TOOLS_LOG=%~dp0init-tools.log
5 if [%PACKAGES_DIR%]==[] set PACKAGES_DIR=%~dp0packages\
6 if [%TOOLRUNTIME_DIR%]==[] set TOOLRUNTIME_DIR=%~dp0Tools
7 set DOTNET_PATH=%TOOLRUNTIME_DIR%\dotnetcli\
8 if [%DOTNET_CMD%]==[] set DOTNET_CMD=%DOTNET_PATH%dotnet.exe
9 if [%BUILDTOOLS_SOURCE%]==[] set BUILDTOOLS_SOURCE=https://dotnet.myget.org/F/dotnet-buildtools/api/v3/index.json
10 set /P BUILDTOOLS_VERSION=< "%~dp0BuildToolsVersion.txt"
11 set BUILD_TOOLS_PATH=%PACKAGES_DIR%Microsoft.DotNet.BuildTools\%BUILDTOOLS_VERSION%\lib\
12 set INIT_TOOLS_RESTORE_PROJECT=%~dp0init-tools.msbuild
13 set BUILD_TOOLS_SEMAPHORE=%TOOLRUNTIME_DIR%\%BUILDTOOLS_VERSION%\init-tools.completed
14
15 :: if force option is specified then clean the tool runtime and build tools package directory to force it to get recreated
16 if [%1]==[force] (
17   if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
18   if exist "%PACKAGES_DIR%Microsoft.DotNet.BuildTools" rmdir /S /Q "%PACKAGES_DIR%Microsoft.DotNet.BuildTools"
19 )
20
21 :: If sempahore exists do nothing
22 if exist "%BUILD_TOOLS_SEMAPHORE%" (
23   echo Tools are already initialized.
24   goto :EOF
25 )
26
27 if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
28
29 :: Download Nuget.exe
30 if NOT exist "%PACKAGES_DIR%NuGet.exe" (
31   if NOT exist "%PACKAGES_DIR%" mkdir "%PACKAGES_DIR%"
32   powershell -NoProfile -ExecutionPolicy unrestricted -Command "(New-Object Net.WebClient).DownloadFile('https://www.nuget.org/nuget.exe', '%PACKAGES_DIR%NuGet.exe')
33 )
34
35 echo Running %0 > "%INIT_TOOLS_LOG%"
36
37 set /p DOTNET_VERSION=< "%~dp0DotnetCLIVersion.txt"
38 if exist "%DOTNET_CMD%" goto :afterdotnetrestore
39
40 echo Installing dotnet cli...
41 if NOT exist "%DOTNET_PATH%" mkdir "%DOTNET_PATH%"
42 set DOTNET_ZIP_NAME=dotnet-dev-win-x64.%DOTNET_VERSION%.zip
43 set DOTNET_REMOTE_PATH=https://dotnetcli.blob.core.windows.net/dotnet/Sdk/%DOTNET_VERSION%/%DOTNET_ZIP_NAME%
44 set DOTNET_LOCAL_PATH=%DOTNET_PATH%%DOTNET_ZIP_NAME%
45 echo Installing '%DOTNET_REMOTE_PATH%' to '%DOTNET_LOCAL_PATH%' >> "%INIT_TOOLS_LOG%"
46 powershell -NoProfile -ExecutionPolicy unrestricted -Command "$retryCount = 0; $success = $false; do { try { (New-Object Net.WebClient).DownloadFile('%DOTNET_REMOTE_PATH%', '%DOTNET_LOCAL_PATH%'); $success = $true; } catch { if ($retryCount -ge 6) { throw; } else { $retryCount++; Start-Sleep -Seconds (5 * $retryCount); } } } while ($success -eq $false); Add-Type -Assembly 'System.IO.Compression.FileSystem' -ErrorVariable AddTypeErrors; if ($AddTypeErrors.Count -eq 0) { [System.IO.Compression.ZipFile]::ExtractToDirectory('%DOTNET_LOCAL_PATH%', '%DOTNET_PATH%') } else { (New-Object -com shell.application).namespace('%DOTNET_PATH%').CopyHere((new-object -com shell.application).namespace('%DOTNET_LOCAL_PATH%').Items(),16) }" >> "%INIT_TOOLS_LOG%"
47 if NOT exist "%DOTNET_LOCAL_PATH%" (
48   echo ERROR: Could not install dotnet cli correctly. See '%INIT_TOOLS_LOG%' for more details. 1>&2
49   exit /b 1
50 )
51
52 :afterdotnetrestore
53
54 if exist "%BUILD_TOOLS_PATH%" goto :afterbuildtoolsrestore
55 echo Restoring BuildTools version %BUILDTOOLS_VERSION%...
56 echo Running: "%DOTNET_CMD%" restore "%INIT_TOOLS_RESTORE_PROJECT%" --no-cache --packages %PACKAGES_DIR% --source "%BUILDTOOLS_SOURCE%" /p:BuildToolsPackageVersion=%BUILDTOOLS_VERSION% >> "%INIT_TOOLS_LOG%"
57 call "%DOTNET_CMD%" restore "%INIT_TOOLS_RESTORE_PROJECT%" --no-cache --packages %PACKAGES_DIR% --source "%BUILDTOOLS_SOURCE%" /p:BuildToolsPackageVersion=%BUILDTOOLS_VERSION% >> "%INIT_TOOLS_LOG%"
58 if NOT exist "%BUILD_TOOLS_PATH%init-tools.cmd" (
59   echo ERROR: Could not restore build tools correctly. See '%INIT_TOOLS_LOG%' for more details. 1>&2
60   exit /b 1
61 )
62
63 :afterbuildtoolsrestore
64
65 echo Initializing BuildTools...
66 echo Running: "%BUILD_TOOLS_PATH%init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" >> "%INIT_TOOLS_LOG%"
67 call "%BUILD_TOOLS_PATH%init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" >> "%INIT_TOOLS_LOG%"
68 set INIT_TOOLS_ERRORLEVEL=%ERRORLEVEL%
69 if not [%INIT_TOOLS_ERRORLEVEL%]==[0] (
70   echo ERROR: An error occured when trying to initialize the tools. Please check '%INIT_TOOLS_LOG%' for more details. 1>&2
71   exit /b %INIT_TOOLS_ERRORLEVEL%
72 )
73
74 :: Create sempahore file
75 echo Done initializing tools.
76 echo Init-Tools.cmd completed for BuildTools Version: %BUILDTOOLS_VERSION% > "%BUILD_TOOLS_SEMAPHORE%"
77 exit /b 0