Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making pre- and post-build event scripts pretty?

I have some moderately hefty pre- and post-build event scripts for my Visual Studio 2008 projects (actually it's mainly post-build event scripts). They work OK in that they function correctly and when I exit 0 the build succeeds and when I exit 1 the build fails with an error. However, that error is enormous, and goes something like this:

The command "if Release == Debug goto Foo
if Release == Release goto Bar
exit 0

:Foo
mkdir "abc"
copy "$(TargetDir)file.dll" "abc"
[...]
" exited with code 1.

You get the idea. The entire script is always dumped out as part of the error description. The entire script is also dumped out in the Output window while the build is happening, too. So, why have I seen various references on the web to using echo in these scripts? For example, here's part of an example on one particular site:

:BuildEventFailed
echo POSTBUILDSTEP for $(ProjectName) FAILED
exit 1
:BuildEventOK
echo POSTBUILDSTEP for $(ProjectName) COMPLETED OK

Is there a way to get Visual Studio to suppress all script output apart from what is echoed (and therefore using echo only to output what you want would make sense), or are these examples just misguided and they don't realize that the whole script is always dumped out?

like image 474
Jez Avatar asked Jun 07 '11 15:06

Jez


1 Answers

@Jez your answer is great, but i would improve the Batch file as follow: By using %~1 it will remove quotes from args more easily:

@echo off
if "%~1"=="" (set ErrorMessage=Parameter 1 missing. It has to be the Config name. & GOTO :EndError)
if "%~2"=="" (set ErrorMessage=Parameter 2 missing. It has to be the Project name. & GOTO :EndError)
if "%~3"=="" (set ErrorMessage=Parameter 3 missing. It has to be the Targetdir. & GOTO :EndError)
if "%~4"=="" (set ErrorMessage=Parameter 4 missing. It has to be the Projectdir. & GOTO :EndError)

cd /D "%~dp0"  &:: setting current directory to there where the batchfile is.

:: With ~ the quotes are removed
set _configName=%~1 
set _projectName=%~2
set _targetDir=%~3
set _projectDir=%~4
set _outDir=%~5

rd Plugins\%_projectName% /S /Q
if errorlevel 1 (set ErrorMessage=remove directory failed!!. & GOTO :EndError)
md Plugins\%_projectName%
if errorlevel 1 (set ErrorMessage=make directory failed!!. & GOTO :EndError)
xcopy %_targetDir%* Plugins\%_projectName% /E /Q
if errorlevel 1 (set ErrorMessage=Copy failed!!. & GOTO :EndError)

exit 0

:EndError
echo --! Error in %~nx0 !-- 
echo %ErrorMessage%
pause
exit 1
like image 196
Apfelkuacha Avatar answered Oct 27 '22 00:10

Apfelkuacha