Skip to content

Commit

Permalink
Improve how Scoop and Pip parse Package Details, make PackageDetails …
Browse files Browse the repository at this point in the history
…fields nullable
  • Loading branch information
marticliment committed May 30, 2024
1 parent 841efd4 commit fdef9e4
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 89 deletions.
23 changes: 23 additions & 0 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using UniGetUI.Core.Language;
using UniGetUI.Core.Logging;
using Jeffijoe.MessageFormat;
using System.ComponentModel.DataAnnotations;

namespace UniGetUI.Core.Tools
{
Expand Down Expand Up @@ -322,6 +323,28 @@ public static string EnsureSafeQueryString(string query)
.Replace("\\", string.Empty)
.Replace("`", string.Empty);
}

/// <summary>
/// Returns null if the string is empty
/// </summary>
/// <param name="value">The string to check</param>
/// <returns>a string? instance</returns>
public static string? GetStringOrNull(string? value)
{
if (value == "") return null;
return value;
}

/// <summary>
/// Returns a new Uri if the string is not empty. Returns null otherwhise
/// </summary>
/// <param name="url">The null, empty or valid string</param>
/// <returns>an Uri? instance</returns>
public static Uri? GetUriOrNull(string? url)
{
if (url == "" || url == null) return null;
return new Uri(url);
}
}
}

107 changes: 55 additions & 52 deletions src/UniGetUI.PackageEngine.Managers.Pip/PipPackageDetailsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,59 +35,62 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
return details;
}

try
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("author") ?? false)
details.Author = (RawInfo["info"] as JsonObject)?["author"]?.ToString() ?? "";
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load author: " + ex); }

try
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("home_page") ?? false)
details.HomepageUrl = new Uri((RawInfo["info"] as JsonObject)?["home_page"]?.ToString() ?? "");
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load home_page: " + ex); }
try
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("package_url") ?? false)
details.ManifestUrl = new Uri((RawInfo["info"] as JsonObject)?["package_url"]?.ToString() ?? "");
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load package_url: " + ex); }
try
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("summary") ?? false)
details.Description = (RawInfo["info"] as JsonObject)?["summary"]?.ToString() ?? "";
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load summary: " + ex); }
try
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("license") ?? false)
details.License = (RawInfo["info"] as JsonObject)?["license"]?.ToString() ?? "";
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load license: " + ex); }
try
JsonObject? infoNode = RawInfo["info"] as JsonObject;
if (infoNode != null)
{
if ((RawInfo["info"] as JsonObject)?.ContainsKey("maintainer") ?? false)
details.Publisher = (RawInfo["info"] as JsonObject)?["maintainer"]?.ToString() ?? "";
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load maintainer: " + ex); }
try
{
if (((RawInfo["info"] as JsonObject)?.ContainsKey("classifiers") ?? false)
&& ((RawInfo["info"] as JsonObject)?["classifiers"] is JsonArray))
try
{
if (infoNode.ContainsKey("author"))
details.Author = CoreTools.GetStringOrNull(infoNode["author"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load author: " + ex); }
try
{
if (infoNode.ContainsKey("home_page"))
details.HomepageUrl = CoreTools.GetUriOrNull(infoNode["home_page"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load home_page: " + ex); }
try
{
if (infoNode.ContainsKey("package_url"))
details.ManifestUrl = CoreTools.GetUriOrNull(infoNode["package_url"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load package_url: " + ex); }
try
{
List<string> Tags = new();
foreach (string? line in (RawInfo["info"] as JsonObject)?["classifiers"] as JsonArray ?? new())
if (line?.Contains("License ::") ?? false)
details.License = line.Split("::")[^1].Trim();
else if (line?.Contains("Topic ::") ?? false)
if (!Tags.Contains(line.Split("::")[^1].Trim()))
Tags.Add(line.Split("::")[^1].Trim());
details.Tags = Tags.ToArray();
if (infoNode.ContainsKey("summary"))
details.Description = CoreTools.GetStringOrNull(infoNode["summary"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load summary: " + ex); }
try
{
if (infoNode.ContainsKey("license"))
details.License = CoreTools.GetStringOrNull(infoNode["license"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load license: " + ex); }
try
{
if (infoNode.ContainsKey("maintainer"))
details.Publisher = CoreTools.GetStringOrNull(infoNode["maintainer"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load maintainer: " + ex); }
try
{
if ((infoNode.ContainsKey("classifiers"))
&& (infoNode["classifiers"] is JsonArray))
{
List<string> Tags = new();
foreach (string? line in infoNode["classifiers"] as JsonArray ?? new())
if (line?.Contains("License ::") ?? false)
details.License = line.Split("::")[^1].Trim();
else if (line?.Contains("Topic ::") ?? false)
if (!Tags.Contains(line.Split("::")[^1].Trim()))
Tags.Add(line.Split("::")[^1].Trim());
details.Tags = Tags.ToArray();
}
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load classifiers: " + ex); }
}
catch (Exception ex) { Logger.Debug("[Pip] Can't load classifiers: " + ex); }

try
{
Expand All @@ -102,12 +105,12 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
{
if (url.ContainsKey("digests") && ((url["digests"] as JsonObject)?.ContainsKey("sha256") ?? false))
{
details.InstallerHash = url["digests"]?["sha256"]?.ToString() ?? "";
details.InstallerHash = url["digests"]?["sha256"]?.ToString();
}
if (url.ContainsKey("url"))
{
details.InstallerType = url["url"]?.ToString().Split('.')[^1].Replace("whl", "Wheel") ?? "";
details.InstallerUrl = url["url"] != null? new Uri(url["url"]?.ToString() ?? ""): null;
details.InstallerType = url["url"]?.ToString().Split('.')[^1].Replace("whl", "Wheel");
details.InstallerUrl = CoreTools.GetUriOrNull(url["url"]?.ToString());
details.InstallerSize = await CoreTools.GetFileSizeAsync(details.InstallerUrl);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
details.Description = details.Description.Replace("\n\n", "\n").Trim();
}
else if (RawInfo.ContainsKey("description"))
details.Description = RawInfo["description"]?.ToString() ?? "";
details.Description = CoreTools.GetStringOrNull(RawInfo["description"]?.ToString());
}
catch (Exception ex) { Logger.Debug("[Scoop] Can't load description: " + ex); }

Expand All @@ -83,11 +83,11 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
{
if (RawInfo.ContainsKey("homepage"))
{
details.HomepageUrl = RawInfo["homepage"] != null? new Uri(RawInfo["homepage"]?.ToString() ?? ""): null;
details.HomepageUrl = CoreTools.GetUriOrNull(RawInfo["homepage"]?.ToString());
if (details.HomepageUrl?.ToString().Contains("https://github.com/") ?? false)
details.Author = details.HomepageUrl.ToString().Replace("https://github.com/", "").Split("/")[0];
else
details.Author = details.HomepageUrl?.Host.Split(".")[^2] ?? "";
details.Author = details.HomepageUrl?.Host.Split(".")[^2];
}
}
catch (Exception ex) { Logger.Debug("[Scoop] Can't load homepage: " + ex); }
Expand All @@ -102,7 +102,7 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
details.ReleaseNotes = details.ReleaseNotes.Replace("\n\n", "\n").Trim();
}
else if (RawInfo.ContainsKey("notes"))
details.ReleaseNotes = RawInfo["notes"]?.ToString() ?? "";
details.ReleaseNotes = RawInfo["notes"]?.ToString();
}
catch (Exception ex) { Logger.Debug("[Scoop] Can't load notes: " + ex); }

Expand All @@ -112,11 +112,11 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
{
if (RawInfo["license"] is not JsonValue)
{
details.License = RawInfo["license"]?["identifier"]?.ToString() ?? "";
details.LicenseUrl = RawInfo["license"]?["url"] != null? new Uri(RawInfo["license"]?["url"]?.ToString() ?? ""): null;
details.License = RawInfo["license"]?["identifier"]?.ToString();
details.LicenseUrl = CoreTools.GetUriOrNull(RawInfo["license"]?["url"]?.ToString());
}
else
details.License = RawInfo["license"]?.ToString() ?? "";
details.License = RawInfo["license"]?.ToString();
}
}
catch (Exception ex) { Logger.Debug("[Scoop] Can't load license: " + ex); }
Expand All @@ -126,21 +126,20 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
if (RawInfo.ContainsKey("url") && RawInfo.ContainsKey("hash"))
{
if (RawInfo["url"] is JsonArray)
details.InstallerUrl = RawInfo["url"] != null? new Uri(RawInfo["url"]?[0]?.ToString() ?? ""): null;
details.InstallerUrl = CoreTools.GetUriOrNull(RawInfo["url"]?[0]?.ToString());
else
details.InstallerUrl = RawInfo["url"] != null? new Uri(RawInfo["url"]?.ToString() ?? ""): null;
details.InstallerUrl = CoreTools.GetUriOrNull(RawInfo["url"]?.ToString());

if (RawInfo["hash"] is JsonArray)
details.InstallerHash = RawInfo["hash"]?[0]?.ToString() ?? "";
details.InstallerHash = RawInfo["hash"]?[0]?.ToString();
else
details.InstallerHash = RawInfo["hash"]?.ToString() ?? "";
details.InstallerHash = RawInfo["hash"]?.ToString();
}
else if (RawInfo.ContainsKey("architecture"))
{
string FirstArch = (RawInfo["architecture"] as JsonObject)?.ElementAt(0).Key ?? "";
details.InstallerHash = RawInfo["architecture"]?[FirstArch]?["hash"]?.ToString() ?? "";
details.InstallerUrl = RawInfo["architecture"]?[FirstArch]?["url"] != null?
new Uri(RawInfo["architecture"]?[FirstArch]?["url"]?.ToString() ?? ""): null;
details.InstallerHash = RawInfo["architecture"]?[FirstArch]?["hash"]?.ToString();
details.InstallerUrl = CoreTools.GetUriOrNull(RawInfo["architecture"]?[FirstArch]?["url"]?.ToString());
}

details.InstallerSize = await CoreTools.GetFileSizeAsync(details.InstallerUrl);
Expand All @@ -150,7 +149,7 @@ protected override async Task<PackageDetails> GetPackageDetails_Unsafe(Package p
try
{
if (RawInfo.ContainsKey("checkver") && RawInfo["checkver"] is JsonObject && ((RawInfo["checkver"] as JsonObject)?.ContainsKey("url") ?? false))
details.ReleaseNotesUrl = RawInfo["checkver"]?["url"] != null? new Uri(RawInfo["checkver"]?["url"]?.ToString() ?? ""): null;
details.ReleaseNotesUrl = CoreTools.GetUriOrNull(RawInfo["checkver"]?["url"]?.ToString() ?? "");
}
catch (Exception ex) { Logger.Debug("[Scoop] Can't load notes URL: " + ex); }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,25 @@ namespace UniGetUI.PackageEngine.PackageClasses
public class PackageDetails
{
public Package Package { get; }
public string Name { get; }
public string Id { get; }
public string Version { get; }
public string NewVersion { get; }
public ManagerSource Source { get; }
public string Description { get; set; } = "";
public string Publisher { get; set; } = "";
public string Author { get; set; } = "";
public string? Description { get; set; } = null;
public string? Publisher { get; set; } = null;
public string? Author { get; set; } = null;
public Uri? HomepageUrl { get; set; } = null;
public string License { get; set; } = "";
public string? License { get; set; } = null;
public Uri? LicenseUrl { get; set; } = null;
public Uri? InstallerUrl { get; set; } = null;
public string InstallerHash { get; set; } = "";
public string InstallerType { get; set; } = "";
public string? InstallerHash { get; set; } = null;
public string? InstallerType { get; set; } = null;
public double InstallerSize { get; set; } = 0; // In Megabytes
public Uri? ManifestUrl { get; set; } = null;
public string UpdateDate { get; set; } = "";
public string ReleaseNotes { get; set; } = "";
public string? UpdateDate { get; set; } = null;
public string? ReleaseNotes { get; set; } = null;
public Uri? ReleaseNotesUrl { get; set; } = null;
public string[] Tags { get; set; } = new string[0];

public PackageDetails(Package package)
{
Package = package;
Name = package.Name;
Id = package.Id;
Version = package.Version;
Source = package.Source;
if (package is UpgradablePackage)
NewVersion = ((UpgradablePackage)package).NewVersion;
else
NewVersion = "";
}
}

Expand Down
1 change: 0 additions & 1 deletion src/UniGetUI/Interface/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,6 @@ public void UniGetUILogs_Click(object sender, RoutedEventArgs e)
NavigateToPage(new Logger_LogPage(Logger_LogType.UniGetUILog));
}


private void HelpMenu_Click(object sender, RoutedEventArgs e)
{
ShowHelp();
Expand Down

0 comments on commit fdef9e4

Please sign in to comment.