Пример #1
0
 public ConfigParsedData LoadConfigFromFile(IArguments arguments)
 {
     Arguments = arguments;
     try
     {
         var lines       = new List <string>();
         var fileContent = FileSystem.File.ReadLines(GetFilePath());
         foreach (var line in fileContent)
         {
             if (line.StartsWith("#"))
             {
                 ConfigData.AddComment(new ConfigComment(line));
             }
             else if (string.IsNullOrWhiteSpace(line) && lines.Count > 0)
             {
                 ConfigData.Add(ParseSection(lines));
                 lines = new List <string>();
             }
             else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
             {
                 lines.Add(line);
             }
         }
         if (lines.Count > 0)
         {
             ConfigData.Add(ParseSection(lines));
         }
         ConfigParsedData = ConfigData.ComputeAndReplace(Arguments, ComputeFuncs, EnumerationFuncs);
         return(ConfigParsedData);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Пример #2
0
        public ConfigParsedData ComputeAndReplace(IArguments arguments, IComputeFunc computeFuncs, IEnumerationFunc enumerationFuncs)
        {
            var newConfigData = new ConfigParsedData();
            var dictionary    = new Dictionary <string, List <string> >();
            List <(string header, string value, List <IConfigSet> variables)> list = new List <(string, string, List <IConfigSet>)>();
            var allKeys = TurnListConfigSectionIntoDict();

            DataStore.Where(y => y.IsString).ToList().ForEach(x => { var keyValue = allKeys[x.Key]; list.Add((x.Key, keyValue, x.ReturnAllVariables())); });
            foreach (var item in list)
            {
                StringBuilder        stringBuilder     = new StringBuilder(item.value);
                List <StringBuilder> stringBuilderList = new List <StringBuilder>()
                {
                    stringBuilder
                };
                foreach (var innerItem in item.variables)
                {
                    if (innerItem is ConfigComputeSet)
                    {
                        var innerComputeItem = innerItem as ConfigComputeSet;
                        stringBuilderList = stringBuilderList.SelectMany(y => GetParameter(innerComputeItem.Name, arguments, dictionary).Select(x => new StringBuilder(y.ToString().Replace(innerComputeItem.Token, innerComputeItem.Compute(x))))).ToList();
                    }
                    else if (innerItem is ConfigEnumerationSet)
                    {
                        var innerEnumerationItem = innerItem as ConfigEnumerationSet;
                        var enumeration          = GetParameter(innerEnumerationItem.Name, arguments, dictionary).SelectMany(y => innerEnumerationItem.GetEnumerable(y));
                        stringBuilderList = stringBuilderList.SelectMany(y => enumeration.Select(x => new StringBuilder(y.ToString().Replace(innerEnumerationItem.Token, x)))).ToList();
                    }
                    else
                    {
                        stringBuilderList = stringBuilderList.SelectMany(y => GetParameter(innerItem.Name, arguments, dictionary).Select(x => new StringBuilder(y.ToString().Replace(innerItem.Token, x)))).ToList();
                    }
                }
                AddConfigSections(item, stringBuilderList);
                dictionary.Add(item.header, stringBuilderList.Select(x => x.ToString()).ToList());
            }
            newConfigData.AddRange(DataStore.Where(x => !x.IsString));
            return(newConfigData);

            void AddConfigSections((string header, string value, List <IConfigSet> variables) item, List <StringBuilder> listOfStringBuilders)
            {
                foreach (var itemForEach in listOfStringBuilders)
                {
                    var stringBuilderValue = itemForEach.ToString();
                    newConfigData.Add(new ConfigSection(item.header, stringBuilderValue, isStringHeader: true, computeFuncs: computeFuncs, enumerationFuncs: enumerationFuncs));
                }
            }
        }
Пример #3
0
 public ConfigParsedData WriteToConfig(string section, bool value)
 {
     ConfigData.Add(CreateConfigSection(section, value.ToString(), false));
     ConfigParsedData = ConfigData.ComputeAndReplace(Arguments, ComputeFuncs, EnumerationFuncs);
     return(ConfigParsedData);
 }
Пример #4
0
 public ConfigParsedData WriteCommentToConfig(string comment)
 {
     ConfigData.AddComment(new ConfigComment($"#{comment}"));
     ConfigParsedData = ConfigData.ComputeAndReplace(Arguments, ComputeFuncs, EnumerationFuncs);
     return(ConfigParsedData);
 }