Name:
Location: Rowland Heights, California, United States

Wednesday, February 02, 2005

Configuration Setting for Dev and Live Environments

Today I found an interesting event in VS.NET 2003, it's called Post Build Event. In it you can place script code that you would normally put in a batch file.

In our ASP.NET Application we have a virtual directory called config where we keep all our xml file with configuration settings... much like the machine.config, app.config or web.config files. However, we were looking for a way to store simple strings and flags that would differ between a dev environment and live environment and this was the solution.

When we started building Windows Applications we felt the need for a "virtual directory" in the Windows App but as we all know there isn't any!

That's when we found the Post Build Event!

In our project we have a folder call config which has two other folders called dev and live. They contain the xml files with simple strings and flags for our application. Each has settings that are appropriate for dev and live respectively.

What we needed was a way to copy the xml files from

%PROJECTDIR%\config\dev to %PROJECTDIR%\bin\debug\config

if we were building debug version of our application and

%PROJECTDIR%\config\live to %PROJECTDIR%\bin\release\config

if we were building a release version of our application. Another thing we found interesting was environment variables that were set in the Post Build Event Command Line. Variables such as:

ConfigurationName
TargetDir
ProjectDir
TargetPath
ProjectPath
etc...

Granted you have to build ugly script files like those you would when building batch files (i.e. autoexec.bat, boot.bat, dos.bat, etc... you get the picture) but we can at least determine when we were building a debug or release app.

Soooo, this is what our batch file look like when we were done:

rem This represent the begining of the bat file
:start
rem If you set your build to debug here's what it'll look like
rem you may have to set the name accordingly if you have a
rem different configuration name. By default you have
rem Debug and Release.
if "$(ConfigurationName)" == "Debug" goto :debug

:live
rem We use xcopy with options for copying our live config files
rem /s - Copy directory structure
rem /e - Copy directory even if empty
rem /y - Over write existing files
xcopy $(ProjectDir)config\live\*.* $(TargetDir)config\ /s/e/y
goto :end

:debug
rem /s - Copy directory structure
rem /e - Copy directory even if empty
rem /y - Over write existing files
xcopy $(ProjectDir)config\dev\*.* $(TargetDir)config\ /s/e/y
goto :end

:end
rem Simply bring the line of execution to here to indicate the EOF
rem The script will end since there are no more lines to execute.

One thing to remember is that this is only available in VS.NET 2003 (maybe 2002, too) but definately not 2001. And, another reason for choosing C# is that this option is not available for VB.NET no matter what version of VS.NET you have. Sorry, VB Peeps!

I hope this helps someone when needing to cover your bases for both dev and live environments!

0 Comments:

Post a Comment

<< Home