Showing posts with label automated builds. Show all posts
Showing posts with label automated builds. Show all posts

Tuesday, October 4, 2016

Tool to create Parameters.xml and SetParameters.xml for MS Web deploy


             Tool to create Parameters.xml and SetParameters.xml for MS Web deploy

While automating builds and deployments, there is a need to create two xml files used by MS web deploy. Using the below program in a console application, can produce the desired files in output. It needs a sample web.config for its input. Both of them are configurable items.


--------------------------------------------program.cs----------------------------------------------

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace WebDeployParamsFileCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            string paramFile = String.Concat(ConfigurationManager.AppSettings["WebDeployParamFileLocation"], "Parameters.xml");
            string setParamFile = String.Concat(ConfigurationManager.AppSettings["WebDeployParamFileLocation"], "SetParameters.xml");

            string paramFileContents = GetParamFileContents().Item1;
            string setParamFileContents = GetParamFileContents().Item2;

            File.WriteAllText(paramFile, paramFileContents);
            File.WriteAllText(setParamFile, setParamFileContents);
        }

        private static Tuple<string, string> GetParamFileContents()
        {
            StringBuilder sbParamFileContents = new StringBuilder();
            sbParamFileContents.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sbParamFileContents.AppendLine("<parameters>");

            StringBuilder sbSetParamFileContents = new StringBuilder();
            sbSetParamFileContents.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            sbSetParamFileContents.AppendLine("<parameters>");


            var webconfig = XDocument.Load(ConfigurationManager.AppSettings["WebConfigFileLocation"]);


            #region AppSettings
            sbParamFileContents.AppendLine("<!--AppSettings START-->");
            sbSetParamFileContents.AppendLine("<!--AppSettings START-->");
            var appSettings = from c in webconfig.Root.Descendants("appSettings").Descendants()
                              select c;
            foreach (var item in appSettings)
            {
                var keyName = item.Attribute("key").Value;
                var val = item.Attribute("value").Value;

                //Parameters.xml
                sbParamFileContents.AppendLine("<parameter name=\"AppSettings - " + keyName + "\" description=\"\" defaultValue=\"\">");
                sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/appSettings/add[@key='" + keyName + "']/@value\" />");
                sbParamFileContents.AppendLine("</parameter>");

                //SetParameters.xml
                sbSetParamFileContents.AppendLine("<setParameter name=\"AppSettings - " + keyName + "\" value=\"" + val + " \"/>");


            }
            sbParamFileContents.AppendLine("<!--AppSettings END-->");
            sbSetParamFileContents.AppendLine("<!--AppSettings END-->");
            #endregion

            sbParamFileContents.AppendLine("");
            sbSetParamFileContents.AppendLine("");

            #region Client EndPoints
            sbParamFileContents.AppendLine("<!--Client EndPoint START-->");
            sbSetParamFileContents.AppendLine("<!--Client EndPoint START-->");
            var clientEndpoints = from c in webconfig.Root.Descendants("system.serviceModel").Descendants("client").Descendants()
                                  select c;
            foreach (var item in clientEndpoints)
            {
                var endpointName = item.Attribute("name").Value;
                var address = item.Attribute("address").Value;

                //Parameters.xml
                sbParamFileContents.AppendLine("<parameter name=\"Client Endpoint - " + endpointName + "\" description=\"\" defaultValue=\"\">");
                sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/system.serviceModel/client/endpoint[@name='" + endpointName + "']/@address\" />");
                sbParamFileContents.AppendLine("</parameter>");

                //SetParameters.xml
                sbSetParamFileContents.AppendLine("<setParameter name=\"Client Endpoint - " + endpointName + "\" value=\"" + address + "\" />");
            }
            sbParamFileContents.AppendLine("<!--Client EndPoint END-->");
            sbSetParamFileContents.AppendLine("<!--Client EndPoint END-->");
            #endregion

            sbParamFileContents.AppendLine("");
            sbSetParamFileContents.AppendLine("");

            #region Log4Net
            sbParamFileContents.AppendLine("<!--Log4net START-->");
            sbSetParamFileContents.AppendLine("<!--Log4net START-->");
            var log4netAppender = from c in webconfig.Root.Descendants("log4net").Descendants()
                                  where c.Name == "appender"
                                  select c;
            foreach (var item in log4netAppender)
            {
                var appenderType = item.Attribute("type").Value;
                var appenderName = item.Attribute("name").Value;                

                if (appenderType == "log4net.Appender.RollingFileAppender")
                {
                    //Parameters.xml
                    if (item.Descendants("file").Count() != 0) //two ways of representing the same thing in log4net.
                    {
                        sbParamFileContents.AppendLine("<parameter name=\"Log4net - " + appenderName + " - File Location\" description=\"\" defaultValue=\"\">");
                        sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/log4net/appender[@name='" + appenderName + "']/file/@value\" />");
                        sbParamFileContents.AppendLine("</parameter>");
                    }
                    else
                    {
                        sbParamFileContents.AppendLine("<parameter name=\"Log4net - " + appenderName + " - File Location\" description=\"\" defaultValue=\"\">");
                        sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/log4net/appender[@name='" + appenderName + "']/param[@name='File']/@value\" />");
                        sbParamFileContents.AppendLine("</parameter>");

                    }

                    //SetParameters.xml
                    string val = "";
                    if (item.Descendants("file").Count() != 0)
                    {
                        val = item.Descendants("file").First().Attribute("value").Value;
                    }
                    else
                    {
                        val = item.Descendants("param").Where(x => x.Attribute("name").Value == "File").First().Attribute("value").Value; 
                    }
                    sbSetParamFileContents.AppendLine("<setParameter name=\"Log4net - " + appenderName + " - File Location\" value=\"" + val + "\" />");

                }
                else if (appenderType == "log4net.Appender.AdoNetAppender")
                {
                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"Log4net - " + appenderName + " - ConnectionString\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/log4net/appender[@name='" + appenderName + "']/connectionString/@value\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val = item.Descendants("connectionString").First().Attribute("value").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"Log4net - " + appenderName + " - ConnectionString\" value=\"" + val + "\" />");
                }
                else if (appenderType == "log4net.Appender.SmtpAppender")
                {
                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"Log4net - " + appenderName + " - MailTo\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/log4net/appender[@name='" + appenderName + "']/to/@value\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val1 = item.Descendants("to").First().Attribute("value").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"Log4net - " + appenderName + " - MailTo\" value=\"" + val1 + "\" />");

                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"Log4net - " + appenderName + " - MailFrom\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/log4net/appender[@name='" + appenderName + "']/from/@value\" />");
                    sbParamFileContents.AppendLine("</parameter>");
                   
                    //SetParameters.xml
                    var val2 = item.Descendants("from").First().Attribute("value").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"Log4net - " + appenderName + " - MailFrom\" value=\"" + val2 + "\" />");
                }
                else
                {
                    throw new Exception("Code generation failed as there is no configuration for " + appenderType);
                }
            }
            sbParamFileContents.AppendLine("<!--Log4net END-->");
            sbSetParamFileContents.AppendLine("<!--Log4net END-->");
            #endregion

            sbParamFileContents.AppendLine("");
            sbSetParamFileContents.AppendLine("");

            #region EL Logging
            sbParamFileContents.AppendLine("<!--EL Logging START-->");
            sbSetParamFileContents.AppendLine("<!--EL Logging START-->");
            var elLoggingListeners = from c in webconfig.Root.Descendants("loggingConfiguration").Elements()
                                     where c.Name == "listeners"
                                     select c;
            elLoggingListeners = elLoggingListeners.Descendants();
            foreach (var item in elLoggingListeners)
            {
                var listenerType = item.Attribute("type").Value;
                var listenerName = item.Attribute("name").Value;
                if (listenerType.Contains("Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener"))
                {
                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"EL - " + listenerName + " - File Location\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/loggingConfiguration/listeners[@add='" + listenerName + "']/@fileName\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val = item.Attribute("fileName").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"EL - " + listenerName + " - File Location\" value=\"" + val + "\" />");

                }
                else if (listenerType.Contains("Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.EmailTraceListener"))
                {
                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"EL - " + listenerName + " - MailTo\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/loggingConfiguration/listeners[@add='" + listenerName + "']/@toAddress\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val = item.Attribute("toAddress").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"EL - " + listenerName + " - MailTo\" value=\"" + val + "\" />");

                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"EL - " + listenerName + " - MailFrom\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/loggingConfiguration/listeners[@add='" + listenerName + "']/@fromAddress\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val2 = item.Attribute("fromAddress").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"EL - " + listenerName + " - MailFrom\" value=\"" + val2 + "\" />");

                    //Parameters.xml
                    sbParamFileContents.AppendLine("<parameter name=\"EL - " + listenerName + " - SubjectLineStarter\" description=\"\" defaultValue=\"\">");
                    sbParamFileContents.AppendLine("<parameterEntry kind=\"XmlFile\" scope=\"Web.config\" match=\"/configuration/loggingConfiguration/listeners[@add='" + listenerName + "']/@subjectLineStarter\" />");
                    sbParamFileContents.AppendLine("</parameter>");

                    //SetParameters.xml
                    var val3 = item.Attribute("subjectLineStarter").Value;
                    sbSetParamFileContents.AppendLine("<setParameter name=\"EL - " + listenerName + " - SubjectLineStarter\" value=\"" + val3 + "\" />");
                }
                else
                {
                    throw new Exception("Code generation failed as there is no configuration for " + listenerType);
                }
            }
            sbParamFileContents.AppendLine("<!--EL Logging START END-->");
            sbSetParamFileContents.AppendLine("<!--EL Logging START END-->");
            #endregion


            sbParamFileContents.AppendLine("");
            sbSetParamFileContents.AppendLine("");

            #region ConnectionStrings-SetParameters Only
            sbSetParamFileContents.AppendLine("<!--Connection String START-->");
            var connectionStrings = from c in webconfig.Root.Descendants("connectionStrings").Descendants()
                              select c;
            foreach (var item in connectionStrings)
            {
                var connectionStringName = item.Attribute("name").Value;
                var connectionStringValue = item.Attribute("connectionString").Value;
                connectionStringValue = connectionStringValue.Replace("\"", @"&quot;");
                
                //SetParameters.xml
                sbSetParamFileContents.AppendLine("<setParameter name=\"" + connectionStringName + "-Web.config Connection String\" value=\"" + connectionStringValue + " \"/>");


            }
            sbSetParamFileContents.AppendLine("<!--Connection String END-->");
            #endregion

            sbParamFileContents.AppendLine("</parameters>");
            sbSetParamFileContents.AppendLine("</parameters>");
            return new Tuple<string, string>(sbParamFileContents.ToString(), sbSetParamFileContents.ToString());
        }
    }

}


----------------------------------------------config file--------------------------------------------

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <appSettings>
    <add key="WebDeployParamFileLocation" value="c:\\temp2\\"/>
    <add key="WebConfigFileLocation" value="c:\\temp2\\web.config"/>
  </appSettings>
</configuration>
----------------------------------------------------------------------------------------------------