Skip to content

Commit

Permalink
(chocolateyGH-607) Removed multiple calls to Lets.GetChocolatey
Browse files Browse the repository at this point in the history
- Instead, use a single instance across the application
- There are still some remaining all that could be removed, but more work is required here
- Need to capture logs from errors for reporting
  • Loading branch information
gep13 committed Jun 19, 2018
1 parent 872a37e commit 1acd84f
Showing 1 changed file with 33 additions and 42 deletions.
75 changes: 33 additions & 42 deletions Source/ChocolateyGui/Services/ChocolateyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<bool> IsElevated()
Expand All @@ -51,16 +53,15 @@ public async Task<IEnumerable<Package>> 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<PackageResult>())
.Select(package => GetMappedPackage(choco, package, _mapper, true))
return (await _choco.ListAsync<PackageResult>())
.Select(package => GetMappedPackage(_choco, package, _mapper, true))
.ToArray();
}
}
Expand All @@ -69,8 +70,7 @@ public async Task<IReadOnlyList<Tuple<string, SemanticVersion>>> GetOutdatedPack
{
using (await Lock.ReadLockAsync())
{
var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService));
choco.Set(
_choco.Set(
config =>
{
config.CommandName = "outdated";
Expand All @@ -80,9 +80,9 @@ public async Task<IReadOnlyList<Tuple<string, SemanticVersion>>> GetOutdatedPack
config.QuietOutput = true;
config.Prerelease = false;
});
var chocoConfig = choco.GetConfiguration();
var chocoConfig = _choco.GetConfiguration();

var nugetService = choco.Container().GetInstance<INugetService>();
var nugetService = _choco.Container().GetInstance<INugetService>();
var packages = await Task.Run(() => nugetService.upgrade_noop(chocoConfig, null));
var results = packages
.Where(p => !p.Value.Inconclusive)
Expand Down Expand Up @@ -149,8 +149,7 @@ public async Task<PackageResults> 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();
Expand All @@ -175,13 +174,13 @@ public async Task<PackageResults> Search(string query, PackageSearchOptions opti
});

var packages =
(await choco.ListAsync<PackageResult>()).Select(
pckge => GetMappedPackage(choco, pckge, _mapper));
(await _choco.ListAsync<PackageResult>()).Select(
pckge => GetMappedPackage(_choco, pckge, _mapper));

return new PackageResults
{
Packages = packages.ToArray(),
TotalCount = await Task.Run(() => choco.ListCount())
TotalCount = await Task.Run(() => _choco.ListCount())
};
}
}
Expand All @@ -190,8 +189,7 @@ public async Task<Package> 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";
Expand All @@ -204,17 +202,17 @@ public async Task<Package> GetByVersionAndIdAsync(string id, string version, boo
config.Verbose = false;
#endif // DEBUG
});
var chocoConfig = choco.GetConfiguration();
var chocoConfig = _choco.GetConfiguration();

var nugetLogger = choco.Container().GetInstance<NuGet.ILogger>();
var nugetLogger = _choco.Container().GetInstance<NuGet.ILogger>();
var semvar = new SemanticVersion(version);
var nugetPackage = (NugetList.GetPackages(chocoConfig, nugetLogger) as IQueryable<IPackage>).FirstOrDefault(p => p.Version == semvar);
if (nugetPackage == null)
{
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);
}
}

Expand Down Expand Up @@ -263,8 +261,7 @@ public async Task<PackageOperationResult> 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";
Expand All @@ -275,7 +272,7 @@ public async Task<PackageOperationResult> PinPackage(string id, string version)

try
{
await choco.RunAsync();
await _choco.RunAsync();
}
catch (Exception ex)
{
Expand All @@ -290,8 +287,7 @@ public async Task<PackageOperationResult> 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";
Expand All @@ -301,7 +297,7 @@ public async Task<PackageOperationResult> UnpinPackage(string id, string version
});
try
{
await choco.RunAsync();
await _choco.RunAsync();
}
catch (Exception ex)
{
Expand All @@ -325,16 +321,15 @@ 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";
config.FeatureCommand.Command = feature.Enabled ? FeatureCommandType.enable : FeatureCommandType.disable;
config.FeatureCommand.Name = feature.Name;
});

await choco.RunAsync();
await _choco.RunAsync();
}
}

Expand All @@ -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";
Expand All @@ -361,7 +355,7 @@ public async Task SetSetting(ChocolateySetting setting)
config.ConfigCommand.ConfigValue = setting.Value;
});

await choco.RunAsync();
await _choco.RunAsync();
}
}

Expand All @@ -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";
Expand All @@ -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();
}
}
}
Expand All @@ -446,16 +439,15 @@ public async Task<bool> RemoveSource(string id)
return false;
}

var choco = Lets.GetChocolatey().SetCustomLogging(new SerilogLogger(Logger, _progressService));
choco.Set(
_choco.Set(
config =>
{
config.CommandName = "source";
config.SourceCommand.Command = SourceCommandType.remove;
config.SourceCommand.Name = id;
});

await choco.RunAsync();
await _choco.RunAsync();
return true;
}
}
Expand Down Expand Up @@ -520,8 +512,7 @@ private async Task<PackageOperationResult> RunCommand(GetChocolatey choco, Seril

private async Task<ConfigFileSettings> GetConfigFile()
{
var choco = Lets.GetChocolatey();
var xmlService = choco.Container().GetInstance<IXmlService>();
var xmlService = _choco.Container().GetInstance<IXmlService>();
var config =
await Task.Run(
() => xmlService.deserialize<ConfigFileSettings>(ApplicationParameters.GlobalConfigFileLocation));
Expand Down

0 comments on commit 1acd84f

Please sign in to comment.