Build script for an ASP.NET MVC project

April 18, 2010 20:44

Building a project from command line gives you more control over the build process and provides quite a few other interesting options - comparing to building from Visual Studio.

And recently your humble correspondent was thinking about a "perfect build script" for an ASP.NET MVC project.

What should it have? Here's my take:

  • Option to pre-compile views. Errors in views are not detected until run time but MvcBuildViews property is your compile time friend (pre-compiling the views is not fast, so it's switched off by default).
  • Option to run a clean build. Cached dlls are lurking around every bend so there must be a way to clean up everything. 
  • Non-removable unit tests runner. Even if running all your unit tests takes ages, there must be a set of "smoke" unit tests that verify your basics.


Build script for an ASP.NET MVC project.

Below is a simple script - put it into
build.bat next to the .sln file and enjoy.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Builds all code base.
:: Usage: 
::    build             - incremental build without building MVC views
::    build views       - incremental build with MVC views, to catch runtime errors
::    build clean       - clean build without building MVC views
::    build clean views - clean build with MVC views 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF
 
SETLOCAL
CALL "%VS90COMNTOOLS%\VSVars32.bat"
 
IF NOT _%1==_clean GOTO INCREMENTED
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 'clean' was specified - remove all compiled code 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
msbuild YourSolution.sln  /t:Clean /p:Configuration=Debug /p:BuildConfiguration=Debug
SHIFT
 
:INCREMENTED
   IF NOT _%1==_views GOTO BUILD 
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   :: 'views' was specified - build code and MVC views
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   msbuild YourSolution.sln  /t:Build /p:Configuration=Debug /p:BuildConfiguration=Debug /p:MvcBuildViews=true
   IF %ERRORLEVEL% NEQ 0 GOTO ERROR
   GOTO TESTS
 
:BUILD
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   :: 'views' not specified - build code only
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   msbuild YourSolution.sln /t:Build /p:Configuration=Debug /p:BuildConfiguration=Debug
   IF %ERRORLEVEL% NEQ 0 GOTO ERROR
 
:TESTS
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   :: Build passed ok, now run the tests
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   CALL RunTests.bat console
 
   ENDLOCAL
   GOTO :EOF
 
:ERROR
   ENDLOCAL
   ECHO.
   ECHO :::::::::::::::::::::::::::::::::::::::::::::
   ECHO ::     Uh oh, errors during the build.     ::
   ECHO :::::::::::::::::::::::::::::::::::::::::::::
   PAUSE


Tests script, for any project.

As you can see, tests are run from a different script, defined below:

@ECHO OFF
 
IF _%1 == _console GOTO CONSOLE
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Kick off GUI test runner
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
nunit.exe %~n0.nunit /noload
GOTO :EOF
 
:CONSOLE
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   :: Kick off console test runner
   ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
   nunit-console.exe %~n0.nunit /nologo /noshadow
 

It allows you to kick off the GUI test runner and go through a particular test - while "console" option tests everything from console.

The script uses %~n0 which gives you the current file name - so if the batch file is called
RunTests.bat, make sure you have RunTests.nunit sitting next to it (that guy would contain the test dlls to feed NUnit).

Summary.

That's that - if there's anything you feel could be improved, let me know!


kick it on DotNetKicks.com


Comments

Comments are closed