Google
WWW Yariv Hammer's Code Site

Thursday, October 27, 2005

Create a Script to Build .NET Projects

Introduction
Sometimes we need to use command line in order to compile our C# or VB.NET projects. This is very useful when we have a big project with a lot of solutions. Compiling each manually can be vary tedious.
I want a solution that will not require me to use csc (C# compiler) because I don't know all the flags that the VS IDE uses when it builds my project.

Using The IDE From Command Line
I found out that I can tell VS.NET to build my project/solution in command line using the following statement:
devenv mySolution.sln /rebuild debug
or
devenv mySolution.sln /build release

It will build my solution as if I click the Build button in the menu inside the IDE. This is exactly what I need - I will set the project properties in the comfortable project properties window, and then run a script that will build everything for me, and get an extra bonus: It can copy stuff around, run executables, check things in between the builds.

Creating the Script
The script is actually a bat file. Here is a template you can use for your project:
-----------------------------------------------

@echo off
set visualStudioDir=C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\IDEset solutionDir="."
set buildType=Debug
set returnErrorCode=true
set pause=true

if "%1"=="/?" goto HELP
if not "%1"=="" set solutionDir=%1
if "%1"=="" set solutionDir="C:\"
if not Exist "%visualStudioDir%..\Tools\vsvars32.bat" goto HELPVS

set savedPrompt=%prompt%
set prompt=*$g
@call "%visualStudioDir%..\Tools\vsvars32.bat"

GenerateSolution:
set solutionName= MySolutionName
set solutionPath= RelativePathToSolution

pushd %solutionDir%\%RelativePathToSolution%\
if not Exist %solutionDir%\%solutionPath%\%solutionName%.sln goto ErrorCouldNotFindSolution
devenv %solutionName%.sln /build %buildType%
@if errorlevel 1 goto :error
popd
@goto :exit

:error
if %returnErrorCode%==false goto exit
@ECHO An error occured in BuildLibrary.bat - %errorLevel%
if %pause%==true PAUSE
@exit errorLevel

:ErrorCouldNotFindSolution
echo Could not find %solutionDir%\%solutionPath%\%solutionName%.sln
@goto :exit

:HELPVS
echo Error: Unable to locate Visual Studio.NET
echo.
echo This batch assumes Visual Studio.NET 2003 has been installed in its default location
echo ("%visualStudioDir%".)
echo.
echo If you have installed Visual Studio.NET 2003 to a different location, you will need echo to update this batch file to reflect that location.
echo.
goto exit

:HELP
echo Usage: BatchBuild [path]
echo.
echo BatchBuild will build the solution that is located in path.
echo.

goto exit
:exit
if %pause%==true PAUSE
set pause=
set solutionDir=
set buildType=
set returnErrorCode=
set prompt=%savedPrompt%
set savedPrompt=
set solutionName=
set solutionPath=
echo on

---------------------------------------
The emphasized part is the one that builds your solution. Change only what in the red section: Customize it by changing the tag (GeneratedSolution:), the solutionName and solutionPath. You can duplicate it if you need to build several solutions.
Usage:
Script.bat /? - Shows help
Script.bat C:\ - Will start all compilations from this directory (useful when you have few versions of same project).

0 Comments:

Post a Comment

<< Home

Feel free to use everything here. Add links to my site if you wish.

Do not copy anything to other sites without adding link to here.

All the contents of the site belong to Yariv Hammer.