From 1d690a148fcec47d7cae5f44edab2215d2d55d2b Mon Sep 17 00:00:00 2001 From: Cory Knox Date: Wed, 31 Aug 2022 18:11:32 -0700 Subject: [PATCH] (#494) Watch Chocolatey config file for changes When Chocolatey CLI makes changes to the configuration file while Chocolatey GUI is running, Chocolatey GUI should refresh the sources so that they're accurate. This adds a FileSystemWatcher to monitor the configuration file and update the GUI if there's updates to the file. --- .../Bootstrapper.cs | 1 + .../ChocolateyGui.Common.Windows.csproj | 3 +- .../Services/ConfigFileWatcher.cs | 53 +++++++++++++++++++ .../Startup/ChocolateyGuiModule.cs | 1 + .../ChocolateyGui.Common.csproj | 3 +- .../Services/IConfigFileWatcher.cs | 13 +++++ 6 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs create mode 100644 Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs diff --git a/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs b/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs index 86c62c5c6..d786f095b 100644 --- a/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs +++ b/Source/ChocolateyGui.Common.Windows/Bootstrapper.cs @@ -121,6 +121,7 @@ protected override async void OnStartup(object sender, StartupEventArgs e) var packageService = Container.Resolve(); var features = await packageService.GetFeatures(); + var fileSystemWatcher = Container.Resolve(); var backgroundFeature = features.FirstOrDefault(feature => string.Equals(feature.Name, "useBackgroundService", StringComparison.OrdinalIgnoreCase)); var elevationProvider = Elevation.Instance; diff --git a/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj b/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj index 016ce723e..852f7ac20 100644 --- a/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj +++ b/Source/ChocolateyGui.Common.Windows/ChocolateyGui.Common.Windows.csproj @@ -1,4 +1,4 @@ - + @@ -229,6 +229,7 @@ + diff --git a/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs b/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs new file mode 100644 index 000000000..b34c20a39 --- /dev/null +++ b/Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs @@ -0,0 +1,53 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright 2017 - Present Chocolatey Software, LLC +// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC +// +// -------------------------------------------------------------------------------------------------------------------- + +using System.IO; +using Caliburn.Micro; +using chocolatey.infrastructure.app.configuration; +using chocolatey.infrastructure.services; +using ChocolateyGui.Common.Models.Messages; +using ChocolateyGui.Common.Services; +using IFileSystem = chocolatey.infrastructure.filesystem.IFileSystem; + +namespace ChocolateyGui.Common.Windows.Services +{ + public class ConfigFileWatcher : IConfigFileWatcher + { + private readonly IFileSystem _fileSystem; + private readonly IEventAggregator _eventAggregator; + private readonly FileSystemWatcher _fileSystemWatcher; + private readonly IXmlService _xmlService; + private readonly string _configFile = chocolatey.infrastructure.app.ApplicationParameters.GlobalConfigFileLocation; + private int _lastKnownConfigFileHash; + + public ConfigFileWatcher(IFileSystem fileSystem, IEventAggregator eventAggregator, IXmlService xmlService) + { + _fileSystem = fileSystem; + _eventAggregator = eventAggregator; + _xmlService = xmlService; + _fileSystemWatcher = new FileSystemWatcher + { + Path = _fileSystem.get_directory_name(_configFile), + Filter = _fileSystem.get_file_name(_configFile), + NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName, + }; + _fileSystemWatcher.Changed += ConfigFileChanged; + _fileSystemWatcher.EnableRaisingEvents = true; + _lastKnownConfigFileHash = _xmlService.deserialize(_configFile).GetHashCode(); + } + + private void ConfigFileChanged(object sender, FileSystemEventArgs e) + { + var currentSettingsHash = _xmlService.deserialize(_configFile).GetHashCode(); + if (currentSettingsHash != _lastKnownConfigFileHash) + { + _eventAggregator.PublishOnUIThread(new SourcesUpdatedMessage()); + _lastKnownConfigFileHash = currentSettingsHash; + } + } + } +} \ No newline at end of file diff --git a/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs b/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs index e19bcf522..d80cca943 100644 --- a/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs +++ b/Source/ChocolateyGui.Common.Windows/Startup/ChocolateyGuiModule.cs @@ -52,6 +52,7 @@ protected override void Load(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); // Register ViewModels builder.RegisterAssemblyTypes(viewModelAssembly) diff --git a/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj b/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj index 6630edf82..1968c7bce 100644 --- a/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj +++ b/Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj @@ -1,4 +1,4 @@ - + @@ -185,6 +185,7 @@ + diff --git a/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs b/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs new file mode 100644 index 000000000..46a0eeda0 --- /dev/null +++ b/Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs @@ -0,0 +1,13 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright 2017 - Present Chocolatey Software, LLC +// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ChocolateyGui.Common.Services +{ + public interface IConfigFileWatcher + { + } +} \ No newline at end of file