Skip to content

Commit d9298ec

Browse files
authored
Merge pull request #3337 from marticliment/nuget-updates-testing
2 parents 8c59b05 + 289bd70 commit d9298ec

8 files changed

Lines changed: 85 additions & 335 deletions

File tree

src/UniGetUI.Core.Data/Licenses.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public static class LicenseData
2525
{"parse_pip_search", "MIT"},
2626
{"PowerShell Gallery", "Unknown"},
2727
{".NET SDK", "MIT"},
28-
{"dotnet-tools-outdated", "MIT"},
2928
{"Cargo", "MIT"},
3029
{"cargo-binstall", "GPL-3.0-only"},
3130
{"cargo-update", "MIT"},
@@ -59,7 +58,6 @@ public static class LicenseData
5958
{"Pip", new Uri("https://github.com/pypa/pip/blob/main/LICENSE.txt")},
6059
{"parse_pip_search", new Uri("https://github.com/marticliment/parseable_pip_search/blob/master/LICENSE.md")},
6160
{".NET SDK", new Uri("https://github.com/dotnet/sdk/blob/main/LICENSE.TXT")},
62-
{"dotnet-tools-outdated", new Uri("https://github.com/rychlym/dotnet-tools-outdated/blob/master/LICENSE")},
6361
{"PowerShell Gallery", new Uri("https://www.powershellgallery.com/")},
6462
{"Cargo", new Uri("https://github.com/rust-lang/cargo/blob/master/LICENSE-MIT")},
6563
{"cargo-binstall", new Uri("https://spdx.org/licenses/GPL-3.0-only.html")},
@@ -94,7 +92,6 @@ public static class LicenseData
9492
{"Pip", new Uri("https://github.com/pypa/pip/")},
9593
{"parse_pip_search", new Uri("https://github.com/marticliment/parseable_pip_search/")},
9694
{".NET SDK", new Uri("https://dotnet.microsoft.com/")},
97-
{"dotnet-tools-outdated", new Uri("https://github.com/rychlym/dotnet-tools-outdated/")},
9895
{"PowerShell Gallery", new Uri("https://www.powershellgallery.com/")},
9996
{"Cargo", new Uri("https://github.com/rust-lang/cargo")},
10097
{"cargo-binstall", new Uri("https://github.com/cargo-bins/cargo-binstall")},

src/UniGetUI.PackageEngine.Managers.Chocolatey/Chocolatey.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
117117
return Packages;
118118
}
119119

120-
protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
120+
protected override IReadOnlyList<Package> _getInstalledPackages_UnSafe()
121121
{
122122
Process p = new()
123123
{

src/UniGetUI.PackageEngine.Managers.Dotnet/DotNet.cs

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ public class DotNet : BaseNuGet
2525

2626
public DotNet()
2727
{
28-
Dependencies = [
29-
new ManagerDependency(
30-
".NET Tools Outdated",
31-
Path.Join(Environment.SystemDirectory, "windowspowershell\\v1.0\\powershell.exe"),
32-
"-ExecutionPolicy Bypass -NoLogo -NoProfile -Command \"& {dotnet tool install --global dotnet-tools-outdated --add-source https://api.nuget.org/v3/index.json; if ($error.count -ne 0){pause}}\"",
33-
"dotnet tool install --global dotnet-tools-outdated --add-source https://api.nuget.org/v3/index.json",
34-
async () => (await CoreTools.WhichAsync("dotnet-tools-outdated.exe")).Item1)
35-
];
36-
3728
Capabilities = new ManagerCapabilities
3829
{
3930
CanRunAsAdmin = true,
@@ -66,96 +57,7 @@ public DotNet()
6657
OperationHelper = new DotNetPkgOperationHelper(this);
6758
}
6859

69-
protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
70-
{
71-
var (found, path) = CoreTools.Which("dotnet-tools-outdated.exe");
72-
if (!found)
73-
{
74-
Process proc = new()
75-
{
76-
StartInfo = new ProcessStartInfo
77-
{
78-
FileName = Status.ExecutablePath,
79-
Arguments = Properties.ExecutableCallArgs + " install --global dotnet-tools-outdated",
80-
UseShellExecute = false,
81-
RedirectStandardOutput = true,
82-
RedirectStandardError = true,
83-
CreateNoWindow = true,
84-
}
85-
};
86-
87-
IProcessTaskLogger aux_logger = TaskLogger.CreateNew(LoggableTaskType.InstallManagerDependency, proc);
88-
proc.Start();
89-
90-
aux_logger.AddToStdOut(proc.StandardOutput.ReadToEnd());
91-
aux_logger.AddToStdErr(proc.StandardError.ReadToEnd());
92-
proc.WaitForExit();
93-
aux_logger.Close(proc.ExitCode);
94-
95-
path = "dotnet-tools-outdated.exe";
96-
}
97-
98-
Process p = new()
99-
{
100-
StartInfo = new ProcessStartInfo
101-
{
102-
FileName = path,
103-
Arguments = "--format json --utf8",
104-
RedirectStandardOutput = true,
105-
RedirectStandardError = true,
106-
UseShellExecute = false,
107-
CreateNoWindow = true,
108-
StandardOutputEncoding = System.Text.Encoding.UTF8
109-
}
110-
};
111-
112-
p.StartInfo = CoreTools.UpdateEnvironmentVariables(p.StartInfo);
113-
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);
114-
p.Start();
115-
116-
List<Package> Packages = [];
117-
string output = p.StandardOutput.ReadToEnd();
118-
logger.AddToStdOut(output);
119-
logger.AddToStdErr(p.StandardError.ReadToEnd());
120-
121-
while (output[0] != '{') output = output[1..];
122-
JsonArray? data = (JsonNode.Parse(output) as JsonObject)?["dotnet-tools-outdated"] as JsonArray;
123-
if (data is not null)
124-
{
125-
foreach (JsonNode? node in data)
126-
{
127-
if (node is not JsonObject element) continue;
128-
129-
bool unlisted = element["becomeUnlisted"]?.GetValue<bool>() ?? false;
130-
string id = element["packageName"]?.ToString() ?? "";
131-
string version = element["currentVer"]?.ToString() ?? "";
132-
string newVersion = element["availableVer"]?.ToString() ?? "";
133-
134-
if (unlisted)
135-
{
136-
Logger.Warn($".NET package {id} is unlisted, not showing it...");
137-
continue;
138-
};
139-
140-
Packages.Add(new(
141-
CoreTools.FormatAsName(id), id, version, newVersion, DefaultSource, this,
142-
new(PackageScope.Global)
143-
));
144-
145-
}
146-
}
147-
else
148-
{
149-
logger.AddToStdErr("\"JsonArray? data\" was null!");
150-
}
151-
152-
p.WaitForExit();
153-
logger.Close(p.ExitCode);
154-
155-
return Packages;
156-
}
157-
158-
protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
60+
protected override IReadOnlyList<Package> _getInstalledPackages_UnSafe()
15961
{
16062
List<Package> Packages = [];
16163
foreach (var options in new OverridenInstallationOptions[] { new(PackageScope.Local), new(PackageScope.Global) })

src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGet.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
using System.Text;
12
using System.Text.RegularExpressions;
23
using System.Web;
4+
using UniGetUI.Core.Classes;
35
using UniGetUI.Core.Data;
6+
using UniGetUI.Core.Logging;
47
using UniGetUI.Core.Tools;
8+
using UniGetUI.PackageEngine.Enums;
59
using UniGetUI.PackageEngine.Interfaces;
610
using UniGetUI.PackageEngine.ManagerClasses.Classes;
711
using UniGetUI.PackageEngine.ManagerClasses.Manager;
@@ -104,6 +108,79 @@ protected sealed override IReadOnlyList<Package> FindPackages_UnSafe(string quer
104108
return Packages;
105109
}
106110

111+
112+
protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
113+
{
114+
int errors = 0;
115+
var logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates);
116+
117+
var installedPackages = TaskRecycler<IReadOnlyList<IPackage>>.RunOrAttach(GetInstalledPackages);
118+
var Packages = new List<Package>();
119+
120+
Dictionary<IManagerSource, List<IPackage>> sourceMapping = new();
121+
122+
foreach (var package in installedPackages)
123+
{
124+
var uri = package.Source;
125+
if (!sourceMapping.ContainsKey(uri)) sourceMapping[uri] = new();
126+
sourceMapping[uri].Add(package);
127+
}
128+
129+
foreach (var pair in sourceMapping)
130+
{
131+
var packageIds = new StringBuilder();
132+
var packageVers = new StringBuilder();
133+
var packageIdVersion = new Dictionary<string, string>();
134+
foreach (var package in pair.Value)
135+
{
136+
packageIds.Append(package.Id + "|");
137+
packageVers.Append(package.VersionString + "|");
138+
packageIdVersion[package.Id.ToLower()] = package.VersionString;
139+
}
140+
141+
var SearchUrl = $"{pair.Key.Url.ToString().Trim('/')}/GetUpdates()" +
142+
$"?packageIds=%27{HttpUtility.UrlEncode(packageIds.ToString().Trim('|'))}%27" +
143+
$"&versions=%27{HttpUtility.UrlEncode(packageVers.ToString().Trim('|'))}%27" +
144+
$"&includePrerelease=0&includeAllVersions=0";
145+
146+
using HttpClient client = new(CoreData.GenericHttpClientParameters);
147+
client.DefaultRequestHeaders.UserAgent.ParseAdd(CoreData.UserAgentString);
148+
HttpResponseMessage response = client.GetAsync(SearchUrl).GetAwaiter().GetResult();
149+
150+
if (!response.IsSuccessStatusCode)
151+
{
152+
logger.Error($"Failed to fetch api at Url={SearchUrl} with status code {response.StatusCode}");
153+
errors++;
154+
}
155+
else
156+
{
157+
string SearchResults = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
158+
MatchCollection matches = Regex.Matches(SearchResults, "<entry>([\\s\\S]*?)<\\/entry>");
159+
160+
foreach (Match match in matches)
161+
{
162+
if (!match.Success) continue;
163+
164+
string id = Regex.Match(match.Value, "<d:Id>([^<]+)</d:Id>").Groups[1].Value;
165+
string new_version = Regex.Match(match.Value, "<d:Version>([^<]+)</d:Version>").Groups[1].Value;
166+
// Match title = Regex.Match(match.Value, "<title[ \\\"\\=A-Za-z0-9]+>([^<>]+)<\\/title>");
167+
168+
logger.Log($"Found package {id} version {new_version} on source {pair.Key.Name}");
169+
Packages.Add(new Package(CoreTools.FormatAsName(id), id, packageIdVersion[id.ToLower()], new_version, pair.Key, this));
170+
}
171+
}
172+
}
173+
174+
logger.Close(errors);
175+
return Packages;
176+
}
177+
178+
protected sealed override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
179+
=> TaskRecycler<IReadOnlyList<Package>>.RunOrAttach(_getInstalledPackages_UnSafe);
180+
181+
protected abstract IReadOnlyList<Package> _getInstalledPackages_UnSafe();
182+
183+
107184
}
108185

109186
}

src/UniGetUI.PackageEngine.Managers.PowerShell/Helpers/PowerShellPkgOperationHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ protected override OperationVeredict _getOperationResult(
5555
{
5656
string output_string = string.Join("\n", processOutput);
5757

58-
if (!package.OverridenOptions.RunAsAdministrator != true && output_string.Contains("AdminPrivilegesAreRequired"))
58+
if (package.OverridenOptions.RunAsAdministrator is not true &&
59+
(output_string.Contains("AdminPrivilegesAreRequired") || output_string.Contains("AdminPrivilegeRequired")))
5960
{
6061
package.OverridenOptions.RunAsAdministrator = true;
6162
return OperationVeredict.AutoRetry;

src/UniGetUI.PackageEngine.Managers.PowerShell/PowerShell.cs

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -55,94 +55,8 @@ public PowerShell()
5555
SourcesHelper = new PowerShellSourceHelper(this);
5656
OperationHelper = new PowerShellPkgOperationHelper(this);
5757
}
58-
protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
59-
{
60-
Process p = new()
61-
{
62-
StartInfo = new ProcessStartInfo
63-
{
64-
FileName = Status.ExecutablePath,
65-
Arguments = "-NoProfile",
66-
RedirectStandardOutput = true,
67-
RedirectStandardError = true,
68-
RedirectStandardInput = true,
69-
UseShellExecute = false,
70-
CreateNoWindow = true,
71-
StandardOutputEncoding = System.Text.Encoding.UTF8,
72-
StandardInputEncoding = new UTF8Encoding(false),
73-
}
74-
};
75-
76-
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);
77-
78-
p.Start();
79-
80-
string command = """
81-
function Test-GalleryModuleUpdate {
82-
param (
83-
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Name,
84-
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [version] $Version,
85-
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Repository,
86-
[switch] $NeedUpdateOnly
87-
)
88-
process {
89-
$URLs = @{}
90-
@(Get-PSRepository).ForEach({$URLs[$_.Name] = If ($_.Uri) {$_.Uri.AbsoluteUri} Else {$_.SourceLocation}})
91-
$page = Invoke-WebRequest -Uri ($URLs[$Repository] + "/package/$Name") -UseBasicParsing -Maximum 0 -ea Ignore
92-
[version]$latest = Split-Path -Path ($page.Headers.Location -replace "$Name." -replace ".nupkg") -Leaf
93-
$needsupdate = $Latest -gt $Version
94-
if ($needsupdate) {
95-
Write-Output($Name + "|" + $Version.ToString() + "|" + $Latest.ToString() + "|" + $Repository)
96-
}
97-
}
98-
}
99-
Get-InstalledModule | Test-GalleryModuleUpdate
100-
101-
102-
exit
103-
""";
104-
p.StandardInput.WriteLine(command);
105-
logger.AddToStdIn(command);
106-
p.StandardInput.Close();
107-
108-
string? line;
109-
List<Package> Packages = [];
110-
while ((line = p.StandardOutput.ReadLine()) is not null)
111-
{
112-
logger.AddToStdOut(line);
113-
if (line.StartsWith(">>"))
114-
{
115-
continue;
116-
}
117-
118-
string[] elements = line.Split('|');
119-
if (elements.Length < 4)
120-
{
121-
continue;
122-
}
123-
124-
for (int i = 0; i < elements.Length; i++)
125-
{
126-
elements[i] = elements[i].Trim();
127-
}
128-
129-
if (elements[1] + ".0" == elements[2] || elements[1] + ".0.0" == elements[2])
130-
{
131-
continue;
132-
}
133-
134-
Packages.Add(new Package(CoreTools.FormatAsName(elements[0]), elements[0], elements[1],
135-
elements[2], SourcesHelper.Factory.GetSourceOrDefault(elements[3]), this));
136-
}
137-
138-
logger.AddToStdErr(p.StandardError.ReadToEnd());
139-
p.WaitForExit();
140-
logger.Close(p.ExitCode);
141-
142-
return Packages;
143-
}
14458

145-
protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
59+
protected override IReadOnlyList<Package> _getInstalledPackages_UnSafe()
14660
{
14761
Process p = new()
14862
{

src/UniGetUI.PackageEngine.Managers.PowerShell7/Helpers/PowerShell7PkgOperationHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ protected override OperationVeredict _getOperationResult(
5252
{
5353
string output_string = string.Join("\n", processOutput);
5454

55-
if (!package.OverridenOptions.RunAsAdministrator != true && output_string.Contains("AdminPrivilegesAreRequired"))
55+
if (package.OverridenOptions.RunAsAdministrator is not true &&
56+
(output_string.Contains("AdminPrivilegesAreRequired") || output_string.Contains("AdminPrivilegeRequired")))
5657
{
5758
package.OverridenOptions.RunAsAdministrator = true;
5859
return OperationVeredict.AutoRetry;

0 commit comments

Comments
 (0)