|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json.Nodes; |
| 3 | +using UniGetUI.Core.Data; |
| 4 | +using UniGetUI.Core.Tools; |
| 5 | +using UniGetUI.Interface.Enums; |
| 6 | +using UniGetUI.PackageEngine.Classes.Manager; |
| 7 | +using UniGetUI.PackageEngine.Classes.Manager.ManagerHelpers; |
| 8 | +using UniGetUI.PackageEngine.Enums; |
| 9 | +using UniGetUI.PackageEngine.ManagerClasses.Classes; |
| 10 | +using UniGetUI.PackageEngine.ManagerClasses.Manager; |
| 11 | +using UniGetUI.PackageEngine.PackageClasses; |
| 12 | +using UniGetUI.PackageEngine.Structs; |
| 13 | + |
| 14 | +namespace UniGetUI.PackageEngine.Managers.BunManager |
| 15 | +{ |
| 16 | + public class Bun : PackageManager |
| 17 | + { |
| 18 | + public Bun() |
| 19 | + { |
| 20 | + Capabilities = new ManagerCapabilities |
| 21 | + { |
| 22 | + CanRunAsAdmin = true, |
| 23 | + SupportsCustomVersions = true, |
| 24 | + CanDownloadInstaller = true, |
| 25 | + SupportsCustomScopes = true, |
| 26 | + CanListDependencies = true, |
| 27 | + SupportsPreRelease = true, |
| 28 | + SupportsProxy = ProxySupport.No, |
| 29 | + SupportsProxyAuth = false |
| 30 | + }; |
| 31 | + |
| 32 | + Properties = new ManagerProperties |
| 33 | + { |
| 34 | + Name = "Bun", |
| 35 | + Description = CoreTools.Translate("A npmjs package manager written in Zig. Full of libraries and other utilities that orbit the javascript world<br>Contains: <b>Node javascript libraries and other related utilities</b>"), |
| 36 | + IconId = IconType.Node, |
| 37 | + ColorIconId = "node_color", |
| 38 | + ExecutableFriendlyName = "bun", |
| 39 | + InstallVerb = "install", |
| 40 | + UninstallVerb = "uninstall", |
| 41 | + UpdateVerb = "install", |
| 42 | + DefaultSource = new ManagerSource(this, "Bun", new Uri("https://www.npmjs.com/")), |
| 43 | + KnownSources = [new ManagerSource(this, "Bun", new Uri("https://www.npmjs.com/"))], |
| 44 | + |
| 45 | + }; |
| 46 | + |
| 47 | + DetailsHelper = new BunPkgDetailsHelper(this); |
| 48 | + OperationHelper = new BunPkgOperationHelper(this); |
| 49 | + } |
| 50 | + |
| 51 | + protected override IReadOnlyList<Package> FindPackages_UnSafe(string query) |
| 52 | + { |
| 53 | + using Process p = new() |
| 54 | + { |
| 55 | + StartInfo = new ProcessStartInfo |
| 56 | + { |
| 57 | + FileName = Status.ExecutablePath, |
| 58 | + Arguments = Status.ExecutableCallArgs + " search \"" + query + "\" --json", |
| 59 | + RedirectStandardOutput = true, |
| 60 | + RedirectStandardError = true, |
| 61 | + RedirectStandardInput = true, |
| 62 | + UseShellExecute = false, |
| 63 | + CreateNoWindow = true, |
| 64 | + WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), |
| 65 | + StandardOutputEncoding = System.Text.Encoding.UTF8 |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.FindPackages, p); |
| 70 | + p.Start(); |
| 71 | + |
| 72 | + string? line; |
| 73 | + List<Package> Packages = []; |
| 74 | + while ((line = p.StandardOutput.ReadLine()) is not null) |
| 75 | + { |
| 76 | + logger.AddToStdOut(line); |
| 77 | + if (line.StartsWith("{")) |
| 78 | + { |
| 79 | + JsonNode? node = JsonNode.Parse(line); |
| 80 | + string? id = node?["name"]?.ToString(); |
| 81 | + string? version = node?["version"]?.ToString(); |
| 82 | + if (id is not null && version is not null) |
| 83 | + { |
| 84 | + Packages.Add(new Package(CoreTools.FormatAsName(id), id, version, DefaultSource, this)); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + logger.AddToStdErr("Line could not be parsed: " + line); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + logger.AddToStdErr(p.StandardError.ReadToEnd()); |
| 94 | + p.WaitForExit(); |
| 95 | + logger.Close(p.ExitCode); |
| 96 | + |
| 97 | + return Packages; |
| 98 | + } |
| 99 | + |
| 100 | + protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe() |
| 101 | + { |
| 102 | + List<Package> Packages = []; |
| 103 | + foreach (var options in new OverridenInstallationOptions[] { new(PackageScope.Local), new(PackageScope.Global) }) |
| 104 | + { |
| 105 | + using Process p = new() |
| 106 | + { |
| 107 | + StartInfo = new ProcessStartInfo |
| 108 | + { |
| 109 | + FileName = Status.ExecutablePath, |
| 110 | + Arguments = Status.ExecutableCallArgs + " outdated --json" + (options.Scope == PackageScope.Global ? " --global" : ""), |
| 111 | + RedirectStandardOutput = true, |
| 112 | + RedirectStandardError = true, |
| 113 | + RedirectStandardInput = true, |
| 114 | + UseShellExecute = false, |
| 115 | + CreateNoWindow = true, |
| 116 | + WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), |
| 117 | + StandardOutputEncoding = System.Text.Encoding.UTF8 |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p); |
| 122 | + p.Start(); |
| 123 | + |
| 124 | + string strContents = p.StandardOutput.ReadToEnd(); |
| 125 | + logger.AddToStdOut(strContents); |
| 126 | + JsonObject? contents = null; |
| 127 | + if (strContents.Any()) contents = JsonNode.Parse(strContents) as JsonObject; |
| 128 | + foreach (var (packageId, packageData) in contents?.ToDictionary() ?? []) |
| 129 | + { |
| 130 | + string? version = packageData?["current"]?.ToString(); |
| 131 | + string? newVersion = packageData?["latest"]?.ToString(); |
| 132 | + if (version is not null && newVersion is not null) |
| 133 | + { |
| 134 | + Packages.Add(new Package(CoreTools.FormatAsName(packageId), packageId, version, newVersion, |
| 135 | + DefaultSource, this, options)); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + logger.AddToStdErr(p.StandardError.ReadToEnd()); |
| 140 | + p.WaitForExit(); |
| 141 | + logger.Close(p.ExitCode); |
| 142 | + } |
| 143 | + return Packages; |
| 144 | + } |
| 145 | + |
| 146 | + protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe() |
| 147 | + { |
| 148 | + List<Package> Packages = []; |
| 149 | + foreach (var options in new OverridenInstallationOptions[] { new(PackageScope.Local), new(PackageScope.Global) }) |
| 150 | + { |
| 151 | + using Process p = new() |
| 152 | + { |
| 153 | + StartInfo = new ProcessStartInfo |
| 154 | + { |
| 155 | + FileName = Status.ExecutablePath, |
| 156 | + Arguments = Status.ExecutableCallArgs + " list --json" + (options.Scope == PackageScope.Global ? " --global" : ""), |
| 157 | + RedirectStandardOutput = true, |
| 158 | + RedirectStandardError = true, |
| 159 | + RedirectStandardInput = true, |
| 160 | + UseShellExecute = false, |
| 161 | + CreateNoWindow = true, |
| 162 | + WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), |
| 163 | + StandardOutputEncoding = System.Text.Encoding.UTF8 |
| 164 | + } |
| 165 | + }; |
| 166 | + |
| 167 | + IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListInstalledPackages, p); |
| 168 | + p.Start(); |
| 169 | + |
| 170 | + string strContents = p.StandardOutput.ReadToEnd(); |
| 171 | + logger.AddToStdOut(strContents); |
| 172 | + JsonObject? contents = null; |
| 173 | + if (strContents.Any()) contents = (JsonNode.Parse(strContents) as JsonObject)?["dependencies"] as JsonObject; |
| 174 | + foreach (var (packageId, packageData) in contents?.ToDictionary() ?? []) |
| 175 | + { |
| 176 | + string? version = packageData?["version"]?.ToString(); |
| 177 | + if (version is not null) |
| 178 | + { |
| 179 | + Packages.Add(new Package(CoreTools.FormatAsName(packageId), packageId, version, DefaultSource, this, options)); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + logger.AddToStdErr(p.StandardError.ReadToEnd()); |
| 184 | + p.WaitForExit(); |
| 185 | + logger.Close(p.ExitCode); |
| 186 | + } |
| 187 | + |
| 188 | + return Packages; |
| 189 | + } |
| 190 | + |
| 191 | + public override IReadOnlyList<string> FindCandidateExecutableFiles() |
| 192 | + => CoreTools.WhichMultiple("bun.cmd"); |
| 193 | + |
| 194 | + protected override void _loadManagerExecutableFile(out bool found, out string path, out string callArguments) |
| 195 | + { |
| 196 | + var (_found, _executable) = GetExecutableFile(); |
| 197 | + |
| 198 | + found = _found; |
| 199 | + path = CoreData.PowerShell5; |
| 200 | + callArguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{_executable.Replace(" ", "` ")}\" "; |
| 201 | + } |
| 202 | + |
| 203 | + protected override void _loadManagerVersion(out string version) |
| 204 | + { |
| 205 | + Process process = new() |
| 206 | + { |
| 207 | + StartInfo = new ProcessStartInfo |
| 208 | + { |
| 209 | + FileName = Status.ExecutablePath, |
| 210 | + Arguments = Status.ExecutableCallArgs + "--version", |
| 211 | + UseShellExecute = false, |
| 212 | + RedirectStandardOutput = true, |
| 213 | + RedirectStandardError = true, |
| 214 | + RedirectStandardInput = true, |
| 215 | + CreateNoWindow = true, |
| 216 | + WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), |
| 217 | + StandardOutputEncoding = System.Text.Encoding.UTF8 |
| 218 | + } |
| 219 | + }; |
| 220 | + process.Start(); |
| 221 | + version = process.StandardOutput.ReadToEnd().Trim(); |
| 222 | + process.WaitForExit(); |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments