Beispiel #1
0
        public static void GenerateSettings(string filePath)
        {
            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException("No file path specified, either set the GlobalSettingsFileLocation setting in the config file or pass it in as an argument\r\ne.g. SetGen.exe GlobalSettingsFileLocation=Resources\\GlobalSettings.xml");
            }
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(
                          string.Format("Global settings File not found at path \"{0}\"", Path.GetFullPath(filePath)), filePath);
            }
            XDocument document = XDocument.Load(filePath, LoadOptions.SetLineInfo | LoadOptions.PreserveWhitespace);

            Console.WriteLine("Getting projects & Azure modules from xml");
            IEnumerable <Project> projects = GetAzureModulesFromXml(document);

            Console.WriteLine("Getting Azure mappings from xml");
            IEnumerable <AzureMapping> mappings = GetAzureMappingsFromXml(document);

            Console.WriteLine("Getting App settings from xml");
            IEnumerable <XmlAppSetting> appSettings = GetAppSettingsFromXml(document);

            Console.WriteLine("Getting settings class settings from xml");
            SettingsClass settingsClassSettings = GetSettingsClassSettingsFromXml(document, "settingsClass");

            Console.WriteLine("Getting static settings class settings from xml");
            SettingsClass staticClassSettings = GetSettingsClassSettingsFromXml(document, "staticClass");

            Console.WriteLine("Generating Azure files");
            CreateAzureFiles(appSettings, projects, mappings);

            Console.WriteLine("Generating environment settings files");
            CreateEnvironmentSettings(appSettings, projects);

            Console.WriteLine("Generating settings class");
            CreateSettingsClass(appSettings, settingsClassSettings);

            Console.WriteLine("Generating static settings class");
            CreateStaticSettingsClass(appSettings, staticClassSettings, settingsClassSettings);
        }
Beispiel #2
0
        private static SettingsClass GetSettingsClassSettingsFromXml(XDocument document, string nodeName)
        {
            var xElement = document.Descendants(nodeName).FirstOrDefault();

            if (xElement == null)
            {
                return(null);
            }
            string defaultFileName = string.Empty;

            switch (nodeName)
            {
            case "settingsClass":
                defaultFileName = "Settings.cs";
                break;

            case "staticClass":
                defaultFileName = "ConfigurationManager.cs";
                break;

            default: throw new ArgumentException(string.Format("Unknown class \"{0}\"", nodeName));
            }
            var settings = new SettingsClass
            {
                Directory    = GetAttribute(xElement, "directory", false) ?? string.Empty,
                Name         = GetAttribute(xElement, "name", true) ?? defaultFileName,
                Namespace    = GetAttribute(xElement, "namespace", true) ?? "Entelect",
                ExcludeAzure = bool.Parse(GetAttribute(xElement, "excludeazure", false) ?? "false")
            };

            if (!settings.Name.EndsWith(".cs"))
            {
                settings.Name = string.Format("{0}.cs", settings.Name);
            }
            return(settings);
        }
Beispiel #3
0
        private static void CreateStaticSettingsClass(IEnumerable <XmlAppSetting> appSettings, SettingsClass staticClassSettings, SettingsClass settingsClassSettings)
        {
            //staticClassSettings
            if (staticClassSettings == null)
            {
                return;
            }
            if (settingsClassSettings == null)
            {
                throw new ArgumentException("in order to use the static class, you must also include the <settingsClass> node in your xml");
            }
            const string  indentation          = "    ";
            string        doubleIndentation    = string.Concat(indentation, indentation);
            string        trippleIndentation   = string.Concat(doubleIndentation, indentation);
            string        quadrupleIndentation = string.Concat(doubleIndentation, doubleIndentation);
            string        className            = staticClassSettings.Name.Replace(".cs", "").CapitaliseFirstLetter();
            StringBuilder stringBuilder        = new StringBuilder();

            stringBuilder.AppendLine("using System;");
            stringBuilder.AppendLine("using System.Collections.Generic;");
            stringBuilder.AppendLine("using System.Linq;");
            stringBuilder.AppendLine("using Entelect.Settings;");
            if (!staticClassSettings.ExcludeAzure)
            {
                stringBuilder.AppendLine("using Entelect.Azure;");
                stringBuilder.AppendLine("using Microsoft.WindowsAzure.ServiceRuntime;");
            }

            stringBuilder.AppendLineFormat("using {0};", settingsClassSettings.Namespace);
            stringBuilder.AppendLineFormat("namespace {0}", staticClassSettings.Namespace);
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLineFormat("{0}public static partial class {1}", indentation, className);
            stringBuilder.AppendLineFormat("{0}{{", indentation);
            stringBuilder.AppendLineFormat("{0}public static ISettingsSource Settings {{ get; set; }}", doubleIndentation).AppendLine();
            BuildStaticConstructor(doubleIndentation, stringBuilder, className, trippleIndentation, quadrupleIndentation, staticClassSettings.ExcludeAzure);
            //Add environment property
            stringBuilder.AppendLineFormat("{0}public static DeploymentEnvironment Environment {{ get {{ return Settings.Environment; }} }}", doubleIndentation).AppendLine();
            //Add get typed setting property
            BuildUntypedGetSetting(doubleIndentation, stringBuilder, trippleIndentation);
            BuildTypedGetSetting(doubleIndentation, stringBuilder, trippleIndentation);
            foreach (var xmlAppSetting in appSettings)
            {
                if (xmlAppSetting.Autogenerate == false)
                {
                    continue;
                }
                GetAppSettingProperty(stringBuilder, xmlAppSetting, settingsClassSettings);
            }
            stringBuilder.AppendLineFormat("{0}}}", indentation);
            stringBuilder.AppendLine("}");
            string filePath = Path.Combine(staticClassSettings.Directory, staticClassSettings.Name);

            if (!Directory.Exists(staticClassSettings.Directory))
            {
                Directory.CreateDirectory(staticClassSettings.Directory);
            }
            using (StreamWriter streamWriter = new StreamWriter(filePath))
            {
                streamWriter.Write(stringBuilder);
            }
        }
Beispiel #4
0
        private static void CreateSettingsClass(IEnumerable <XmlAppSetting> appSettings, SettingsClass settingsClass)
        {
            if (settingsClass == null)
            {
                return;
            }
            const string  indentation       = "    ";
            string        doubleIndentation = string.Concat(indentation, indentation);
            StringBuilder stringBuilder     = new StringBuilder();

            stringBuilder.AppendLine("using System;");
            stringBuilder.AppendLine("using System.Collections.Generic;");
            stringBuilder.AppendLine("using System.Linq;");
            stringBuilder.AppendLine("using System.Text;");
            stringBuilder.AppendLine("using Entelect.Settings;");
            stringBuilder.AppendLineFormat("namespace {0}", settingsClass.Namespace);
            stringBuilder.AppendLine("{");
            stringBuilder.AppendLineFormat("{0}public static partial class {1}", indentation,
                                           settingsClass.Name.Replace(".cs", "").CapitaliseFirstLetter());
            stringBuilder.AppendLineFormat("{0}{{", indentation);
            foreach (var xmlAppSetting in appSettings)
            {
                if (xmlAppSetting.Autogenerate == false)
                {
                    continue;
                }
                string escapedSettingName = xmlAppSetting.Name.Replace(":", "").Replace(".", "").CapitaliseFirstLetter();
                //Check if string
                if (xmlAppSetting.Type.Equals("string", StringComparison.OrdinalIgnoreCase))
                {
                    stringBuilder.AppendLineFormat("{0}public static string {1} = \"{2}\";", doubleIndentation, escapedSettingName, xmlAppSetting.Name);
                }
                // check if list
                else if (xmlAppSetting.Type.StartsWith("list<", StringComparison.OrdinalIgnoreCase))
                {
                    string innerType = xmlAppSetting.Type.ReplaceIgnoreCase("list<", "");
                    innerType = innerType.Remove(innerType.Length - 1);
                    if (string.IsNullOrWhiteSpace(xmlAppSetting.Delimiter))
                    {
                        throw new XmlSchemaException(string.Format("No delimiter specified for list with name {0}",
                                                                   xmlAppSetting.Name));
                    }
                    //list of strings
                    if (innerType.Equals("string", StringComparison.OrdinalIgnoreCase))
                    {
                        stringBuilder.AppendLineFormat(
                            "{0}public static Setting<{1}> {2} = new Setting<{1}>(x => new {1}(x.GetSetting(\"{3}\").Split(new[]{{'{4}'}},StringSplitOptions.RemoveEmptyEntries)));",
                            doubleIndentation, xmlAppSetting.Type, escapedSettingName, xmlAppSetting.Name,
                            xmlAppSetting.Delimiter);
                    }
                    else
                    {
                        Type parsedType = TypeExtensions.GetTypeFromTypeName(innerType);
                        if (parsedType != null && parsedType.IsGenericType && parsedType.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            //list of nullable base types
                            string trippleIndentation   = string.Concat(doubleIndentation, indentation);
                            string quadrupleIndentation = string.Concat(doubleIndentation, doubleIndentation);
                            string quintupleIndentation = string.Concat(quadrupleIndentation, indentation);
                            string baseTypeName         =
                                innerType.Replace("?", "").Replace("Nullable<", "").Replace(">", "");
                            stringBuilder.AppendLineFormat(
                                "{0}public static Setting<{1}> {2} = new Setting<{1}>(x => {{",
                                doubleIndentation, xmlAppSetting.Type, escapedSettingName);
                            stringBuilder.AppendLineFormat("{0}string[] values = x.GetSetting(\"{1}\").Split(new[]{{'{2}'}},StringSplitOptions.RemoveEmptyEntries);", trippleIndentation, xmlAppSetting.Name, xmlAppSetting.Delimiter);
                            stringBuilder.AppendLineFormat("{0}{1} results = new {1}();", trippleIndentation, xmlAppSetting.Type);
                            stringBuilder.AppendLineFormat("{0}foreach(string value in values)", trippleIndentation, baseTypeName);
                            stringBuilder.AppendLineFormat("{0}{{", trippleIndentation);
                            stringBuilder.AppendLineFormat("{0}if (string.IsNullOrWhiteSpace(value))", quadrupleIndentation);
                            stringBuilder.AppendLineFormat("{0}results.Add(null);", quintupleIndentation);
                            stringBuilder.AppendLineFormat("{0}if (value.Equals(\"null\",StringComparison.OrdinalIgnoreCase))", quadrupleIndentation);
                            stringBuilder.AppendLineFormat("{0}results.Add(null);", quintupleIndentation);
                            stringBuilder.AppendLineFormat("{0}{1} parsed;", quadrupleIndentation, baseTypeName);
                            stringBuilder.AppendLineFormat("{0}if ({1}.TryParse(value,out parsed))", quadrupleIndentation, baseTypeName);
                            stringBuilder.AppendLineFormat("{0}results.Add(parsed);", quintupleIndentation);
                            stringBuilder.AppendLineFormat("{0}throw new ArgumentException(string.Format(\"Value '{{0}}' could not be converted to type '{1}'\",value));", quadrupleIndentation, xmlAppSetting.Type);
                            stringBuilder.AppendLineFormat("{0}}}", trippleIndentation);
                            stringBuilder.AppendLineFormat("{0}return results;", trippleIndentation);
                            stringBuilder.AppendLineFormat("{0}}});", doubleIndentation);
                        }
                        else
                        {
                            //list of other type
                            stringBuilder.AppendLineFormat(
                                "{0}public static Setting<{1}> {2} = new Setting<{1}>(x => new {1}(x.GetSetting(\"{3}\").Split(new[]{{'{4}'}},StringSplitOptions.RemoveEmptyEntries).Select({5}.Parse)));",
                                doubleIndentation, xmlAppSetting.Type, escapedSettingName, xmlAppSetting.Name,
                                xmlAppSetting.Delimiter, innerType);
                        }
                    }
                }
                else
                {
                    Type parsedType = TypeExtensions.GetTypeFromTypeName(xmlAppSetting.Type);
                    if (parsedType != null && parsedType.IsGenericType && parsedType.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        //Is nullable core class
                        string trippleIndentation   = string.Concat(doubleIndentation, indentation);
                        string quadrupleIndentation = string.Concat(doubleIndentation, doubleIndentation);
                        string baseTypeName         =
                            xmlAppSetting.Type.Replace("?", "").Replace("Nullable<", "").Replace(">", "");
                        stringBuilder.AppendLineFormat(
                            "{0}public static Setting<{1}> {2} = new Setting<{1}>(x => {{",
                            doubleIndentation, xmlAppSetting.Type, escapedSettingName);
                        stringBuilder.AppendLineFormat("{0}string value = x.GetSetting(\"{1}\");", trippleIndentation, xmlAppSetting.Name);
                        stringBuilder.AppendLineFormat("{0}if (string.IsNullOrWhiteSpace(value))", trippleIndentation);
                        stringBuilder.AppendLineFormat("{0}return null;", quadrupleIndentation);
                        stringBuilder.AppendLineFormat("{0}if (value.Equals(\"null\",StringComparison.OrdinalIgnoreCase))", trippleIndentation);
                        stringBuilder.AppendLineFormat("{0}return null;", quadrupleIndentation);
                        stringBuilder.AppendLineFormat("{0}{1} parsed;", trippleIndentation, baseTypeName);
                        stringBuilder.AppendLineFormat("{0}if ({1}.TryParse(value,out parsed))", trippleIndentation, baseTypeName);
                        stringBuilder.AppendLineFormat("{0}return parsed;", quadrupleIndentation);
                        stringBuilder.AppendLineFormat("{0}throw new ArgumentException(string.Format(\"Value '{{0}}' could not be converted to type '{1}'\",value));", trippleIndentation, xmlAppSetting.Type);
                        stringBuilder.AppendLineFormat("{0}}});", doubleIndentation);
                    }
                    else
                    {
                        //not nullable core type or is custom user type
                        stringBuilder.AppendLineFormat(
                            "{0}public static Setting<{1}> {2} = new Setting<{1}>(x => {1}.Parse(x.GetSetting(\"{3}\")));",
                            doubleIndentation, xmlAppSetting.Type, escapedSettingName, xmlAppSetting.Name);
                    }
                }
            }
            stringBuilder.AppendLineFormat("{0}}}", indentation);
            stringBuilder.AppendLine("}");
            string filePath = Path.Combine(settingsClass.Directory, settingsClass.Name);

            if (!Directory.Exists(settingsClass.Directory))
            {
                Directory.CreateDirectory(settingsClass.Directory);
            }
            using (StreamWriter streamWriter = new StreamWriter(filePath))
            {
                streamWriter.Write(stringBuilder);
            }
        }
Beispiel #5
0
        private static void GetAppSettingProperty(StringBuilder stringBuilder, XmlAppSetting xmlAppSetting, SettingsClass settingsClassSettings)
        {
            const string indentation          = "    ";
            string       doubleIndentation    = string.Concat(indentation, indentation);
            string       trippleIndentation   = string.Concat(doubleIndentation, indentation);
            string       quadrupleIndentation = string.Concat(doubleIndentation, doubleIndentation);
            string       escapedSettingName   = xmlAppSetting.Name.Replace(":", "").Replace(".", "").CapitaliseFirstLetter();

            stringBuilder.AppendLineFormat("{0}public static {1} {2} {{ get {{ return GetSetting({3}.{2}); }} }}", doubleIndentation, xmlAppSetting.Type, escapedSettingName, settingsClassSettings.Name.Replace(".cs", ""));
            stringBuilder.AppendLine();
        }