示例#1
0
        /// <summary>
        /// Executes the selected config
        /// </summary>
        /// <param name="configFileDirectory">The path the config file was loaded from</param>
        /// <param name="relativeFileName">The path relative to the config file to modify</param>
        /// <param name="config">The selected config to run</param>
        /// <param name="fileSystem">File system interface</param>
        public void ProcessConfig(string configFileDirectory, string relativeFileName, ConfigPackage config, IFileSystem fileSystem)
        {
            var xmlManager    = new XmlFileManager();
            var stringManager = new StringManager();

            foreach (var key in config.AppSettings.Keys)
            {
                string filename = Path.Combine(configFileDirectory, relativeFileName);
                var    setting  = new XmlSetting(string.Empty, $"/configuration/appSettings/add[@key = '{key}']/@value", config.AppSettings[key], null);
                logger.Trace($"Processing app setting {key}");
                var result = xmlManager.Process(File.ReadAllText(filename), setting);
                if (result.Success)
                {
                    fileSystem.WriteAllText(filename, result.Content);
                }
            }

            foreach (var key in config.ConnectionStrings.Keys)
            {
                string filename = Path.Combine(configFileDirectory, relativeFileName);
                var    setting  = new XmlSetting(string.Empty, $"/configuration/connectionStrings/add[@name = '{key}']/@connectionString", config.ConnectionStrings[key], null);
                logger.Trace($"Processing connection string setting {key}");
                var result = xmlManager.Process(File.ReadAllText(filename), setting);
                if (result.Success)
                {
                    fileSystem.WriteAllText(filename, result.Content);
                }
            }

            foreach (var setting in config.XmlSettings)
            {
                string filename = Path.Combine(configFileDirectory, relativeFileName);
                logger.Trace($"Processing setting {setting.Description}");
                var result = xmlManager.Process(File.ReadAllText(filename), setting);
                if (result.Success)
                {
                    fileSystem.WriteAllText(filename, result.Content);
                }
            }

            foreach (var setting in config.StringSettings)
            {
                string filename = Path.Combine(configFileDirectory, relativeFileName);
                logger.Trace($"Processing setting {setting.Description}");
                var result = stringManager.Process(File.ReadAllText(filename), setting);
                if (result.Success)
                {
                    fileSystem.WriteAllText(filename, result.Content);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Processes the specified file using the XPath selector and replaces the found element with the supplied value/
        /// </summary>
        /// <param name="contents">file to modify</param>
        /// <param name="setting">XMl setting object</param>
        /// <returns>Returns a process result for the current action</returns>
        public ProcessResult Process(string contents, XmlSetting setting)
        {
            contents.Should().NotBeNullOrWhiteSpace();
            setting.Should().NotBeNull();

            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;
            doc.LoadXml(contents);

            XmlNode root = doc.DocumentElement;

            var namespaceManager = new XmlNamespaceManager(doc.NameTable);

            if (setting.Namespaces != null)
            {
                foreach (var key in setting.Namespaces.Keys)
                {
                    logger.Trace($"Adding namespace {key} with value { setting.Namespaces[key]}");
                    namespaceManager.AddNamespace(key, setting.Namespaces[key]);
                }
            }

            var node = root.SelectSingleNode(setting.Selector, namespaceManager);

            if (node != null)
            {
                logger.Trace($"Selector returned node with value of {node.OuterXml}");
                node.InnerText = setting.Value;
                logger.Info($"Selector {setting.Selector} processed. Node value set to {setting.Value}");
                return(new ProcessResult(true, doc.OuterXml));
            }
            else
            {
                logger.Warn($"The selector {setting.Selector} did not return any nodes");
                return(new ProcessResult(false, string.Empty));
            }
        }