diff --git a/Source/ChocolateyGui/Services/ChocolateyService.cs b/Source/ChocolateyGui/Services/ChocolateyService.cs index 19b460ee2..fe8ef3b51 100644 --- a/Source/ChocolateyGui/Services/ChocolateyService.cs +++ b/Source/ChocolateyGui/Services/ChocolateyService.cs @@ -32,6 +32,7 @@ internal class ChocolateyService : IChocolateyService private readonly IMapper _mapper; private readonly IProgressService _progressService; private readonly IChocolateyConfigSettingsService _configSettingsService; + private GetChocolatey _choco; #pragma warning disable SA1401 // Fields must be private #pragma warning restore SA1401 // Fields must be private @@ -40,6 +41,7 @@ public ChocolateyService(IMapper mapper, IProgressService progressService, IChoc _mapper = mapper; _progressService = progressService; _configSettingsService = configSettingsService; + _choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); } public Task IsElevated() @@ -51,16 +53,15 @@ public async Task> GetInstalledPackages() { using (await Lock.ReadLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = CommandNameType.list.ToString(); config.ListCommand.LocalOnly = true; }); - return (await choco.ListAsync()) - .Select(package => GetMappedPackage(choco, package, _mapper, true)) + return (await _choco.ListAsync()) + .Select(package => GetMappedPackage(_choco, package, _mapper, true)) .ToArray(); } } @@ -69,8 +70,7 @@ public async Task>> GetOutdatedPack { using (await Lock.ReadLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "outdated"; @@ -80,9 +80,9 @@ public async Task>> GetOutdatedPack config.QuietOutput = true; config.Prerelease = false; }); - var chocoConfig = choco.GetConfiguration(); + var chocoConfig = _choco.GetConfiguration(); - var nugetService = choco.Container().GetInstance(); + var nugetService = _choco.Container().GetInstance(); var packages = await Task.Run(() => nugetService.upgrade_noop(chocoConfig, null)); var results = packages .Where(p => !p.Value.Inconclusive) @@ -149,8 +149,7 @@ public async Task Search(string query, PackageSearchOptions opti { using (await Lock.ReadLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = CommandNameType.list.ToString(); @@ -175,13 +174,13 @@ public async Task Search(string query, PackageSearchOptions opti }); var packages = - (await choco.ListAsync()).Select( - pckge => GetMappedPackage(choco, pckge, _mapper)); + (await _choco.ListAsync()).Select( + pckge => GetMappedPackage(_choco, pckge, _mapper)); return new PackageResults { Packages = packages.ToArray(), - TotalCount = await Task.Run(() => choco.ListCount()) + TotalCount = await Task.Run(() => _choco.ListCount()) }; } } @@ -190,8 +189,7 @@ public async Task GetByVersionAndIdAsync(string id, string version, boo { using (await Lock.ReadLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "list"; @@ -204,9 +202,9 @@ public async Task GetByVersionAndIdAsync(string id, string version, boo config.Verbose = false; #endif // DEBUG }); - var chocoConfig = choco.GetConfiguration(); + var chocoConfig = _choco.GetConfiguration(); - var nugetLogger = choco.Container().GetInstance(); + var nugetLogger = _choco.Container().GetInstance(); var semvar = new SemanticVersion(version); var nugetPackage = (NugetList.GetPackages(chocoConfig, nugetLogger) as IQueryable).FirstOrDefault(p => p.Version == semvar); if (nugetPackage == null) @@ -214,7 +212,7 @@ public async Task GetByVersionAndIdAsync(string id, string version, boo throw new Exception("No Package Found"); } - return GetMappedPackage(choco, new PackageResult(nugetPackage, null, chocoConfig.Sources), _mapper); + return GetMappedPackage(_choco, new PackageResult(nugetPackage, null, chocoConfig.Sources), _mapper); } } @@ -263,8 +261,7 @@ public async Task PinPackage(string id, string version) { using (await Lock.WriteLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "pin"; @@ -275,7 +272,7 @@ public async Task PinPackage(string id, string version) try { - await choco.RunAsync(); + await _choco.RunAsync(); } catch (Exception ex) { @@ -290,8 +287,7 @@ public async Task UnpinPackage(string id, string version { using (await Lock.WriteLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "pin"; @@ -301,7 +297,7 @@ public async Task UnpinPackage(string id, string version }); try { - await choco.RunAsync(); + await _choco.RunAsync(); } catch (Exception ex) { @@ -325,8 +321,7 @@ public async Task SetFeature(ChocolateyFeature feature) { using (await Lock.WriteLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "feature"; @@ -334,7 +329,7 @@ public async Task SetFeature(ChocolateyFeature feature) config.FeatureCommand.Name = feature.Name; }); - await choco.RunAsync(); + await _choco.RunAsync(); } } @@ -351,8 +346,7 @@ public async Task SetSetting(ChocolateySetting setting) { using (await Lock.WriteLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "config"; @@ -361,7 +355,7 @@ public async Task SetSetting(ChocolateySetting setting) config.ConfigCommand.ConfigValue = setting.Value; }); - await choco.RunAsync(); + await _choco.RunAsync(); } } @@ -379,8 +373,7 @@ public async Task AddSource(ChocolateySource source) { using (await Lock.WriteLockAsync()) { - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "source"; @@ -397,29 +390,29 @@ public async Task AddSource(ChocolateySource source) config.SourceCommand.VisibleToAdminsOnly = source.VisibleToAdminsOnly; }); - await choco.RunAsync(); + await _choco.RunAsync(); if (source.Disabled) { - choco.Set( + _choco.Set( config => { config.CommandName = "source"; config.SourceCommand.Command = SourceCommandType.disable; config.SourceCommand.Name = source.Id; }); - await choco.RunAsync(); + await _choco.RunAsync(); } else { - choco.Set( + _choco.Set( config => { config.CommandName = "source"; config.SourceCommand.Command = SourceCommandType.enable; config.SourceCommand.Name = source.Id; }); - await choco.RunAsync(); + await _choco.RunAsync(); } } } @@ -446,8 +439,7 @@ public async Task RemoveSource(string id) return false; } - var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService)); - choco.Set( + _choco.Set( config => { config.CommandName = "source"; @@ -455,7 +447,7 @@ public async Task RemoveSource(string id) config.SourceCommand.Name = id; }); - await choco.RunAsync(); + await _choco.RunAsync(); return true; } } @@ -520,8 +512,7 @@ private async Task RunCommand(GetChocolatey choco, Seril private async Task GetConfigFile() { - var choco = Lets.GetChocolatey(); - var xmlService = choco.Container().GetInstance(); + var xmlService = _choco.Container().GetInstance(); var config = await Task.Run( () => xmlService.deserialize(ApplicationParameters.GlobalConfigFileLocation));