-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (97 loc) · 3.8 KB
/
Program.cs
File metadata and controls
112 lines (97 loc) · 3.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
using WingetTuiSharp;
if (args.Length > 0 && args [0] is "--dump")
{
// Diagnostic mode: invoke winget the way the backend would and print the raw output
// verbatim, plus a hex dump of the bytes immediately around the dash-line separator.
// Use this on Windows to verify the encoding and figure out why ParseTable is empty:
//
// winget-tui-sharp.exe --dump search vscode
// winget-tui-sharp.exe --dump list
// winget-tui-sharp.exe --dump upgrade
string [] cmd = args.Length > 1
? [.. args.Skip (1), "--accept-source-agreements"]
: ["list", "--accept-source-agreements"];
Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.WriteLine ($"Invoking: winget {string.Join (' ', cmd)}");
Console.WriteLine ($"Console.OutputEncoding: {Console.OutputEncoding.WebName}");
Console.WriteLine ();
(int code, string output) = await CliBackend.RunWithCodeAsync (cmd, CancellationToken.None);
Console.WriteLine ($"--- exit code: {code}");
Console.WriteLine ($"--- output length: {output.Length} chars");
Console.WriteLine ("--- output:");
Console.WriteLine (output);
Console.WriteLine ("--- parser trace:");
if (cmd [0] == "show")
{
// Extract the id from --id <X> for the parser's name fallback.
int idIdx = Array.IndexOf (cmd, "--id");
string id = idIdx >= 0 && idIdx + 1 < cmd.Length ? cmd [idIdx + 1] : string.Empty;
PackageDetail? detail = CliBackend.ParseShowTraced (id, output, Console.Out);
Console.WriteLine ();
if (detail is null)
{
Console.WriteLine ("--- parsed detail: null");
}
else
{
Console.WriteLine ("--- parsed detail:");
Console.WriteLine ($" Name={detail.Name}");
Console.WriteLine ($" Id={detail.Id}");
Console.WriteLine ($" Version={detail.Version}");
Console.WriteLine ($" Publisher={detail.Publisher}");
Console.WriteLine ($" Homepage={detail.Homepage}");
Console.WriteLine ($" License={detail.License}");
Console.WriteLine ($" ReleaseNotesUrl={detail.ReleaseNotesUrl}");
Console.WriteLine ($" Description={detail.Description}");
}
}
else
{
IReadOnlyList<Package> rows = CliBackend.ParseTableTraced (output, hasAvailable: cmd [0] == "upgrade", Console.Out);
Console.WriteLine ();
Console.WriteLine ($"--- parsed rows: {rows.Count}");
foreach (Package row in rows.Take (5))
{
Console.WriteLine ($" Name='{row.Name}' Id='{row.Id}' Version='{row.Version}' Available='{row.AvailableVersion}' Source='{row.Source}'");
}
}
return;
}
bool useMock = args.Any (a => a is "--mock" or "-m") || !IsWingetAvailable ();
if (useMock && !args.Any (a => a is "--mock" or "-m"))
{
Console.Error.WriteLine ("winget not found on PATH — falling back to mock backend. Run with `winget` available to drive the real CLI.");
}
IBackend backend = useMock ? new MockBackend () : new CliBackend ();
Theme.Register ();
IApplication app = Application.Create ().Init ();
App window = new (backend);
app.Run (window);
window.Dispose ();
app.Dispose ();
return;
static bool IsWingetAvailable ()
{
try
{
using System.Diagnostics.Process p = new ()
{
StartInfo = new ()
{
FileName = "winget",
Arguments = "--version",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start ();
p.WaitForExit (1500);
return p.HasExited && p.ExitCode == 0;
}
catch
{
return false;
}
}