Example #1
0
        private string GetExistingTemplateContent()
        {
            var fileLocation = FileMetadata.GetFullLocationPathWithFileName();

            if (File.Exists(fileLocation))
            {
                return(File.ReadAllText(fileLocation));
            }

            return(null);
        }
Example #2
0
        public override string TransformText()
        {
            var location = FileMetadata.GetFullLocationPathWithFileName();

            var doc = LoadOrCreateWebConfig(location);

            if (doc == null)
            {
                throw new Exception("doc is null");
            }
            if (doc.Root == null)
            {
                throw new Exception("doc.Root is null");
            }

            var namespaces = new XmlNamespaceManager(new NameTable());

            namespaces.AddNamespace("ns", doc.Root.GetDefaultNamespace().NamespaceName);

            var configurationElement = doc.XPathSelectElement("/ns:configuration", namespaces);

            if (configurationElement == null)
            {
                doc.Add(configurationElement = new XElement("configuration"));
            }

            // App Settings:
            var appSettingsElement = doc.XPathSelectElement("/ns:configuration/ns:appSettings", namespaces);

            if (appSettingsElement == null && _appSettings.Any())
            {
                configurationElement.AddFirst(appSettingsElement = new XElement("appSettings"));
            }

            foreach (var appSetting in _appSettings)
            {
                if (appSettingsElement == null)
                {
                    throw new Exception("appSettingsElement is null");
                }

                if (appSettingsElement.XPathSelectElement($"//add[@key='{appSetting.Key}']", namespaces) == null)
                {
                    var setting = new XElement("add");
                    setting.Add(new XAttribute("key", appSetting.Key));
                    setting.Add(new XAttribute("value", appSetting.Value));
                    appSettingsElement.Add(setting);
                }
            }

            // Connection Strings:
            var connectionStringsElement = doc.XPathSelectElement("/ns:configuration/ns:connectionStrings", namespaces);

            if (connectionStringsElement == null && _connectionStrings.Any())
            {
                connectionStringsElement = new XElement("connectionStrings");

                if (appSettingsElement != null)
                {
                    appSettingsElement.AddBeforeSelf(connectionStringsElement);
                }
                else
                {
                    configurationElement.AddFirst(connectionStringsElement);
                }
            }

            foreach (var connectionString in _connectionStrings)
            {
                if (connectionStringsElement == null)
                {
                    throw new Exception("connectionStringsElement is null");
                }

                if (connectionStringsElement.XPathSelectElement($"//add[@name='{connectionString.Name}']", namespaces) == null)
                {
                    var setting = new XElement("add");
                    setting.Add(new XAttribute("name", connectionString.Name));
                    setting.Add(new XAttribute("providerName", connectionString.ProviderName));
                    setting.Add(new XAttribute("connectionString", connectionString.ConnectionString));
                    connectionStringsElement.Add(setting);
                }
            }

            foreach (var webConfigDecorator in GetDecorators())
            {
                webConfigDecorator.Install(doc, Project);
            }

            return(doc.ToStringUTF8());
        }