Create bx.cmd (#2168)

* Try createing a script to only build the current working directory

  Inspired by #2078.

  I wanted to use this for WindowsTerminal, but I can't generate the
  resources.pri from just building WindowsTerminal. Maybe @dhowett-msft has
  some ideas.

* Cleanup for PR

* fix some bugs with building outside a project directory.

* PR nits
This commit is contained in:
Mike Griese 2019-08-05 20:18:40 -05:00 committed by GitHub
parent 4529e46d3e
commit b495ad255f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 12 deletions

7
tools/bcx.cmd Normal file
View File

@ -0,0 +1,7 @@
@echo off
rem bcx - Build only the project in this directory, cleaning it first.
rem This is another script to help Microsoft developers feel at home working on
rem the terminal project.
call bcz exclusive %*

View File

@ -1,13 +1,26 @@
@echo off
rem bcz - Clean and build the project
rem This is another script to help Microsoft developers feel at home working on the openconsole project.
rem bcz - Clean and build the solution.
rem This is another script to help Microsoft developers feel at home working on the Terminal project.
rem Args:
rem dbg: manually build the solution in the Debug configuration. If omitted,
rem we'll use whatever the last configuration build was.
rem rel: manually build the solution in the Release configuration. If
rem omitted, we'll use whatever the last configuration build was.
rem no_clean: Don't clean before building. This is a much faster build
rem typically, but leaves artifacts from previous builds around, which
rem can lead to unexpected build failures.
rem exclusive: Only build the project in the cwd. If omitted, we'll try
rem building the entire solution instead.
if (%_LAST_BUILD_CONF%)==() (
set _LAST_BUILD_CONF=%DEFAULT_CONFIGURATION%
)
set _MSBUILD_TARGET=Clean,Build
set _EXCLUSIVE=
set _APPX_ARGS=
:ARGS_LOOP
if (%1) == () goto :POST_ARGS_LOOP
@ -22,30 +35,43 @@ if (%1) == (rel) (
if (%1) == (no_clean) (
set _MSBUILD_TARGET=Build
)
if (%1) == (exclusive) (
set _EXCLUSIVE=1
)
shift
goto :ARGS_LOOP
:POST_ARGS_LOOP
echo Starting build...
nuget.exe restore %OPENCON%\OpenConsole.sln
rem /p:AppxBundle=Never prevents us from building the appxbundle from the commandline.
rem We don't want to do this from a debug build, because it takes ages, so disable it.
rem if you want the appx, build release
if "%_EXCLUSIVE%" == "1" (
set "PROJECT_NAME="
call :get_project
) else if (%_LAST_BUILD_CONF%) == (Debug) (
set _APPX_ARGS=
rem /p:AppxBundle=Never prevents us from building the appxbundle from the
rem commandline. We don't want to do this from a debug build, because it
rem takes ages, so disable it. if you want the appx, build release
rem Only do this check if we're doing a full solution build. If we're only
rem trying to build the appx, then we obviously want to build the appx.
if (%_LAST_BUILD_CONF%) == (Debug) (
echo Skipping building appx...
set _APPX_ARGS=/p:AppxBundle=false
) else (
echo Building Appx...
)
if "%_EXCLUSIVE%" == "1" (
if "%PROJECT_NAME%" == "" ( goto :eof ) else echo Building only %PROJECT_NAME%
)
echo Performing nuget restore...
nuget.exe restore %OPENCON%\OpenConsole.sln
set _BUILD_CMDLINE="%MSBUILD%" %OPENCON%\OpenConsole.sln /t:%_MSBUILD_TARGET% /m /p:Configuration=%_LAST_BUILD_CONF% /p:Platform=%ARCH% %_APPX_ARGS%
echo %_BUILD_CMDLINE%
echo Starting build...
%_BUILD_CMDLINE%
rem Cleanup unused variables here. Note we cannot use setlocal because we need to pass modified
@ -53,3 +79,48 @@ rem _LAST_BUILD_CONF out to OpenCon.cmd later.
rem
set _MSBUILD_TARGET=
set _BIN_=%~dp0\bin\%PLATFORM%\%_LAST_BUILD_CONF%
goto :eof
rem ############################################################################
rem The code to figure out what project we're building needs to be in its own
rem function. Otherwise, when cmd evaluates the if statement above `if
rem "%_EXCLUSIVE%" == "1"`, it'll evaluate the entire block with the value of
rem the the variables at the time the if was executed. So instead, make a
rem function here with `enabledelayedexpansion` set.
:get_project
setlocal enabledelayedexpansion
rem TODO:GH#2172 Find a way to only rebuild the metaproj if the sln changed
rem First generate the metaproj file
set MSBuildEmitSolution=1
"%msbuild%" %OPENCON%\OpenConsole.sln /t:ValidateSolutionConfiguration /m > NUL
set MSBuildEmitSolution=
rem Use bx.ps1 to figure out which target we're looking at
set _BX_SCRIPT=powershell bx.ps1
set _OUTPUT=
FOR /F "tokens=* USEBACKQ" %%F IN (`powershell bx.ps1 2^> NUL`) DO (
set _OUTPUT=%%F
)
if "!_OUTPUT!" == "" (
echo Could not find a .vcxproj file in this directory.
echo `bx.cmd` only works in directories with a vcxproj file.
echo Please navigate to directory with a project file in it, or try `bz` to build the entire solution.
goto :eof
)
set "__PROJECT_NAME=!_OUTPUT!"
rem If we're trying to clean build, make sure to update the target here.
if "%_MSBUILD_TARGET%" == "Build" (
set __MSBUILD_TARGET=%__PROJECT_NAME%
) else if "%_MSBUILD_TARGET%" == "Clean,Build" (
set __MSBUILD_TARGET=%__PROJECT_NAME%:Rebuild
)
rem This statement will propogate our internal variables up to the calling
rem scope. Because they're all on one line, the value of our local variables
rem will be evaluated before we endlocal
endlocal & set "PROJECT_NAME=%__PROJECT_NAME%" & set "_MSBUILD_TARGET=%__MSBUILD_TARGET%"
rem ############################################################################
:eof

7
tools/bx.cmd Normal file
View File

@ -0,0 +1,7 @@
@echo off
rem bx - Build only the project in this directory without cleaning it first.
rem This is another script to help Microsoft developers feel at home working on
rem the terminal project.
call bcz exclusive no_clean %*

28
tools/bx.ps1 Normal file
View File

@ -0,0 +1,28 @@
# This is a helper script to figure out which target corresponds to the project
# in this directory. Parses the solution's .metaproj file looking for the
# project file in this directory, to be able to get the project's name.
$projects = Get-Childitem -Path .\ -Filter *.vcxproj -File
if ($projects.length -eq 0)
{
exit -1
}
$projectPath = $projects.FullName
$msBuildCondition = "'%(ProjectReference.Identity)' == '$projectPath.metaproj'"
# Parse the solution's metaproj file.
[xml]$Metaproj = Get-Content "$env:OPENCON\OpenConsole.sln.metaproj"
$targets = $Metaproj.Project.Target
# Filter to project targets that match out metaproj file.
# For Conhost\Server, this will match:
# [Conhost\Server, Conhost\Server:Clean, Conhost\Server:Rebuild, Conhost\Server:Publish]
$matchingTargets = $targets | Where-Object { $_.MSBuild.Condition -eq $msBuildCondition }
# Further filter to the targets that dont have a suffix (like ":Clean")
$matchingTargets = $matchingTargets | Where-Object { $hasProperty = $_.MsBuild.PSobject.Properties.name -match "Targets" ; return -Not $hasProperty }
Write-Host $matchingTargets.Name
exit 0

View File

@ -1,6 +1,7 @@
@echo off
rem bcz - Build the project without clean it first
rem This is another script to help Microsoft developers feel at home working on the openconsole project.
rem bz - Build the entire solution without cleaning it first.
rem This is another script to help Microsoft developers feel at home working on
rem the terminal project.
call bcz no_clean %*