I created a new utility called ConfigPoke for creating application configuration files from a template and one or more property files. The property files allow you to create a set of property files in which each subsequent file overrides any settings in the previous.
The input property files are just plain text files with key value pairs, for example:
dbConnectionString = server=localhost;database=Northwind;integrated security=SSPIsessionConnectionString = server=localhost;database=AspState;integrated security=SSPIsmtpServer = localhost
This allow me to define a base set of shared developer properties and then I can define my own property file for me which overrides just properties, like the local database connection string. I also define property files for each of my environments I deploy the application too: dev, test, release, live.
This utility integrates tightly with MSBuild and Visual Studio 2005/2008 using it's own target file. There is also a command line interface for the application that can be used from NAnt or in your deployment process (NSIS in my case).
Per developer configuration happens automatically and is always up to date with the input template and property files with Visual Studio integration. The output configuration files are conditionally built only if the input templates file or input property(s) files are changed, just like any regular source file in Visual Studio. If a Rebuild action is initiated in Visual Studio, the output configuration files are always rebuilt, even if the input files have not changed.
I've been using this utility for the past week at work, and it has worked really well. Edit a property and everything just works. Machine specific settings have proven invaluable especially on my laptop which has a lot of non-standard settings because I usually work disconnected on it.
You can download the MSBuild target DLL, console EXE, and MSBuild targets file from Google Code. The code and the binary is release under the Apache 2.0 license.
Include the Sneal.Build.ConfigPoke.targets file in your Visual Studio project file (csproj) using the following element:
<Import Project="Sneal.Build.ConfigPoke.targets" />
With the ConfigPoke MSBuild targets file included, the configuration building will automatically be hooked into the Visual Studio clean, build, rebuild process.
The Sneal.Build.ConfigPoke.targets file requires two MSBuild item groups as input: ConfigTemplateFiles?, and ConfigPropertyFiles?. These must be specified in your MSBuild (csproj) project file. Each ConfigTemplateFiles? item should be the destination filename + some arbitrary extension, I use '.template'. You may also probably want to set the "InProject?" item meta data to false so these templates or property files don't show up in VS solution explorer.
Here's an example ConfigTemplateFiles? ItemGroup? that you would put into your csproj file:
<ItemGroup> <ConfigTemplateFiles Include="$(MSBuildProjectDirectory)\Configs\web.config.template"/> <ConfigTemplateFiles Include="$(MSBuildProjectDirectory)\Configs\windsor.config.template" /></ItemGroup>
To create user and machine specific overrides, you can include a base properties file and then optionally include a per user and per machine config, if they exist. This would allow each developer to create their own override properties file which can then be optionally checked into source control. To automatically set this up, you can take advantage of the built in MSBuild properties: USERNAME and COMPUTERNAME.
Here's an example ConfigPropertyFiles? ItemGroup? that you would put into your csproj file:
<ItemGroup> <ConfigPropertyFiles Include="$(MSBuildProjectDirectory)\Properties\App.Properties.base"/> <ConfigPropertyFiles Include="$(MSBuildProjectDirectory)\Properties\App.Properties.$(USERNAME)" Condition="Exists('$(MSBuildProjectDirectory)\Properties\App.Properties.$(USERNAME)')"/> <ConfigPropertyFiles Include="$(MSBuildProjectDirectory)\Properties\App.Properties.$(COMPUTERNAME)" Condition="Exists('$(MSBuildProjectDirectory)\Properties\App.Properties.$(COMPUTERNAME)')"/> </ItemGroup>
Note: You cannot pass in non-existant property files to the ConfigPoke utility, hence the Condition check above.
Optionally you can specify a ConfigPokeDirectory? and a ConfigOutputDirectory? property to override the default directories. The ConfigPokeDirectory? property should point to the directory where Sneal.Build.ConfigPoke.MSBuild.dll is located on your machine, if not specified this property will default to the current MSBuild project file directory. The ConfigOutputDirectory? property should point to the directory where the output configuration files are written too. If not specified this property will default to the current MSBuild project file directory.
For additional usage see the example in SVN, http://sneal.googlecode.com/svn/trunk/ConfigPoke/Sneal.Build.ConfigPoke.Example/Sneal.Build.ConfigPoke.Example.csproj
Powered by: newtelligence dasBlog 2.1.8102.813
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2012, Shawn Neal
E-mail