|
| 1 | +using System.Diagnostics; |
| 2 | +using UniGetUI.Core.IconEngine; |
| 3 | +using UniGetUI.PackageEngine.Classes.Manager.BaseProviders; |
| 4 | +using UniGetUI.PackageEngine.Enums; |
| 5 | +using UniGetUI.PackageEngine.Interfaces; |
| 6 | +using UniGetUI.PackageEngine.ManagerClasses.Classes; |
| 7 | + |
| 8 | +namespace UniGetUI.PackageEngine.Managers.SnapManager; |
| 9 | + |
| 10 | +internal sealed class SnapPkgDetailsHelper : BasePkgDetailsHelper |
| 11 | +{ |
| 12 | + public SnapPkgDetailsHelper(Snap manager) |
| 13 | + : base(manager) { } |
| 14 | + |
| 15 | + protected override void GetDetails_UnSafe(IPackageDetails details) |
| 16 | + { |
| 17 | + using var p = new Process |
| 18 | + { |
| 19 | + StartInfo = new ProcessStartInfo |
| 20 | + { |
| 21 | + FileName = Manager.Status.ExecutablePath, |
| 22 | + Arguments = $"info {details.Package.Id}", |
| 23 | + UseShellExecute = false, |
| 24 | + RedirectStandardOutput = true, |
| 25 | + RedirectStandardError = true, |
| 26 | + CreateNoWindow = true, |
| 27 | + }, |
| 28 | + }; |
| 29 | + p.StartInfo.Environment["LANG"] = "C"; |
| 30 | + p.StartInfo.Environment["LC_ALL"] = "C"; |
| 31 | + |
| 32 | + IProcessTaskLogger logger = Manager.TaskLogger.CreateNew( |
| 33 | + LoggableTaskType.LoadPackageDetails, p); |
| 34 | + p.Start(); |
| 35 | + |
| 36 | + var descLines = new List<string>(); |
| 37 | + bool inDescription = false; |
| 38 | + bool inChannels = false; |
| 39 | + |
| 40 | + while (p.StandardOutput.ReadLine() is { } line) |
| 41 | + { |
| 42 | + logger.AddToStdOut(line); |
| 43 | + |
| 44 | + if (line.StartsWith("channels:", StringComparison.Ordinal)) |
| 45 | + { |
| 46 | + inChannels = true; |
| 47 | + inDescription = false; |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + if (inChannels && line.StartsWith(" ")) |
| 52 | + continue; |
| 53 | + |
| 54 | + if (inChannels && !line.StartsWith(" ")) |
| 55 | + inChannels = false; |
| 56 | + |
| 57 | + if (inDescription) |
| 58 | + { |
| 59 | + if (line.StartsWith(" ")) |
| 60 | + { |
| 61 | + var descLine = line.TrimStart(); |
| 62 | + if (descLine != ".") descLines.Add(descLine); |
| 63 | + continue; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + inDescription = false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + var colonIdx = line.IndexOf(':', StringComparison.Ordinal); |
| 72 | + if (colonIdx <= 0) continue; |
| 73 | + |
| 74 | + var key = line[..colonIdx].Trim().ToLowerInvariant(); |
| 75 | + var value = line[(colonIdx + 1)..].Trim(); |
| 76 | + |
| 77 | + switch (key) |
| 78 | + { |
| 79 | + case "summary": |
| 80 | + details.Description = value; |
| 81 | + break; |
| 82 | + case "publisher": |
| 83 | + details.Publisher = value.Replace("\u2713", "").Trim(); |
| 84 | + break; |
| 85 | + case "license": |
| 86 | + details.License = value; |
| 87 | + break; |
| 88 | + case "homepage": |
| 89 | + if (Uri.TryCreate(value, UriKind.Absolute, out var homepage)) |
| 90 | + details.HomepageUrl = homepage; |
| 91 | + break; |
| 92 | + case "store-url": |
| 93 | + if (Uri.TryCreate(value, UriKind.Absolute, out var storeUrl)) |
| 94 | + details.ManifestUrl = storeUrl; |
| 95 | + break; |
| 96 | + case "description": |
| 97 | + if (value == "|") |
| 98 | + { |
| 99 | + details.Description = ""; |
| 100 | + inDescription = true; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + details.Description = value; |
| 105 | + } |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if (descLines.Count > 0) |
| 111 | + details.Description = (details.Description ?? "") + "\n" + string.Join("\n", descLines); |
| 112 | + |
| 113 | + logger.AddToStdErr(p.StandardError.ReadToEnd()); |
| 114 | + p.WaitForExit(); |
| 115 | + logger.Close(p.ExitCode); |
| 116 | + } |
| 117 | + |
| 118 | + protected override CacheableIcon? GetIcon_UnSafe(IPackage package) |
| 119 | + => throw new NotImplementedException(); |
| 120 | + |
| 121 | + protected override IReadOnlyList<Uri> GetScreenshots_UnSafe(IPackage package) |
| 122 | + => Array.Empty<Uri>(); |
| 123 | + |
| 124 | + protected override string? GetInstallLocation_UnSafe(IPackage package) |
| 125 | + => $"/snap/{package.Id}/current"; |
| 126 | + |
| 127 | + protected override IReadOnlyList<string> GetInstallableVersions_UnSafe(IPackage package) |
| 128 | + => throw new InvalidOperationException("Snap does not support installing arbitrary versions"); |
| 129 | +} |
0 commit comments