Exemplo n.º 1
0
        public async Task CapturePreferences(Preferences preferences, bool async = false)
        {
            List <Task> tasks = new List <Task>();

            preferences.Default ??= new Dictionary <string, SolutionPreferences>();
            foreach ((string?solutionId, Solution? solution) in this.All)
            {
                if (!preferences.Default.TryGetValue(solutionId, out SolutionPreferences? solutionPreferences))
                {
                    solutionPreferences = new SolutionPreferences();
                    preferences.Default.Add(solutionId, solutionPreferences);
                }

                Task capture = solution.Capture(solutionPreferences);
                if (async)
                {
                    tasks.Add(capture);
                }
                else
                {
                    await capture;
                }
            }

            if (async)
            {
                await Task.WhenAll(tasks);
            }
        }
Exemplo n.º 2
0
        // TODO: consider adding an "async" operation which can capture multiple settings in parallel; for now we're keeping it simple and using serial captures
        public async Task <MorphicResult <MorphicUnit, MorphicUnit> > CapturePreferencesAsync(Preferences preferences)
        {
            var success = true;

            List <Task> tasks = new List <Task>();

            preferences.Default ??= new Dictionary <string, SolutionPreferences>();
            foreach ((string?solutionId, Solution? solution) in this.All)
            {
                if (!preferences.Default.TryGetValue(solutionId, out SolutionPreferences? solutionPreferences))
                {
                    solutionPreferences = new SolutionPreferences();
                    preferences.Default.Add(solutionId, solutionPreferences);
                }

                // NOTE: CaptureAsync is adding data to the class (which we only have a reference to)
                var captureAsyncResult = await solution.CaptureAsync(solutionPreferences);

                if (captureAsyncResult.IsError == true)
                {
                    success = false;
                    continue;
                }
            }

            return(success ? MorphicResult.OkResult() : MorphicResult.ErrorResult());
        }
Exemplo n.º 3
0
        /// <summary>Applies the preferences to this solution.</summary>
        public async Task Apply(SolutionPreferences solutionPreferences)
        {
            bool captureCurrent = solutionPreferences.Previous != null;

            foreach (SettingGroup group in this.SettingGroups)
            {
                Values         values   = new Values();
                List <Setting>?settings = captureCurrent ? null : new List <Setting>();

                foreach ((string settingId, object?value) in solutionPreferences.Values)
                {
                    if (group.TryGetSetting(settingId, out Setting? setting))
                    {
                        settings?.Add(setting);
                        values.Add(setting !, value);
                    }
                }

                if (settings != null)
                {
                    await group.SettingsHandler.Get(group, settings);
                }

                await group.SettingsHandler.Set(group, values);
            }
        }
Exemplo n.º 4
0
        /// <summary>Captures the preferences of this solution.</summary>
        public async Task Capture(SolutionPreferences solutionPreferences)
        {
            foreach (SettingGroup settings in this.SettingGroups)
            {
                Values values = await settings.GetAll();

                foreach ((Setting setting, object?value) in values)
                {
                    solutionPreferences.Values.Add(setting.Id, value);
                }
            }
        }
        public async Task IniGet()
        {
            TestIni ini = new(TestContent);

            ServiceCollection serviceCollection = TestUtil.GetTestServices();

            serviceCollection.ReplaceTransient <Ini>(s => ini);
            IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();

            Solutions sr = Solutions.FromFile(serviceProvider, TestUtil.GetLocalFile("..\\test-solutions.json5"));

            // Capture the preferences.
            Solution            solution            = sr.GetSolution("ini");
            SolutionPreferences solutionPreferences = new SolutionPreferences();

            var captureAsyncResult = await solution.CaptureAsync(solutionPreferences);

            if (captureAsyncResult.IsError == true)
            {
                throw new Exception("CaptureAsync failed with an error");
            }

            // The correct file path is used
            Assert.Equal("test-file.ini", ini.FilePath);

            Dictionary <string, string> expect = TestUtil.StringToDictionary(@"
settingA=valueOneA
settingB=valueTwoA
settingC=valueThreeA
settingD=valueOneB
settingE=valueTwoB
settingF=valueT1
settingG=valueOneParent
settingH=valueTwoParent
settingI=valueT2
settingJ=valueOneChild
settingK=valueTwoChild
settingL=valueT3
");

            Dictionary <string, string> actual =
                solutionPreferences.Values.ToDictionary(kv => kv.Key, kv => kv.Value?.ToString() ?? string.Empty);

            // Check the correct values are read.
            TestUtil.CompareDictionaries(expect, actual, this.output.WriteLine);
        }
Exemplo n.º 6
0
        /// <summary>Applies the preferences to this solution.</summary>
        public async Task <MorphicResult <MorphicUnit, MorphicUnit> > ApplyAsync(SolutionPreferences solutionPreferences)
        {
            var success = true;

            bool captureCurrent = solutionPreferences.Previous is not null;

            foreach (SettingGroup group in this.SettingGroups)
            {
                Values         values   = new Values();
                List <Setting>?settings = captureCurrent ? null : new List <Setting>();

                foreach ((string settingId, object?value) in solutionPreferences.Values)
                {
                    if (group.TryGetSetting(settingId, out Setting? setting))
                    {
                        settings?.Add(setting);
                        values.Add(setting !, value);
                    }
                }

                if (settings is not null)
                {
                    // OBSERVATION: unsure why we are not capturing the values here; does this "Get" function have a side-effect we're trying to take advantage of (perhaps caching)?
                    var(settingHandlerGetResult, _) = await group.SettingsHandler.GetAsync(group, settings);

                    if (settingHandlerGetResult.IsError == true)
                    {
                        success = false;
                    }
                    // NOTE: if this failed: unsure if we should "continue;" here and skip the setting...or proceed to setting it
                }

                var setResult = await group.SettingsHandler.SetAsync(group, values);

                if (setResult.IsError == true)
                {
                    success = false;
                }
            }

            return(success ? MorphicResult.OkResult() : MorphicResult.ErrorResult());
        }
Exemplo n.º 7
0
        /// <summary>Captures the preferences of this solution.</summary>
        public async Task <MorphicResult <MorphicUnit, MorphicUnit> > CaptureAsync(SolutionPreferences solutionPreferences)
        {
            var success = true;

            foreach (SettingGroup settings in this.SettingGroups)
            {
                var(getResult, values) = await settings.GetAllAsync();

                if (getResult.IsError == true)
                {
                    success = false;
                    // NOTE: we will continue capturing the values we _could_ get, even if some failed
                }
                foreach ((Setting setting, object?value) in values)
                {
                    // NOTE: in our testing, sometimes the setting was already present...so we can't be adding it twice.
                    solutionPreferences.Values[setting.Id] = value;
                }
            }

            return(success ? MorphicResult.OkResult() : MorphicResult.ErrorResult());
        }
        public async Task IniSet()
        {
            TestIni writer = new(TestUtil.ReadLocalFile("handler-test.ini"));

            ServiceProvider serviceProvider = TestUtil.GetTestServices()
                                              .ReplaceTransient <Ini>(s => writer)
                                              .BuildServiceProvider();

            Solutions sr = Solutions.FromFile(serviceProvider, TestUtil.GetLocalFile("..\\test-solutions.json5"));

            Solution            solution            = sr.GetSolution("ini");
            SolutionPreferences solutionPreferences = new SolutionPreferences();

            // Set the new values
            Dictionary <string, string> newValues = TestUtil.StringToDictionary(@"
settingA=value-UPDATED-OneA
settingC=value-UPDATED-ThreeA
settingD=value-UPDATED-OneB
settingF=value-UPDATED-T1
settingG=value-UPDATED-OneParent
settingI=value-UPDATED-T2
settingJ=value-UPDATED-OneChild
settingL=value-UPDATED-T3
");

            foreach ((string?key, string?value) in newValues)
            {
                solutionPreferences.Values.Add(key, value);
            }

            // Apply the preferences.
            await solution.ApplyAsync(solutionPreferences);

            // Check the correct file path is used
            Assert.Equal("test-file.ini", writer.FilePath);
            // Check the file was saved
            Assert.True(writer.Saved, "INI file was saved");

            string iniContent = writer.Result !;

            // Check the new content is correct.
            string expected = @"ignoreA=AA
key1=value-UPDATED-OneA
key2=valueTwoA
key3=value-UPDATED-ThreeA
[test1]
ignoreB=BB
key1=value-UPDATED-OneB
key2=valueTwoB
keyT1=value-UPDATED-T1
[test2]
ignoreC=CC
key1=value-UPDATED-OneParent
key2=valueTwoParent
keyT2=value-UPDATED-T2
[test3]
ignoreD=DD
key1=value-UPDATED-OneChild
key2=valueTwoChild
keyT3=value-UPDATED-T3
";

            //File.WriteAllText(TestUtil.GetLocalFile("out.ini"), iniContent);
            Assert.Equal(expected, iniContent);

            return;

            ;
        }
Exemplo n.º 9
0
 public SolutionConfiguration(SolutionPreferences preferences)
 {
     this.preferences = preferences;
 }