diff --git a/src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs b/src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs index 6b98f9897..7291bb5a7 100644 --- a/src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs +++ b/src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs @@ -7,6 +7,7 @@ using UniGetUI.PackageEngine.Classes.Manager.ManagerHelpers; using UniGetUI.PackageEngine.Enums; using UniGetUI.PackageEngine.ManagerClasses.Manager; +using UniGetUI.PackageEngine.Managers.Chocolatey; using UniGetUI.PackageEngine.Managers.PowerShellManager; using UniGetUI.PackageEngine.PackageClasses; @@ -55,7 +56,7 @@ public Chocolatey() : base() }; SourceProvider = new ChocolateySourceProvider(this); - + PackageDetailsProvider = new ChocolateyDetailsProvider(this); } protected override async Task GetAvailableUpdates_UnSafe() diff --git a/src/UniGetUI.PackageEngine.Managers.Chocolatey/ChocolateyDetailsProvider.cs b/src/UniGetUI.PackageEngine.Managers.Chocolatey/ChocolateyDetailsProvider.cs new file mode 100644 index 000000000..3f8016969 --- /dev/null +++ b/src/UniGetUI.PackageEngine.Managers.Chocolatey/ChocolateyDetailsProvider.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using UniGetUI.Core.Data; +using UniGetUI.Core.Logging; +using UniGetUI.PackageEngine.Enums; +using UniGetUI.PackageEngine.ManagerClasses.Manager; +using UniGetUI.PackageEngine.Managers.PowerShellManager; +using UniGetUI.PackageEngine.PackageClasses; + +namespace UniGetUI.PackageEngine.Managers.Chocolatey +{ + public class ChocolateyDetailsProvider : BaseNuGetDetailsProvider + { + public ChocolateyDetailsProvider(BaseNuGet manager) : base(manager) + { } + + protected override async Task GetPackageVersions_Unsafe(Package package) + { + Process p = new() + { + StartInfo = new ProcessStartInfo() + { + // choco search php --exact --all-versions + FileName = Manager.Status.ExecutablePath, + Arguments = Manager.Properties.ExecutableCallArgs + $" search {package.Id} --exact --all-versions", + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + RedirectStandardInput = true, + CreateNoWindow = true, + StandardOutputEncoding = System.Text.Encoding.UTF8 + } + }; + + var logger = Manager.TaskLogger.CreateNew(LoggableTaskType.LoadPackageVersions, p); + + p.Start(); + + string? line; + List versions = new(); + while ((line = await p.StandardOutput.ReadLineAsync()) != null) + { + logger.AddToStdOut(line); + if (line.Contains("[Approved]")) + versions.Add(line.Split(' ')[1].Trim()); + } + logger.AddToStdErr(await p.StandardError.ReadToEndAsync()); + await p.WaitForExitAsync(); + logger.Close(p.ExitCode); + + return versions.ToArray(); + } + } +} diff --git a/src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGetDetailsProvider.cs b/src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGetDetailsProvider.cs index 60357ec8f..f3050c8a6 100644 --- a/src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGetDetailsProvider.cs +++ b/src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGetDetailsProvider.cs @@ -13,7 +13,7 @@ namespace UniGetUI.PackageEngine.Managers.PowerShellManager { - internal class BaseNuGetDetailsProvider : BasePackageDetailsProvider + public class BaseNuGetDetailsProvider : BasePackageDetailsProvider { public BaseNuGetDetailsProvider(BaseNuGet manager) : base(manager) { } @@ -129,36 +129,35 @@ protected override Task GetPackageScreenshots_Unsafe(Package package) protected override async Task GetPackageVersions_Unsafe(Package package) { Uri SearchUrl = new($"{package.Source.Url}/FindPackagesById()?id='{package.Id}'"); - Logger.Debug($"Begin package version search with url={SearchUrl} on manager {Manager.Name}"); + Logger.Debug($"Begin package version search with url={SearchUrl} on manager {Manager.Name}"); - using (HttpClient client = new(CoreData.GenericHttpClientParameters)) - { - client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString); - HttpResponseMessage response = await client.GetAsync(SearchUrl); - - if (!response.IsSuccessStatusCode) - { - Logger.Warn($"Failed to fetch api at Url={SearchUrl} with status code {response.StatusCode} to load versions"); - return []; - } - - string SearchResults = await response.Content.ReadAsStringAsync(); - MatchCollection matches = Regex.Matches(SearchResults, "Version='([^<>']+)'"); + List results = new(); - List results = new(); - HashSet alreadyProcessed = new(); + HttpClient client = new(CoreData.GenericHttpClientParameters); + client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString); - foreach (Match match in matches) - if(!alreadyProcessed.Contains(match.Groups[1].Value) && match.Success) - { - results.Add(match.Groups[1].Value); - alreadyProcessed.Add(match.Groups[1].Value); - } + HttpResponseMessage response = await client.GetAsync(SearchUrl); + if (!response.IsSuccessStatusCode) + { + Logger.Warn($"Failed to fetch api at Url={SearchUrl} with status code {response.StatusCode} to load versions"); + return []; + } - results.Reverse(); - return results.ToArray(); + string SearchResults = await response.Content.ReadAsStringAsync(); + HashSet alreadyProcessed = new(); + MatchCollection matches = Regex.Matches(SearchResults, "Version='([^<>']+)'"); + foreach (Match match in matches) + { + if (!alreadyProcessed.Contains(match.Groups[1].Value) && match.Success) + { + results.Add(match.Groups[1].Value); + alreadyProcessed.Add(match.Groups[1].Value); + } } + + results.Sort(StringComparer.OrdinalIgnoreCase); + return results.ToArray(); } } }