-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathBun.cs
More file actions
379 lines (329 loc) · 15.8 KB
/
Copy pathBun.cs
File metadata and controls
379 lines (329 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System.Diagnostics;
using System.Text.Json.Nodes;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;
using UniGetUI.Core.Tools;
using UniGetUI.Interface.Enums;
using UniGetUI.PackageEngine.Classes.Manager;
using UniGetUI.PackageEngine.Classes.Manager.ManagerHelpers;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.Interfaces;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUI.PackageEngine.Structs;
namespace UniGetUI.PackageEngine.Managers.BunManager
{
public class Bun : PackageManager
{
public Bun()
{
Capabilities = new ManagerCapabilities
{
CanRunAsAdmin = true,
SupportsCustomVersions = true,
CanDownloadInstaller = true,
SupportsCustomScopes = false,
CanListDependencies = true,
SupportsPreRelease = true,
SupportsProxy = ProxySupport.No,
SupportsProxyAuth = false,
KnowsPackageReleaseDate = PackageReleaseDateSupport.Yes,
};
Properties = new ManagerProperties
{
Id = "bun",
Name = "Bun",
Description = CoreTools.Translate("Fast JavaScript runtime, bundler, and package manager"),
IconId = IconType.Bun,
ColorIconId = "bun_color",
ExecutableFriendlyName = "bun",
InstallVerb = "add",
UninstallVerb = "remove",
UpdateVerb = "add",
DefaultSource = new ManagerSource(this, "Bun", new Uri("https://www.npmjs.com/")),
KnownSources = [new ManagerSource(this, "Bun", new Uri("https://www.npmjs.com/"))],
};
DetailsHelper = new BunPkgDetailsHelper(this);
OperationHelper = new BunPkgOperationHelper(this);
}
protected override IReadOnlyList<Package> FindPackages_UnSafe(string query)
{
using Process p = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + " search \"" + query + "\" --json",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.FindPackages, p);
p.Start();
string strContents = p.StandardOutput.ReadToEnd();
logger.AddToStdOut(strContents);
logger.AddToStdErr(p.StandardError.ReadToEnd());
p.WaitForExit();
logger.Close(p.ExitCode);
return ParseSearchOutput(strContents, DefaultSource, this);
}
protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
{
// bun outdated checks the project in the current directory, not a --global flag.
// Until Bun supports per-project working directories in UniGetUI, expose Bun as
// a global-only manager and query the dedicated global package.json.
string globalDir = GetGlobalPackagesDirectory(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
if (!HasGlobalPackageManifest(globalDir))
{
Logger.Info($"Bun: Skipping global update detection because {globalDir} is missing package.json");
return [];
}
using Process p = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + " outdated",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = globalDir,
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);
p.Start();
// Read both streams concurrently to avoid deadlock when the process writes
// to both. Bun may write the table to stderr when stdout is not a TTY.
var stdoutTask = p.StandardOutput.ReadToEndAsync();
var stderrTask = p.StandardError.ReadToEndAsync();
Task.WaitAll(stdoutTask, stderrTask);
string strOut = stdoutTask.Result;
string strErr = stderrTask.Result;
logger.AddToStdOut(strOut);
logger.AddToStdErr(strErr);
// Read the preference first
bool preferLatest = Settings.Get(Settings.K.BunPreferLatestVersions);
// Parse stdout first; fall back to stderr if stdout has no table rows.
string tableSrc = ParseBunOutdatedTable(strOut, preferLatest).Any() ? strOut : strErr;
var result = ParseAvailableUpdates(tableSrc, DefaultSource, this, preferLatest);
Logger.Info($"Bun: Found {result.Count} packages with available updates (preferLatest={preferLatest})");
foreach (var pkg in result)
{
Logger.Info($" - {pkg.Id}: {pkg.VersionString} → {pkg.NewVersionString}");
}
p.WaitForExit();
logger.Close(p.ExitCode);
return result;
}
protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
{
List<Package> Packages = [];
using Process p = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + " pm ls --global",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListInstalledPackages, p);
p.Start();
// Read both streams concurrently to avoid deadlock when the process writes
// to both. Bun may write to stderr when stdout is not a TTY.
var stdoutTask = p.StandardOutput.ReadToEndAsync();
var stderrTask = p.StandardError.ReadToEndAsync();
Task.WaitAll(stdoutTask, stderrTask);
string strContents = stdoutTask.Result;
logger.AddToStdOut(strContents);
Packages.AddRange(ParseInstalledPackages(strContents, DefaultSource, this, new OverridenInstallationOptions(PackageScope.Global)));
logger.AddToStdErr(stderrTask.Result);
p.WaitForExit();
logger.Close(p.ExitCode);
return Packages;
}
public override IReadOnlyList<string> FindCandidateExecutableFiles()
=> CoreTools.WhichMultiple(OperatingSystem.IsWindows() ? "bun.exe" : "bun");
internal static string GetGlobalPackagesDirectory(string userProfile)
=> Path.Combine(userProfile, ".bun", "install", "global");
internal static bool HasGlobalPackageManifest(string globalDir)
=> Directory.Exists(globalDir) && File.Exists(Path.Combine(globalDir, "package.json"));
protected override void _loadManagerExecutableFile(out bool found, out string path, out string callArguments)
{
var (_found, _executablePath) = GetExecutableFile();
found = _found;
path = _executablePath;
callArguments = "";
}
protected override void _loadManagerVersion(out string version)
{
Process process = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + "--version",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true,
WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
StandardOutputEncoding = System.Text.Encoding.UTF8
}
};
process.Start();
version = process.StandardOutput.ReadToEnd().Trim();
process.WaitForExit();
}
/// <summary>
/// Parses JSON search results from 'bun search <query> --json'.
/// Each result object contains 'name' and 'version' fields.
/// </summary>
internal static IReadOnlyList<Package> ParseSearchOutput(
string output,
IManagerSource source,
IPackageManager manager)
{
List<Package> packages = [];
if (!output.Any()) return packages;
try
{
JsonArray? results = JsonNode.Parse(output) as JsonArray;
foreach (JsonNode? entry in results ?? [])
{
string? id = entry?["name"]?.ToString();
string? version = entry?["version"]?.ToString();
if (id is not null && version is not null)
{
packages.Add(new Package(CoreTools.FormatAsName(id), id, version, source, manager));
}
}
}
catch (Exception e)
{
Logger.Warn($"Failed to parse Bun search results: {e.Message}");
}
return packages;
}
/// <summary>
/// Parses the outdated packages table from 'bun outdated'.
/// Creates packages with current and available versions and sets scope to Global.
/// </summary>
internal static IReadOnlyList<Package> ParseAvailableUpdates(
string output,
IManagerSource source,
IPackageManager manager,
bool preferLatest = false)
{
List<Package> packages = [];
foreach (var (packageId, version, newVersion) in ParseBunOutdatedTable(output, preferLatest))
{
packages.Add(new Package(CoreTools.FormatAsName(packageId), packageId, version, newVersion,
source, manager, new(PackageScope.Global)));
}
return packages;
}
/// <summary>
/// Parses the installed packages tree from 'bun pm ls --global'.
/// Each package entry is formatted as: [├/└]── [@scope/]name@version
/// </summary>
internal static IReadOnlyList<Package> ParseInstalledPackages(
string output,
IManagerSource source,
IPackageManager manager,
OverridenInstallationOptions options)
{
List<Package> packages = [];
// bun pm ls --global outputs a tree:
// /home/user/.bun/install/global node_modules (3)
// ├── @devcontainers/cli@0.81.1
// └── typescript@5.7.3
foreach (string line in output.Split('\n'))
{
if (!line.Contains("──")) continue;
string entry = line[(line.IndexOf("──") + 2)..].Trim();
// Use LastIndexOf to handle scoped packages: @scope/name@version
int atIdx = entry.LastIndexOf('@');
if (atIdx <= 0) continue;
string packageName = entry[..atIdx];
string version = entry[(atIdx + 1)..];
if (string.IsNullOrWhiteSpace(packageName) || string.IsNullOrWhiteSpace(version)) continue;
packages.Add(new Package(CoreTools.FormatAsName(packageName), packageName, version,
source, manager, options));
}
return packages;
}
/// <summary>
/// Parses the outdated packages table from 'bun outdated'.
/// Supports both ASCII pipe format (|) and Unicode box-drawing format (│).
/// Each yielded tuple contains (packageId, currentVersion, recommendedUpdateVersion).
/// Columns: [Package | Current | Update | Latest]
/// If preferLatest is true, uses "Latest" (parts[4], may have breaking changes).
/// If preferLatest is false (default), uses "Update" (parts[3], safe semantic version).
/// </summary>
// TODO: Replace table parsing with JSON deserialization when bun outdated adds --json flag.
// Track: https://github.com/oven-sh/bun/issues — once --json is available, this entire
// method should be swapped for a simple JsonNode.Parse() call.
internal static IEnumerable<(string Id, string Version, string NewVersion)> ParseBunOutdatedTable(
string output,
bool preferLatest = false)
{
int columnIndex = preferLatest ? 4 : 3; // 4 = Latest, 3 = Update
string columnName = preferLatest ? "Latest" : "Update";
foreach (string line in output.Split('\n'))
{
string trimmed = line.TrimStart();
// Skip lines that don't contain package data (headers, separators, etc.)
if (!trimmed.StartsWith('│') && !trimmed.StartsWith('|'))
{
continue;
}
// Split by either Unicode box-drawing or ASCII pipe characters
string[] parts = line.Split(new[] { '│', '|' }, StringSplitOptions.None);
if (parts.Length < columnIndex + 1)
{
continue;
}
string id = parts[1].Trim();
string version = parts[2].Trim();
string recommendedUpdate = parts[columnIndex].Trim();
// Skip header row, empty rows, and border lines (which contain only dashes or box-drawing chars)
if (id is "Package" || string.IsNullOrWhiteSpace(id)
|| string.IsNullOrWhiteSpace(version) || string.IsNullOrWhiteSpace(recommendedUpdate)
|| id.All(c => c == '-' || c == '─' || c == '┬' || c == '┼' || c == '┴' || c == '├' || c == '┤' || c == '┌' || c == '└' || c == '┘' || c == '┐')
|| version.All(c => c == '-' || c == '─' || c == '┬' || c == '┼' || c == '┴' || c == '├' || c == '┤' || c == '┌' || c == '└' || c == '┘' || c == '┐'))
{
continue;
}
// Only include packages that have a different update version
if (version != recommendedUpdate)
{
Logger.Debug($"Bun: Found update for {id}: {version} → {recommendedUpdate} (using {columnName} column)");
yield return (id, version, recommendedUpdate);
}
else
{
Logger.Debug($"Bun: Skipping {id} (no update available: {version} == {recommendedUpdate})");
}
}
}
}
}