1- :: Optional batch file to quickly build with some defaults.
2- :: Alternatively, this batch file can be invoked passing msbuild parameters, like: build.cmd "/v:detailed" "/t:Rebuild"
1+ @ echo off
2+ setlocal enabledelayedexpansion
33
4- @ ECHO OFF
4+ set BatchFile = %0
5+ set Root = %~dp0
6+ set BuildConfiguration = Debug
7+ set MSBuildTarget = All
8+ set NodeReuse = true
9+ set MultiProcessor = /m
510
6- :: Ensure MSBuild can be located. Allows for a better error message below.
7- where msbuild > %TEMP% \msbuild.txt
8- set /p msb = < %TEMP% \msbuild.txt
11+ :ParseArguments
12+ if " %1 " == " " goto :DoneParsing
13+ if /I " %1 " == " /?" call :Usage && exit /b 1
14+ if /I " %1 " == " /debug" set BuildConfiguration = Debug&& shift&& goto :ParseArguments
15+ if /I " %1 " == " /release" set BuildConfiguration = Release&& shift&& goto :ParseArguments
16+ if /I " %1 " == " /build" set MSBuildTarget = Build&& shift&& goto :ParseArguments
17+ if /I " %1 " == " /rebuild" set MSBuildTarget = Rebuild&& shift&& goto :ParseArguments
18+ if /I " %1 " == " /package" set MSBuildTarget = Package&& shift&& goto :ParseArguments
19+ if /I " %1 " == " /test" set MSBuildTarget = Test&& shift&& goto :ParseArguments
20+ if /I " %1 " == " /restore" set MSBuildTarget = Restore&& shift&& goto :ParseArguments
21+ if /I " %1 " == " /no-node-reuse" set NodeReuse = false&& shift&& goto :ParseArguments
22+ if /I " %1 " == " /no-multi-proc" set MultiProcessor = && shift&& goto :ParseArguments
23+ MSBuildAdditionalArguments=" %1 %MSBuildAdditionalArguments"% &&shift&& goto :ParseArguments
24+ :DoneParsing
925
10- IF " %msb% " == " " (
11- echo Please run %~n0 from a Visual Studio Developer Command Prompt.
12- exit /b -1
26+ :: Detect if MSBuild is in the path
27+ for /f " delims=" %%i in ('where msbuild') do set " MSBuildPath = %%i " & goto :MSBuildPathDone
28+ :MSBuildPathDone
29+
30+ if not exist " %MSBuildPath% " (
31+ call :PrintColor Red " To build this repository, MSBuild.exe must be in the PATH."
32+ echo MSBuild is included with Visual Studio 2017 or later.
33+ echo .
34+ echo If Visual Studio is not installed, visit this page to download:
35+ echo .
36+ echo https://www.visualstudio.com/vs/
37+ echo .
38+ exit /b 1
1339)
1440
15- SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
41+ :: Detect MSBuild version >= 15
42+ for /f " delims=" %%i in ('msbuild -nologo -version') do set MSBuildFullVersion = %%i
43+ for /f " delims=. tokens=1" %%a in (" %MSBuildFullVersion% " ) do (
44+ set MSBuildMajorVersion = %%a
45+ )
1646
17- IF EXIST .nuget\nuget.exe goto restore
18- IF NOT EXIST .nuget md .nuget
19- echo Downloading latest version of NuGet.exe...
20- @ powershell -NoProfile -ExecutionPolicy RemoteSigned -Command " $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' -OutFile .nuget/nuget.exe"
47+ if %MSBuildMajorVersion% LSS 15 (
48+ call :PrintColor Red " To build this repository, the MSBuild.exe in the PATH needs to be 15.0 or higher."
49+ echo MSBuild 15.0 is included with Visual Studio 2017 or later.
50+ echo .
51+ echo If Visual Studio is not installed, visit this page to download:
52+ echo .
53+ echo https://www.visualstudio.com/vs/
54+ echo .
55+ echo Located MSBuild in the PATH was " %MSBuildPath% " .
56+ exit /b 1
57+ )
2158
22- :restore
23- :: Build script packages have no version in the path, so we install them to .nuget\packages to avoid conflicts with
24- :: solution/project packages.
25- IF NOT EXIST packages.config goto run
26- .nuget\nuget.exe install packages.config -OutputDirectory .nuget\packages -ExcludeVersion -Verbosity quiet
59+ :: Ensure developer command prompt variables are set
60+ if " %VisualStudioVersion% " == " " (
61+ for /f " delims=" %%i in ('msbuild build.props -nologo /t:GetVsInstallRoot') do set " VsInstallRoot = %%i " & goto :VsInstallRootDone
62+ :VsInstallRootDone
63+ for /f " tokens=* delims= " %%i in (" %VsInstallRoot% " ) do set " VsInstallRoot = %%i "
64+ set " DeveloperCommandPrompt = %VsInstallRoot% \Common7\Tools\VsDevCmd.bat"
65+ if not exist " %DeveloperCommandPrompt% " (
66+ call :PrintColor Red " Failed to locate 'Common7\Tools\VsDevCmd.bat' under the reported Visual Studio installation root '%VsInstallRoot% '."
67+ echo .
68+ echo If Visual Studio is not installed, visit this page to download:
69+ echo .
70+ echo https://www.visualstudio.com/vs/
71+ echo .
72+ exit /b 1
73+ )
74+ call " %DeveloperCommandPrompt% " || goto :BuildFailed
75+ )
2776
28- :run
29- IF " %Verbosity% " == " " (
30- set Verbosity = minimal
77+ msbuild /nologo /nodeReuse:%NodeReuse% /t:" %MSBuildTarget% " /p:Configuration=" %BuildConfiguration% " %MSBuildAdditionalArguments%
78+ if ERRORLEVEL 1 (
79+ echo .
80+ call :PrintColor Red " Build failed, for full log see msbuild.log."
81+ exit /b 1
3182)
3283
33- " %msb% " build.proj /v:%Verbosity% %1 %2 %3 %4 %5 %6 %7 %8 %9
84+ echo .
85+ call :PrintColor Green " Build completed successfully, for full log see msbuild.log"
86+ exit /b 0
87+
88+ :Usage
89+ echo Usage: %BatchFile% [/build^ |/rebuild^ |/test^ |/restore^ ] [/debug^ |/release] [/no-node-reuse] [/no-multi-proc]
90+ echo .
91+ echo Build targets:
92+ echo /build Perform build
93+ echo /rebuild Perform a clean, then build
94+ echo /package Packages the product
95+ echo /test Builds and runs tests only
96+ echo /restore Only restore NuGet packages
97+ echo .
98+ echo Build options:
99+ echo /debug Perform debug build (default)
100+ echo /release Perform release build
101+ echo /no-node-reuse Prevents MSBuild from reusing existing MSBuild instances,
102+ echo useful for avoiding unexpected behavior on build machines
103+ echo /no-multi-proc No multi-proc build, useful for diagnosing build logs
104+ goto :eof
105+
106+ :BuildFailed
107+ call :PrintColor Red " Build failed with ERRORLEVEL %ERRORLEVEL% "
108+ exit /b 1
109+
110+ :PrintColor
111+ " %Windir% \System32\WindowsPowerShell\v1.0\Powershell.exe" -noprofile write-host -foregroundcolor %1 " '%2 '"
0 commit comments