Skip to content

Commit

Permalink
Fix chocolatey not properly reporting available package versions
Browse files Browse the repository at this point in the history
  • Loading branch information
marticliment committed Jun 14, 2024
1 parent 74109e5 commit b2958a1
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 26 deletions.
3 changes: 2 additions & 1 deletion src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,7 +56,7 @@ public Chocolatey() : base()
};

SourceProvider = new ChocolateySourceProvider(this);

PackageDetailsProvider = new ChocolateyDetailsProvider(this);
}

protected override async Task<Package[]> GetAvailableUpdates_UnSafe()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<string[]> 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<string> 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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace UniGetUI.PackageEngine.Managers.PowerShellManager
{
internal class BaseNuGetDetailsProvider : BasePackageDetailsProvider<PackageManager>
public class BaseNuGetDetailsProvider : BasePackageDetailsProvider<PackageManager>
{
public BaseNuGetDetailsProvider(BaseNuGet manager) : base(manager) { }

Expand Down Expand Up @@ -129,36 +129,35 @@ protected override Task<Uri[]> GetPackageScreenshots_Unsafe(Package package)
protected override async Task<string[]> 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<string> results = new();

List<string> results = new();
HashSet<string> 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<string> 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();
}
}
}

0 comments on commit b2958a1

Please sign in to comment.