1 @if not defined _echo @echo off
4 set INIT_TOOLS_LOG=%~dp0init-tools.log
5 set PACKAGES_DIR=%~dp0packages\
6 set TOOLRUNTIME_DIR=%~dp0Tools
7 set DOTNET_PATH=%TOOLRUNTIME_DIR%\dotnetcli\
8 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 PROJECT_JSON_PATH=%TOOLRUNTIME_DIR%\%BUILDTOOLS_VERSION%
13 set PROJECT_JSON_FILE=%PROJECT_JSON_PATH%\project.json
14 set PROJECT_JSON_CONTENTS={ "dependencies": { "Microsoft.DotNet.BuildTools": "%BUILDTOOLS_VERSION%" , "Microsoft.DotNet.BuildTools.Coreclr": "1.0.4-prerelease"}, "frameworks": { "dnxcore50": { } } }
15 set BUILD_TOOLS_SEMAPHORE=%PROJECT_JSON_PATH%\init-tools.completed0
16 set TOOLS_INIT_RETURN_CODE=0
18 :: if force option is specified then clean the tool runtime and build tools package directory to force it to get recreated
20 if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
21 if exist "%PACKAGES_DIR%Microsoft.DotNet.BuildTools" rmdir /S /Q "%PACKAGES_DIR%Microsoft.DotNet.BuildTools"
24 :: If sempahore exists do nothing
25 if exist "%BUILD_TOOLS_SEMAPHORE%" (
26 echo Tools are already initialized.
30 if exist "%TOOLRUNTIME_DIR%" rmdir /S /Q "%TOOLRUNTIME_DIR%"
33 if NOT exist "%PACKAGES_DIR%NuGet.exe" (
34 if NOT exist "%PACKAGES_DIR%" mkdir "%PACKAGES_DIR%"
35 powershell -NoProfile -ExecutionPolicy unrestricted -Command "(New-Object Net.WebClient).DownloadFile('https://www.nuget.org/nuget.exe', '%PACKAGES_DIR%NuGet.exe')
38 if NOT exist "%PROJECT_JSON_PATH%" mkdir "%PROJECT_JSON_PATH%"
39 echo %PROJECT_JSON_CONTENTS% > "%PROJECT_JSON_FILE%"
40 echo Running %0 > "%INIT_TOOLS_LOG%"
42 set /p DOTNET_VERSION=< "%~dp0DotnetCLIVersion.txt"
43 if exist "%DOTNET_CMD%" goto :afterdotnetrestore
45 echo Installing dotnet cli...
46 if NOT exist "%DOTNET_PATH%" mkdir "%DOTNET_PATH%"
47 if [%PROCESSOR_ARCHITECTURE%]==[x86] (set DOTNET_ZIP_NAME=dotnet-dev-win-x86.%DOTNET_VERSION%.zip) else (set DOTNET_ZIP_NAME=dotnet-dev-win-x64.%DOTNET_VERSION%.zip)
48 set DOTNET_REMOTE_PATH=https://dotnetcli.blob.core.windows.net/dotnet/Sdk/%DOTNET_VERSION%/%DOTNET_ZIP_NAME%
49 set DOTNET_LOCAL_PATH=%DOTNET_PATH%%DOTNET_ZIP_NAME%
50 echo Installing '%DOTNET_REMOTE_PATH%' to '%DOTNET_LOCAL_PATH%' >> "%INIT_TOOLS_LOG%"
51 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%"
52 if NOT exist "%DOTNET_LOCAL_PATH%" (
53 echo ERROR: Could not install dotnet cli correctly. See '%INIT_TOOLS_LOG%' for more details.
54 set TOOLS_INIT_RETURN_CODE=1
60 if exist "%BUILD_TOOLS_PATH%" goto :afterbuildtoolsrestore
61 echo Restoring BuildTools version %BUILDTOOLS_VERSION%...
62 echo Running: "%DOTNET_CMD%" restore "%PROJECT_JSON_FILE%" --packages "%PACKAGES_DIR% " --source "%BUILDTOOLS_SOURCE%" >> "%INIT_TOOLS_LOG%"
63 call "%DOTNET_CMD%" restore "%PROJECT_JSON_FILE%" --packages "%PACKAGES_DIR% " --source "%BUILDTOOLS_SOURCE%" >> "%INIT_TOOLS_LOG%"
64 if NOT exist "%BUILD_TOOLS_PATH%init-tools.cmd" (
65 echo ERROR: Could not restore build tools correctly. See '%INIT_TOOLS_LOG%' for more details.
66 set TOOLS_INIT_RETURN_CODE=1
70 :afterbuildtoolsrestore
72 echo Initializing BuildTools ...
73 echo Running: "%BUILD_TOOLS_PATH%init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" >> "%INIT_TOOLS_LOG%"
74 call "%BUILD_TOOLS_PATH%init-tools.cmd" "%~dp0" "%DOTNET_CMD%" "%TOOLRUNTIME_DIR%" >> "%INIT_TOOLS_LOG%"
76 echo Updating CLI NuGet Frameworks map...
77 robocopy "%TOOLRUNTIME_DIR%" "%TOOLRUNTIME_DIR%\dotnetcli\sdk\%DOTNET_VERSION%" NuGet.Frameworks.dll /XO >> "%INIT_TOOLS_LOG%"
78 set UPDATE_CLI_ERRORLEVEL=%ERRORLEVEL%
79 if %UPDATE_CLI_ERRORLEVEL% GTR 1 (
80 echo ERROR: Failed to update Nuget for CLI {Error level %UPDATE_CLI_ERRORLEVEL%}. Please check '%INIT_TOOLS_LOG%' for more details. 1>&2
81 exit /b %UPDATE_CLI_ERRORLEVEL%
84 :: Create sempahore file
85 echo Done initializing tools.
86 echo Init-Tools.cmd completed for BuildTools Version: %BUILDTOOLS_VERSION% > "%BUILD_TOOLS_SEMAPHORE%"
90 exit /b %TOOLS_INIT_RETURN_CODE%