| Name | Message | Date |
|---|---|---|
| 📄 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
using System; using System.Diagnostics; using System.IO; using System.IO.Pipes; using System.Management.Automation; using System.Management.Automation.Internal; using System.Text; 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; } #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: {string.Join(",", Headers)}"); } } protected override void ProcessRecord() { if (!pipe.IsConnected) { TerminateDueToWindowClosed(); return; } try { writer.WriteLine( $"RECORD: {Convert.ToBase64String(Encoding.UTF8.GetBytes(PSSerializer.Serialize(InputObject)))}" ); } catch (IOException) { TerminateDueToWindowClosed(); return; } } 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(); } }