| Name | Message | Date |
|---|---|---|
| 📄 ColumnFormatter.cs | 1 month ago | |
| 📄 OutGridTree.cs | 1 month ago | |
| 📄 OutGridTree.csproj | 1 month ago | |
| 📄 packages.lock.json | 1 month ago |
📄
OutGridTree/OutGridTree.cs
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
using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.Pipes; using System.Linq; using System.Management.Automation; using System.Management.Automation.Internal; using System.Text.Json; namespace OutGridTree; [Cmdlet(VerbsData.Out, "GridTree")] [Alias("ogt")] public sealed class OutGridTree : PSCmdlet { [Parameter(ValueFromPipeline = true)] public PSObject InputObject { get; set; } = AutomationNull.Value; [Parameter(Mandatory = true)] public string WindowExe { get; set; } = ""; [Parameter] public string? Title { get; set; } [Parameter] public string[]? Headers { get; set; } private bool hasSentHeaders = false; private ColumnFormatter[]? columnFormats; #nullable disable private NamedPipeServerStream pipe; private Process windowProcess; private StreamWriter writer; #nullable restore protected override void BeginProcessing() { var pipeName = Path.GetTempFileName(); pipe = new(pipeName, PipeDirection.Out); windowProcess = Process.Start( new ProcessStartInfo() { FileName = WindowExe, Arguments = pipeName, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, } ); pipe.WaitForConnection(); writer = new(pipe); if (MyInvocation.BoundParameters.ContainsKey(nameof(Title)) && Title is not null) { writer.WriteLine($"TITLE: {Title}"); } else { writer.WriteLine($"TITLE: {MyInvocation.Line}"); } if (MyInvocation.BoundParameters.ContainsKey(nameof(Headers)) && Headers is not null) { writer.WriteLine($"HEADERS: {JsonSerializer.Serialize(Headers)}"); hasSentHeaders = true; } } protected override void ProcessRecord() { if (!pipe.IsConnected) { TerminateDueToWindowClosed(); return; } try { if (!hasSentHeaders) { SendFormatHeaders(); } var headerValues = GetHeaderValues(); writer.WriteLine( $"RECORD: {JsonSerializer.Serialize(headerValues.Append(PSSerializer.Serialize(InputObject)))}" ); } catch (IOException) { TerminateDueToWindowClosed(); return; } } private void SendFormatHeaders() { string[] headers; var formatResult = InvokeCommand.InvokeScript("Get-FormatData $args[0]", InputObject.BaseObject.GetType()); if ( formatResult.Count is 1 && formatResult[0].BaseObject is ExtendedTypeDefinition { FormatViewDefinition: var views } && views.FirstOrDefault(v => v.Control is TableControl) is { Control: TableControl tableControl } && tableControl.Rows.FirstOrDefault(r => r.Columns.Count == tableControl.Headers.Count) is { Columns: var columns } ) { headers = [.. tableControl.Headers.Zip(columns, (h, c) => h.Label ?? c.DisplayEntry.Value)]; columnFormats = [.. columns.Select(c => ColumnFormatter.FromDisplayEntry(c.DisplayEntry))]; } else if (InputObject is PSObject psObject && !psObject.BaseObject.GetType().IsPrimitive) { Headers = headers = [.. psObject.Properties.Select(p => p.Name)]; } else { headers = [ InputObject.BaseObject switch { bool => "Boolean", byte or sbyte or short or ushort or int or uint or long or ulong => "Integer", float or double or decimal => "Number", char => "Character", string => "String", _ => "Primitive", }, ]; } writer.WriteLine($"HEADERS: {JsonSerializer.Serialize(headers)}"); hasSentHeaders = true; } private IEnumerable<string?> GetHeaderValues() { if (columnFormats is not null) { return columnFormats.Select(f => f switch { ColumnFormatter.Property prop => InputObject .Properties.Match(prop.Name) .FirstOrDefault() ?.Value.ToString(), ColumnFormatter.Script script => script .ScriptBlock.InvokeWithContext([], [new("_", InputObject)]) .FirstOrDefault() is { } value ? value.ToString() : null, _ => null, } ); } else if (InputObject is PSObject psObject && !psObject.BaseObject.GetType().IsPrimitive) { return Headers.Select(h => InputObject.Properties.Match(h).FirstOrDefault()?.Value?.ToString()); } else { return [InputObject?.ToString()]; } } private void TerminateDueToWindowClosed() => ThrowTerminatingError( new(new InvalidOperationException("Window closed"), "WindowClosed", ErrorCategory.InvalidOperation, null) ); protected override void EndProcessing() { // TODO: Wait for window to close if (pipe.IsConnected) { writer.Flush(); } } protected override void StopProcessing() { if (pipe.IsConnected) { pipe?.Close(); pipe?.Disconnect(); } pipe?.Dispose(); if (!windowProcess.HasExited) { windowProcess?.Close(); } windowProcess?.Dispose(); } }