Skip to content

Commit

Permalink
(#494) Watch Chocolatey config file for changes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
corbob committed Sep 2, 2022
1 parent 583b1b9 commit 1d690a1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions Source/ChocolateyGui.Common.Windows/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected override async void OnStartup(object sender, StartupEventArgs e)

var packageService = Container.Resolve<IChocolateyService>();
var features = await packageService.GetFeatures();
var fileSystemWatcher = Container.Resolve<IConfigFileWatcher>();

var backgroundFeature = features.FirstOrDefault(feature => string.Equals(feature.Name, "useBackgroundService", StringComparison.OrdinalIgnoreCase));
var elevationProvider = Elevation.Instance;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -229,6 +229,7 @@
</Compile>
<Compile Include="Controls\Dialogs\IClosableDialog.cs" />
<Compile Include="Controls\Dialogs\IClosableChildWindow.cs" />
<Compile Include="Services\ConfigFileWatcher.cs" />
<Compile Include="Services\DialogService.cs" />
<Compile Include="Services\IDialogService.cs" />
<Compile Include="Utilities\ChocolateyMessageBox.cs" />
Expand Down
53 changes: 53 additions & 0 deletions Source/ChocolateyGui.Common.Windows/Services/ConfigFileWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="ConfigFileWatcher.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

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<ConfigFileSettings>(_configFile).GetHashCode();
}

private void ConfigFileChanged(object sender, FileSystemEventArgs e)
{
var currentSettingsHash = _xmlService.deserialize<ConfigFileSettings>(_configFile).GetHashCode();
if (currentSettingsHash != _lastKnownConfigFileHash)
{
_eventAggregator.PublishOnUIThread(new SourcesUpdatedMessage());
_lastKnownConfigFileHash = currentSettingsHash;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ protected override void Load(ContainerBuilder builder)
builder.RegisterType<DotNetFileSystem>().As<chocolatey.infrastructure.filesystem.IFileSystem>().SingleInstance();
builder.RegisterType<PackageArgumentsService>().As<IPackageArgumentsService>().SingleInstance();
builder.RegisterType<DefaultEncryptionUtility>().As<IEncryptionUtility>().SingleInstance();
builder.RegisterType<ConfigFileWatcher>().As<IConfigFileWatcher>().SingleInstance();

// Register ViewModels
builder.RegisterAssemblyTypes(viewModelAssembly)
Expand Down
3 changes: 2 additions & 1 deletion Source/ChocolateyGui.Common/ChocolateyGui.Common.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -185,6 +185,7 @@
<Compile Include="Services\IAllowedCommandsService.cs" />
<Compile Include="Services\IChocolateyGuiCacheService.cs" />
<Compile Include="Services\IChocolateyService.cs" />
<Compile Include="Services\IConfigFileWatcher.cs" />
<Compile Include="Services\IConfigService.cs" />
<Compile Include="Services\IFileStorageService.cs" />
<Compile Include="Services\IPersistenceService.cs" />
Expand Down
13 changes: 13 additions & 0 deletions Source/ChocolateyGui.Common/Services/IConfigFileWatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Chocolatey" file="IConfigFileWatcher.cs">
// Copyright 2017 - Present Chocolatey Software, LLC
// Copyright 2014 - 2017 Rob Reynolds, the maintainers of Chocolatey, and RealDimensions Software, LLC
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace ChocolateyGui.Common.Services
{
public interface IConfigFileWatcher
{
}
}

0 comments on commit 1d690a1

Please sign in to comment.