3
\$\begingroup\$

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?

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Feb 22, 2014 at 20:30
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to Code Review! It looks like you have figured out exactly what this site is about. You just passed the "first post" test with ease! \$\endgroup\$ Commented Feb 22, 2014 at 20:48
  • 1
    \$\begingroup\$ @Simon André Forsberg - Thanks, I'm glad that Code Review exists. \$\endgroup\$ Commented Feb 22, 2014 at 20:55

2 Answers 2

2
\$\begingroup\$

You probably meant set %BUILDFAILEDxx%=1? /p is for prompting interactively (or reading from a file).

Otherwise, except a minor typo %BUILDFAILED32% instead of just %FAILED32% (and same for 64bit builds), this looks good (and "clean") enough for me.

You could get rid of the parentheses (for the sake of it?) if you wrote each if on a single line.

Jamal
35.2k13 gold badges134 silver badges238 bronze badges
answered Feb 22, 2014 at 21:15
\$\endgroup\$
2
  • \$\begingroup\$ @Jamal oops, thanks for the "codifying", I'll look up how to do it properly. As for the space before the question mark, sorry I'm French :-) so I may do it again. \$\endgroup\$ Commented Feb 22, 2014 at 21:24
  • \$\begingroup\$ Did you expect more ? Apart from reorganizing the order of builds (either by number 32b-1,64b-1,32b-2,... or by regrouping all the 32 and 64bit ones) I think this is the optimal & "cleanest" solution (with environment variables, in cmd). \$\endgroup\$ Commented Feb 23, 2014 at 9:23
4
\$\begingroup\$

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
answered Apr 17, 2014 at 4:30
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.