Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#494) Refresh sources from file #955

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
gep13 marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class ChocolateyService : IChocolateyService
private static readonly AsyncReaderWriterLock Lock = new AsyncReaderWriterLock();
private readonly IMapper _mapper;
private readonly IProgressService _progressService;
private readonly IChocolateyConfigSettingsService _configSettingsService;
private readonly IXmlService _xmlService;
private readonly IFileSystem _fileSystem;
private readonly IConfigService _configService;
Expand All @@ -46,11 +45,10 @@ public class ChocolateyService : IChocolateyService
#pragma warning disable SA1401 // Fields must be private
#pragma warning restore SA1401 // Fields must be private

public ChocolateyService(IMapper mapper, IProgressService progressService, IChocolateyConfigSettingsService configSettingsService, IXmlService xmlService, IFileSystem fileSystem, IConfigService configService)
public ChocolateyService(IMapper mapper, IProgressService progressService, IXmlService xmlService, IFileSystem fileSystem, IConfigService configService)
{
_mapper = mapper;
_progressService = progressService;
_configSettingsService = configSettingsService;
_xmlService = xmlService;
_fileSystem = fileSystem;
_configService = configService;
Expand Down Expand Up @@ -497,8 +495,8 @@ public async Task<ChocolateySource[]> GetSources()
// we need to read all information from the config file, i.e. the username and password
var config = await GetConfigFile();
var allSources = config.Sources.Select(_mapper.Map<ChocolateySource>).ToArray();

var filteredSourceIds = _configSettingsService.source_list(_choco.GetConfiguration()).Select(s => s.Id).ToArray();
var configSettingsService = new ChocolateyConfigSettingsService(_xmlService);
gep13 marked this conversation as resolved.
Show resolved Hide resolved
var filteredSourceIds = configSettingsService.source_list(_choco.GetConfiguration()).Select(s => s.Id).ToArray();

var mappedSources = allSources.Where(s => filteredSourceIds.Contains(s.Id)).ToArray();
return mappedSources;
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;
gep13 marked this conversation as resolved.
Show resolved Hide resolved
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)
gep13 marked this conversation as resolved.
Show resolved Hide resolved
{
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
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,6 @@ public async Task Save()
}

_originalId = DraftSource?.Id;
await _eventAggregator.PublishOnUIThreadAsync(new SourcesUpdatedMessage());
}
catch (UnauthorizedAccessException)
{
Expand Down Expand Up @@ -486,7 +485,6 @@ public async Task Remove()
await _chocolateyService.RemoveSource(_originalId);
Sources.Remove(SelectedSource);
SelectedSource = null;
await _eventAggregator.PublishOnUIThreadAsync(new SourcesUpdatedMessage());
}
catch (UnauthorizedAccessException)
{
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
{
}
}