Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Xml;
- using Microsoft.Build.Framework;
- using Microsoft.Build.Utilities;
- using System.IO;
- namespace MergeConfigTask {
- public class ConfigurationMerger {
- public List<string> ErrorList { get; private set; }
- public List<string> InfoLog { get; private set; }
- string _sourceConfigFilename;
- string _targetConfigFilename;
- string _outputConfigFilename;
- /// <summary>
- /// Gets or sets the filename of the source config file.
- /// </summary>
- public string SourceConfigFilename {
- get { return _sourceConfigFilename; }
- set { _sourceConfigFilename = value; }
- }
- /// <summary>
- /// Gets or sets the filename of the target config file.
- /// </summary>
- public string TargetConfigFilename {
- get { return _targetConfigFilename; }
- set { _targetConfigFilename = value; }
- }
- /// <summary>
- /// Gets or sets the filename of the output config file that is changed.
- /// </summary>
- public string OutputConfigFilename {
- get { return _outputConfigFilename; }
- set { _outputConfigFilename = value; }
- }
- public ConfigurationMerger() {
- ErrorList = new List<string>();
- InfoLog = new List<string>();
- }
- /// <summary>
- /// When overridden in a derived class, executes the task.
- /// </summary>
- /// <returns>
- /// true if the task successfully executed; otherwise, false.
- /// </returns>
- public bool Execute() {
- ErrorList.Clear();
- InfoLog.Clear();
- try {
- XmlDocument source = LoadConfigFile(SourceConfigFilename);
- XmlDocument target = LoadConfigFile(TargetConfigFilename);
- if(!string.IsNullOrEmpty(OutputConfigFilename)) {
- File.Copy(TargetConfigFilename,OutputConfigFilename,true);
- //File.WriteAllText(OutputConfigFilename,File.ReadAllText(TargetConfigFilename));
- }
- SetCompilatationDebug(source,target);
- SetCustomErrorsMode(source,target);
- SetAppSettings(source,target);
- SetConnectionStrings(source,target);
- if(string.IsNullOrEmpty(OutputConfigFilename)) {
- target.Save(TargetConfigFilename);
- InfoLog.Add(string.Format("Saved transformed file to {0}",TargetConfigFilename));
- }
- else {
- target.Save(OutputConfigFilename);
- InfoLog.Add(string.Format("Saved transformed file to {0}",OutputConfigFilename));
- }
- }
- catch(Exception err) {
- ErrorList.Add(err.Message);
- }
- return ErrorList.Count == 0;
- }
- XmlDocument LoadConfigFile(string filename) {
- XmlDocument configFile = new XmlDocument();
- configFile.Load(filename);
- return configFile;
- }
- void SetCompilatationDebug(XmlDocument source,XmlDocument target) {
- SetNodeValue(source,target,"configuration/system.web/compilation/@debug","the compilation node's debug attribute");
- }
- void SetCustomErrorsMode(XmlDocument source,XmlDocument target) {
- SetNodeValue(source,target,"configuration/system.web/customErrors/@mode","the customErrors node's mode attribute");
- }
- void SetNodeValue(XmlDocument source,XmlDocument target,string xpath,string nodeDescription) {
- // Get the value of the source node.
- XmlNode sourceNode = source.SelectSingleNode(xpath);
- if(sourceNode == null) {
- ErrorList.Add(string.Format("{0} could not be found in the source config file.",nodeDescription));
- return;
- }
- string value = sourceNode.Value;
- InfoLog.Add(string.Format("{0} was found in the source config file, with a value of '{1}'.",nodeDescription,value));
- // Set the value of the target node.
- XmlNode targetNode = target.SelectSingleNode(xpath);
- if(targetNode == null) {
- ErrorList.Add(string.Format("{0} could not be found in the target config file. It will not be added.",nodeDescription));
- return;
- }
- targetNode.Value = sourceNode.Value;
- InfoLog.Add(string.Format("The value of {0} was updated successfully to '{1}'.",nodeDescription,value));
- }
- void SetAppSettings(XmlDocument source,XmlDocument target) {
- foreach(XmlAttribute sourceAttribute in source.SelectNodes("configuration/appSettings/add/@key")) {
- SetAppSetting(sourceAttribute.OwnerElement,target);
- }
- }
- void SetAppSetting(XmlNode sourceNode,XmlDocument target) {
- string key = sourceNode.Attributes["key"].Value;
- // Make sure the setting in the source config file actually has a value.
- XmlAttribute sourceValue = sourceNode.Attributes["value"];
- if(sourceValue == null) {
- ErrorList.Add(string.Format("The appSetting with key {0} in the source config file does not have a value.",key));
- return;
- }
- // Make sure that there is a setting with the correct key in the target config file.
- XmlNode targetNode = target.SelectSingleNode(string.Format("/configuration/appSettings/add[@key='{0}']",key));
- if(targetNode == null) {
- ErrorList.Add(string.Format("An appSetting with key {0} could not be found in the target config file. It will not be added.",key));
- return;
- }
- // Make sure that the target setting has a value attribute.
- XmlAttribute targetValue = targetNode.Attributes["value"];
- if(targetValue == null) {
- 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));
- return;
- }
- // At this point everything is in place, so copy the value over.
- targetValue.Value = sourceValue.Value;
- InfoLog.Add(string.Format("The value of {0} was updated successfully to '{1}'.",key,sourceValue.Value));
- }
- void SetConnectionStrings(XmlDocument source,XmlDocument target) {
- foreach(XmlAttribute sourceAttribute in source.SelectNodes("configuration/connectionStrings/add/@name")) {
- SetConnectionString(sourceAttribute.OwnerElement,target);
- }
- }
- void SetConnectionString(XmlNode sourceNode,XmlDocument target) {
- string name = sourceNode.Attributes["name"].Value;
- // Make sure the setting in the source config file actually has a connectionString.
- XmlAttribute sourceValue = sourceNode.Attributes["connectionString"];
- if(sourceValue == null) {
- ErrorList.Add(string.Format("The connectionStrings with name {0} in the source config file does not have a connectionString.",name));
- return;
- }
- // Make sure that there is a setting with the correct key in the target config file.
- XmlNode targetNode = target.SelectSingleNode(string.Format("/configuration/connectionStrings/add[@name='{0}']",name));
- if(targetNode == null) {
- ErrorList.Add(string.Format("An connectionStrings with name {0} could not be found in the target config file. It will not be added.",name));
- return;
- }
- // Make sure that the target setting has a value attribute.
- XmlAttribute targetValue = targetNode.Attributes["connectionString"];
- if(targetValue == null) {
- 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));
- return;
- }
- // At this point everything is in place, so copy the value over.
- targetValue.Value = sourceValue.Value;
- InfoLog.Add(string.Format("The connectionString of {0} was updated successfully to '{1}'.",name,sourceValue.Value));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment