andrew4582

ConfigurationMerger

Sep 25th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using Microsoft.Build.Framework;
  5. using Microsoft.Build.Utilities;
  6. using System.IO;
  7.  
  8.  
  9. namespace MergeConfigTask {
  10.  
  11.     public class ConfigurationMerger {
  12.  
  13.         public List<string> ErrorList { get; private set; }
  14.         public List<string> InfoLog { get; private set; }
  15.        
  16.        
  17.         string _sourceConfigFilename;
  18.         string _targetConfigFilename;
  19.         string _outputConfigFilename;
  20.  
  21.         /// <summary>
  22.         /// Gets or sets the filename of the source config file.
  23.         /// </summary>
  24.         public string SourceConfigFilename {
  25.             get { return _sourceConfigFilename; }
  26.             set { _sourceConfigFilename = value; }
  27.         }
  28.  
  29.         /// <summary>
  30.         /// Gets or sets the filename of the target config file.
  31.         /// </summary>
  32.         public string TargetConfigFilename {
  33.             get { return _targetConfigFilename; }
  34.             set { _targetConfigFilename = value; }
  35.         }
  36.  
  37.         /// <summary>
  38.         /// Gets or sets the filename of the output config file that is changed.
  39.         /// </summary>
  40.         public string OutputConfigFilename {
  41.             get { return _outputConfigFilename; }
  42.             set { _outputConfigFilename = value; }
  43.         }
  44.        
  45.         public ConfigurationMerger() {
  46.             ErrorList = new List<string>();
  47.             InfoLog = new List<string>();
  48.         }
  49.  
  50.         /// <summary>
  51.         /// When overridden in a derived class, executes the task.
  52.         /// </summary>
  53.         /// <returns>
  54.         /// true if the task successfully executed; otherwise, false.
  55.         /// </returns>
  56.         public bool Execute() {
  57.             ErrorList.Clear();
  58.             InfoLog.Clear();
  59.  
  60.             try {
  61.  
  62.                 XmlDocument source = LoadConfigFile(SourceConfigFilename);
  63.                 XmlDocument target = LoadConfigFile(TargetConfigFilename);
  64.                
  65.                 if(!string.IsNullOrEmpty(OutputConfigFilename)) {
  66.                     File.Copy(TargetConfigFilename,OutputConfigFilename,true);
  67.                     //File.WriteAllText(OutputConfigFilename,File.ReadAllText(TargetConfigFilename));
  68.                 }
  69.                 SetCompilatationDebug(source,target);
  70.                 SetCustomErrorsMode(source,target);
  71.                 SetAppSettings(source,target);
  72.                 SetConnectionStrings(source,target);
  73.  
  74.                 if(string.IsNullOrEmpty(OutputConfigFilename)) {
  75.                     target.Save(TargetConfigFilename);
  76.                     InfoLog.Add(string.Format("Saved transformed file to {0}",TargetConfigFilename));
  77.                 }
  78.                 else {
  79.                    
  80.                     target.Save(OutputConfigFilename);
  81.                     InfoLog.Add(string.Format("Saved transformed file to {0}",OutputConfigFilename));
  82.                 }
  83.             }
  84.             catch(Exception err) {
  85.                 ErrorList.Add(err.Message);
  86.             }
  87.             return ErrorList.Count == 0;
  88.         }
  89.          
  90.         XmlDocument LoadConfigFile(string filename) {
  91.             XmlDocument configFile = new XmlDocument();
  92.             configFile.Load(filename);
  93.             return configFile;
  94.         }
  95.  
  96.         void SetCompilatationDebug(XmlDocument source,XmlDocument target) {
  97.             SetNodeValue(source,target,"configuration/system.web/compilation/@debug","the compilation node's debug attribute");
  98.         }
  99.  
  100.         void SetCustomErrorsMode(XmlDocument source,XmlDocument target) {
  101.             SetNodeValue(source,target,"configuration/system.web/customErrors/@mode","the customErrors node's mode attribute");
  102.         }
  103.  
  104.         void SetNodeValue(XmlDocument source,XmlDocument target,string xpath,string nodeDescription) {
  105.             // Get the value of the source node.
  106.             XmlNode sourceNode = source.SelectSingleNode(xpath);
  107.             if(sourceNode == null) {
  108.                 ErrorList.Add(string.Format("{0} could not be found in the source config file.",nodeDescription));
  109.                 return;
  110.             }
  111.             string value = sourceNode.Value;
  112.             InfoLog.Add(string.Format("{0} was found in the source config file, with a value of '{1}'.",nodeDescription,value));
  113.  
  114.             // Set the value of the target node.
  115.             XmlNode targetNode = target.SelectSingleNode(xpath);
  116.             if(targetNode == null) {
  117.                 ErrorList.Add(string.Format("{0} could not be found in the target config file. It will not be added.",nodeDescription));
  118.                 return;
  119.             }
  120.             targetNode.Value = sourceNode.Value;
  121.             InfoLog.Add(string.Format("The value of {0} was updated successfully to '{1}'.",nodeDescription,value));
  122.         }
  123.  
  124.         void SetAppSettings(XmlDocument source,XmlDocument target) {
  125.             foreach(XmlAttribute sourceAttribute in source.SelectNodes("configuration/appSettings/add/@key")) {
  126.                 SetAppSetting(sourceAttribute.OwnerElement,target);
  127.             }
  128.         }
  129.  
  130.         void SetAppSetting(XmlNode sourceNode,XmlDocument target) {
  131.             string key = sourceNode.Attributes["key"].Value;
  132.  
  133.             // Make sure the setting in the source config file actually has a value.
  134.             XmlAttribute sourceValue = sourceNode.Attributes["value"];
  135.             if(sourceValue == null) {
  136.                 ErrorList.Add(string.Format("The appSetting with key {0} in the source config file does not have a value.",key));
  137.                 return;
  138.             }
  139.  
  140.             // Make sure that there is a setting with the correct key in the target config file.
  141.             XmlNode targetNode = target.SelectSingleNode(string.Format("/configuration/appSettings/add[@key='{0}']",key));
  142.             if(targetNode == null) {
  143.                 ErrorList.Add(string.Format("An appSetting with key {0} could not be found in the target config file. It will not be added.",key));
  144.                 return;
  145.             }
  146.  
  147.             // Make sure that the target setting has a value attribute.
  148.             XmlAttribute targetValue = targetNode.Attributes["value"];
  149.             if(targetValue == null) {
  150.                 ErrorList.Add(string.Format("The appSetting with key {0} in the target file does not have a value attribute. It will not be added.",key));
  151.                 return;
  152.             }
  153.  
  154.             // At this point everything is in place, so copy the value over.
  155.             targetValue.Value = sourceValue.Value;
  156.             InfoLog.Add(string.Format("The value of {0} was updated successfully to '{1}'.",key,sourceValue.Value));
  157.         }
  158.  
  159.         void SetConnectionStrings(XmlDocument source,XmlDocument target) {
  160.             foreach(XmlAttribute sourceAttribute in source.SelectNodes("configuration/connectionStrings/add/@name")) {
  161.                 SetConnectionString(sourceAttribute.OwnerElement,target);
  162.             }
  163.         }
  164.  
  165.         void SetConnectionString(XmlNode sourceNode,XmlDocument target) {
  166.             string name = sourceNode.Attributes["name"].Value;
  167.  
  168.             // Make sure the setting in the source config file actually has a connectionString.
  169.             XmlAttribute sourceValue = sourceNode.Attributes["connectionString"];
  170.             if(sourceValue == null) {
  171.                 ErrorList.Add(string.Format("The connectionStrings with name {0} in the source config file does not have a connectionString.",name));
  172.                 return;
  173.             }
  174.  
  175.             // Make sure that there is a setting with the correct key in the target config file.
  176.             XmlNode targetNode = target.SelectSingleNode(string.Format("/configuration/connectionStrings/add[@name='{0}']",name));
  177.             if(targetNode == null) {
  178.                 ErrorList.Add(string.Format("An connectionStrings with name {0} could not be found in the target config file. It will not be added.",name));
  179.                 return;
  180.             }
  181.  
  182.             // Make sure that the target setting has a value attribute.
  183.             XmlAttribute targetValue = targetNode.Attributes["connectionString"];
  184.             if(targetValue == null) {
  185.                 ErrorList.Add(string.Format("The connectionStrings with name {0} in the target file does not have a connectionString attribute. It will not be added.",name));
  186.                 return;
  187.             }
  188.  
  189.             // At this point everything is in place, so copy the value over.
  190.             targetValue.Value = sourceValue.Value;
  191.             InfoLog.Add(string.Format("The connectionString of {0} was updated successfully to '{1}'.",name,sourceValue.Value));
  192.         }
  193.     }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment