HiveBrain v1.2.0
Get Started
← Back to all entries
debugMinor

Msbuild batch - if any build fails than skip the last one

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
lastfailsthemsbuildanythanbatchskiponebuild

Problem

I have this batch that builds some projects using msbuild.

What I want to do is to skip building my-last.proj if any of the above builds fail: my-proj1.proj to my-proj5.proj using environment variables.

I'm sure that there is another cleaner way to do this, but so far I just can't figure it out.

msbuild /target:Win32 my-proj1.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)
msbuild /target:X64 my-proj1.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)

msbuild /target:ManagedWin32 my-proj1.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)
msbuild /target:ManagedX64 my-proj1.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)

msbuild /target:Win32 my-proj2.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)
msbuild /target:X64 my-proj2.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)

msbuild /target:Win32 my-proj3.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)
msbuild /target:Win32 my-proj4.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)
msbuild /target:Win32 my-proj5.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED32=1
)

IF NOT "%FAILED32%" == "1" (
msbuild /target:setup32 my-last.proj
)

msbuild /target:X64 my-proj3.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)
msbuild /target:X64 my-proj4.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)
msbuild /target:X64 my-proj5.proj
IF NOT ERRORLEVEL 0 ( 
    set /p BUILDFAILED64=1
)

IF NOT "%FAILED64%" == "1" (
msbuild /target:setup64 my-last.proj
)


Does anyone have any ideas?

Solution

You probably want to localize any environment changes.

You should initialize your variables to a known state at the beginning, otherwise you could get the wrong result.

You have a lot of redundant code that can be eliminated by using a CALLed subroutine

You can use the || operator to conditionally execute code if the previous command failed. I find it simpler than using IF ERRORLEVEL.

You can save a bit of typing by storing code in a variable to be used as a simple macro.

@echo off
setlocal
set "failed32="
set "failed64="
set "build=call :build"

%build% Win 32 my-proj1
%build% X 64 my-proj1
%build% ManagedWin 32 my-proj1
%build% ManagedX 64 my-proj1
%build% Win 32 my-proj2
%build% X 64 my-proj2
%build% Win 32 my-proj3
%build% Win 32 my-proj4
%build% Win 32 my-proj5

if not defined failed32 msbuild /target:setup32 my-last.proj

%build% X 64 my-proj3
%build% X 64 my-proj4
%build% X 64 my-proj5

if not defined failed64 msbuild /target:setup64 my-last.proj
exit /b

:build target size proj
msbuild /target:%1%2 %3.proj || set failed%%2=1
exit /b

Context

StackExchange Code Review Q#42536, answer score: 4

Revisions (0)

No revisions yet.