11@ echo off
22if " %1 " == " /?" goto :usage
33if " %1 " == " -?" goto :usage
4- if " %VSINSTALLDIR% " == " " goto :usage
54
65setlocal enabledelayedexpansion enableextensions
76
@@ -14,6 +13,13 @@ set sample_filter=%3\
1413if " %platform% " == " " set platform = x64
1514if " %configuration% " == " " set configuration = Release
1615
16+ call :EnsureDeveloperCommandPrompt
17+
18+ if " %VSINSTALLDIR% " == " " (
19+ echo Visual Studio Developer Command Prompt not detected and automatic initialization failed.
20+ goto :usage
21+ )
22+
1723if not exist " .\.nuget" mkdir " .\.nuget"
1824if not exist " .\.nuget\nuget.exe" powershell -Command " Invoke-WebRequest https://dist.nuget.org/win-x86-commandline/latest/nuget.exe -OutFile .\.nuget\nuget.exe"
1925
@@ -64,9 +70,46 @@ endlocal
6470
6571goto :eof
6672
73+ :EnsureDeveloperCommandPrompt
74+ if not " %VSINSTALLDIR% " == " " goto :eof
75+
76+ echo Visual Studio Developer Command Prompt not detected. Attempting to initialize automatically...
77+
78+ set " VS_INSTALL_PATH = "
79+ for %%P in (" %ProgramFiles(x86)% \Microsoft Visual Studio\Installer\vswhere.exe" " %ProgramFiles% \Microsoft Visual Studio\Installer\vswhere.exe" ) do (
80+ if exist %%~P (
81+ for /f " usebackq tokens=*" %%I in (`" %%~P " -latest -requires Microsoft.Component.MSBuild -property installationPath`) do (
82+ if not defined VS_INSTALL_PATH (
83+ set " VS_INSTALL_PATH = %%~I "
84+ )
85+ )
86+ if defined VS_INSTALL_PATH goto :VsInstallFound
87+ )
88+ )
89+ goto :EnsureDeveloperCommandPromptEnd
90+
91+ :VsInstallFound
92+ if exist " %VS_INSTALL_PATH% \Common7\Tools\VsDevCmd.bat" (
93+ set " VSDEVCMD_ARGS = -no_logo"
94+ if /i " %platform% " == " x86" set " VSDEVCMD_ARGS = !VSDEVCMD_ARGS! -arch=x86"
95+ if /i " %platform% " == " win32" set " VSDEVCMD_ARGS = !VSDEVCMD_ARGS! -arch=x86"
96+ if /i " %platform% " == " x64" set " VSDEVCMD_ARGS = !VSDEVCMD_ARGS! -arch=x64"
97+ if /i " %platform% " == " arm" set " VSDEVCMD_ARGS = !VSDEVCMD_ARGS! -arch=arm"
98+ if /i " %platform% " == " arm64" set " VSDEVCMD_ARGS = !VSDEVCMD_ARGS! -arch=arm64"
99+ call " %VS_INSTALL_PATH% \Common7\Tools\VsDevCmd.bat" !VSDEVCMD_ARGS!
100+ )
101+
102+ :EnsureDeveloperCommandPromptEnd
103+ if " %VSINSTALLDIR% " == " " (
104+ echo Failed to automatically configure the Visual Studio environment.
105+ echo Please manually run it from a Developer Command Prompt instead.
106+ )
107+ goto :eof
108+
67109:usage
68110echo Usage:
69- echo This script should be run under a Visual Studio Developer Command Prompt.
111+ echo This script tries to initialize a Visual Studio Developer Command Prompt.
112+ echo If that fails, please manually run it from a Developer Command Prompt instead.
70113echo .
71114echo build.cmd [Platform] [Configuration] [Sample]
72115echo .
0 commit comments