Faster feeback loop for parameterization

These days one of the things I focus on most is the feedback loop for the teams I work with and support.  In the past we just lived with the fact some code couldn’t be tested or tested easily and would waste untold hours in all the ceremony of deploying it out to a server environment just to find it didn’t work.  Now we try to shorten that feedback as much as practical.

We had a case this week where a developer was setting up WebDeploy Parameterization in his project for the first time and didn’t know how to test it without kicking off a build/deployment.  This took 5-10 minutes everytime he made a change and wanted to test it.  Thankfully he asked how this could be improved.  My first response was to use the Parameterization Preview Visual Studio plugin which mimics the old config transform preview option but for parameterization files (blogged about that here).

Unfortunately the developer was working on an older VS2010 project which the plugin doesn’t support.  Obviously, we need to update that project but it that doesn’t solve his immediate problem.  So I decided to invest an hour on a PowerShell script to test parameterization.  It basically performs the same steps that the plugin does but is much easier to understand so I decided to share it for two reasons:

  • Maybe others can use it for older projects or cases where the plugin won’t work
  • This is a great opporturnity for others to understand how parameterization works and how they can test it.

As I’ve mentioned previously, most .NET developers don’t know the gem they have in MSDeploy/WebDeploy/Parameterization/etc.

So here is the script:

$msdeployPath = ‘C:Program FilesIISMicrosoft Web Deploy V3msdeploy.exe’
$diffTool = ‘C:Program Files (x86)Beyond Compare 3bcompare.exe’
$workingDir = (Resolve-Path .).Path + "_temp"

$sourceDir =  $workingDir +  "source"
$parameterizationFilesDir = $workingDir + "parameterizationFiles"
$destinationDir = $workingDir + "dest"
$packageDir = $workingDir + "package"
$package = $packageDir + "package.zip"
$parametersFile = $parameterizationFilesDir + "parameters.xml"

remove-item $workingDir -Force -Recurse
new-item $workingDir -type directory
new-item $sourceDir -type directory
new-item $destinationDir -type directory
new-item $packageDir -type directory
new-item $parameterizationFilesDir -type directory

$targetConfig = read-host "What is the full path to the config file you would like to test parameterization against?"
$targetConfigFilename = (split-path $targetConfig -Leaf)
$environment = read-host "What environment (DEV, QA, etc) would you like to test?"

$setParametersFile = $parameterizationFilesDir + "setParameters." + $environment + ".xml"

& xcopy $targetConfig $sourceDir
& xcopy "$(split-path $targetConfig -parent)*param*.xml" $parameterizationFilesDir
& $msdeployPath -verb:sync -source:dirPath=$sourceDir -dest:package=$package -declareParamFile:"$parametersFile"
& $msdeployPath -verb:sync -source:package=$package -dest:dirPath=$destinationDir -setParamFile:"$setParametersFile" -verbose
& "$diffTool" "$sourceDir$targetConfigFilename" "$destinationDir$targetConfigFilename"

Setup

First we set some folder paths and command that the user might want to change.  Given more time I would have make this a proper PowerShell function with arguments but I’m still learning that stuff.

image

Next we cleanup our work space by deleteing the old and recreating.

image

Inputs

Then we ask the user what config they want to parameterize and what target configuration they want to apply.

image

Processing and Outputs

Finally we create a MSDeploy package which contains only the config file because the other project files aren’t needed.  Notice we specify the parameters.xml file in the declareParamFile attribute. Then we deploy that package to a working directory with the setParamFile set to the appropriate environment.  Lastly we open the original config and the parameterized config in a diff tool for comparison.

image

Assumptions

Obviously I made some assumptions for this script that may not apply to other uers which may require further edits to the script:

  • The set parameters file uses the format “SetParameters.[config].xml”
  • You are only parameterizing a single file.  One of the benefits of Parameterization over config transforms you can run them against any text file or the deployment itself.
  • Your parameterization files live in the same directory as your config file.
  • You parameterization declaration file is name “parameters.xml”.

Please let me know if this is helpful to you or if there are other topics you would like me to blog about.

Leave a Reply