|
5 | 5 | using System.Threading; |
6 | 6 | using System.Threading.Tasks; |
7 | 7 |
|
8 | | -namespace SimpleExec |
| 8 | +namespace SimpleExec; |
| 9 | + |
| 10 | +internal static class ProcessExtensions |
9 | 11 | { |
10 | | - internal static class ProcessExtensions |
| 12 | + public static void Run(this Process process, bool noEcho, string echoPrefix, bool cancellationIgnoresProcessTree, CancellationToken cancellationToken) |
11 | 13 | { |
12 | | - public static void Run(this Process process, bool noEcho, string echoPrefix, bool cancellationIgnoresProcessTree, CancellationToken cancellationToken) |
| 14 | + var cancelled = 0L; |
| 15 | + |
| 16 | + if (!noEcho) |
13 | 17 | { |
14 | | - var cancelled = 0L; |
| 18 | + Console.Out.Write(process.StartInfo.GetEchoLines(echoPrefix)); |
| 19 | + } |
15 | 20 |
|
16 | | - if (!noEcho) |
| 21 | + _ = process.Start(); |
| 22 | + |
| 23 | + using var register = cancellationToken.Register( |
| 24 | + () => |
17 | 25 | { |
18 | | - Console.Out.Write(process.StartInfo.GetEchoLines(echoPrefix)); |
19 | | - } |
| 26 | + if (process.TryKill(cancellationIgnoresProcessTree)) |
| 27 | + { |
| 28 | + _ = Interlocked.Increment(ref cancelled); |
| 29 | + } |
| 30 | + }, |
| 31 | + useSynchronizationContext: false); |
20 | 32 |
|
21 | | - _ = process.Start(); |
| 33 | + process.WaitForExit(); |
22 | 34 |
|
23 | | - using var register = cancellationToken.Register( |
| 35 | + if (Interlocked.Read(ref cancelled) == 1) |
| 36 | + { |
| 37 | + cancellationToken.ThrowIfCancellationRequested(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static async Task RunAsync(this Process process, bool noEcho, string echoPrefix, bool cancellationIgnoresProcessTree, CancellationToken cancellationToken) |
| 42 | + { |
| 43 | + using var sync = new SemaphoreSlim(1, 1); |
| 44 | + var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 45 | + |
| 46 | + process.EnableRaisingEvents = true; |
| 47 | + process.Exited += (_, _) => sync.Run(() => tcs.Task.Status != TaskStatus.Canceled, () => _ = tcs.TrySetResult()); |
| 48 | + |
| 49 | + if (!noEcho) |
| 50 | + { |
| 51 | + await Console.Out.WriteAsync(process.StartInfo.GetEchoLines(echoPrefix)).ConfigureAwait(false); |
| 52 | + } |
| 53 | + |
| 54 | + _ = process.Start(); |
| 55 | + |
| 56 | + await using var register = cancellationToken.Register( |
| 57 | + () => sync.Run( |
| 58 | + () => tcs.Task.Status != TaskStatus.RanToCompletion, |
24 | 59 | () => |
25 | 60 | { |
26 | 61 | if (process.TryKill(cancellationIgnoresProcessTree)) |
27 | 62 | { |
28 | | - _ = Interlocked.Increment(ref cancelled); |
| 63 | + _ = tcs.TrySetCanceled(cancellationToken); |
29 | 64 | } |
30 | | - }, |
31 | | - useSynchronizationContext: false); |
| 65 | + }), |
| 66 | + useSynchronizationContext: false).ConfigureAwait(false); |
32 | 67 |
|
33 | | - process.WaitForExit(); |
| 68 | + await tcs.Task.ConfigureAwait(false); |
| 69 | + } |
34 | 70 |
|
35 | | - if (Interlocked.Read(ref cancelled) == 1) |
36 | | - { |
37 | | - cancellationToken.ThrowIfCancellationRequested(); |
38 | | - } |
39 | | - } |
| 71 | + private static string GetEchoLines(this System.Diagnostics.ProcessStartInfo info, string echoPrefix) |
| 72 | + { |
| 73 | + var builder = new StringBuilder(); |
40 | 74 |
|
41 | | - public static async Task RunAsync(this Process process, bool noEcho, string echoPrefix, bool cancellationIgnoresProcessTree, CancellationToken cancellationToken) |
| 75 | + if (!string.IsNullOrEmpty(info.WorkingDirectory)) |
42 | 76 | { |
43 | | - using var sync = new SemaphoreSlim(1, 1); |
44 | | - var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); |
| 77 | + _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: Working directory: {info.WorkingDirectory}"); |
| 78 | + } |
45 | 79 |
|
46 | | - process.EnableRaisingEvents = true; |
47 | | - process.Exited += (_, _) => sync.Run(() => tcs.Task.Status != TaskStatus.Canceled, () => _ = tcs.TrySetResult()); |
| 80 | + if (info.ArgumentList.Count > 0) |
| 81 | + { |
| 82 | + _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {info.FileName}"); |
48 | 83 |
|
49 | | - if (!noEcho) |
| 84 | + foreach (var arg in info.ArgumentList) |
50 | 85 | { |
51 | | - await Console.Out.WriteAsync(process.StartInfo.GetEchoLines(echoPrefix)).ConfigureAwait(false); |
| 86 | + _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {arg}"); |
52 | 87 | } |
53 | | - |
54 | | - _ = process.Start(); |
55 | | - |
56 | | - await using var register = cancellationToken.Register( |
57 | | - () => sync.Run( |
58 | | - () => tcs.Task.Status != TaskStatus.RanToCompletion, |
59 | | - () => |
60 | | - { |
61 | | - if (process.TryKill(cancellationIgnoresProcessTree)) |
62 | | - { |
63 | | - _ = tcs.TrySetCanceled(cancellationToken); |
64 | | - } |
65 | | - }), |
66 | | - useSynchronizationContext: false).ConfigureAwait(false); |
67 | | - |
68 | | - await tcs.Task.ConfigureAwait(false); |
69 | 88 | } |
70 | | - |
71 | | - private static string GetEchoLines(this System.Diagnostics.ProcessStartInfo info, string echoPrefix) |
| 89 | + else |
72 | 90 | { |
73 | | - var builder = new StringBuilder(); |
| 91 | + _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {info.FileName}{(string.IsNullOrEmpty(info.Arguments) ? "" : $" {info.Arguments}")}"); |
| 92 | + } |
74 | 93 |
|
75 | | - if (!string.IsNullOrEmpty(info.WorkingDirectory)) |
76 | | - { |
77 | | - _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: Working directory: {info.WorkingDirectory}"); |
78 | | - } |
| 94 | + return builder.ToString(); |
| 95 | + } |
79 | 96 |
|
80 | | - if (info.ArgumentList.Count > 0) |
81 | | - { |
82 | | - _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {info.FileName}"); |
| 97 | + private static bool TryKill(this Process process, bool ignoreProcessTree) |
| 98 | + { |
| 99 | + // exceptions may be thrown for all kinds of reasons |
| 100 | + // and the _same exception_ may be thrown for all kinds of reasons |
| 101 | + // System.Diagnostics.Process is "fine" |
| 102 | + try |
| 103 | + { |
| 104 | + process.Kill(!ignoreProcessTree); |
| 105 | + } |
| 106 | +#pragma warning disable CA1031 // Do not catch general exception types |
| 107 | + catch (Exception) |
| 108 | +#pragma warning restore CA1031 // Do not catch general exception types |
| 109 | + { |
| 110 | + return false; |
| 111 | + } |
83 | 112 |
|
84 | | - foreach (var arg in info.ArgumentList) |
85 | | - { |
86 | | - _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {arg}"); |
87 | | - } |
88 | | - } |
89 | | - else |
90 | | - { |
91 | | - _ = builder.AppendLine(CultureInfo.InvariantCulture, $"{echoPrefix}: {info.FileName}{(string.IsNullOrEmpty(info.Arguments) ? "" : $" {info.Arguments}")}"); |
92 | | - } |
| 113 | + return true; |
| 114 | + } |
93 | 115 |
|
94 | | - return builder.ToString(); |
| 116 | + private static void Run(this SemaphoreSlim sync, Func<bool> doubleCheckPredicate, Action action) |
| 117 | + { |
| 118 | + if (!doubleCheckPredicate()) |
| 119 | + { |
| 120 | + return; |
95 | 121 | } |
96 | 122 |
|
97 | | - private static bool TryKill(this Process process, bool ignoreProcessTree) |
| 123 | + try |
98 | 124 | { |
99 | | - // exceptions may be thrown for all kinds of reasons |
100 | | - // and the _same exception_ may be thrown for all kinds of reasons |
101 | | - // System.Diagnostics.Process is "fine" |
102 | | - try |
103 | | - { |
104 | | - process.Kill(!ignoreProcessTree); |
105 | | - } |
106 | | -#pragma warning disable CA1031 // Do not catch general exception types |
107 | | - catch (Exception) |
108 | | -#pragma warning restore CA1031 // Do not catch general exception types |
109 | | - { |
110 | | - return false; |
111 | | - } |
112 | | - |
113 | | - return true; |
| 125 | + sync.Wait(); |
| 126 | + } |
| 127 | + catch (ObjectDisposedException) |
| 128 | + { |
| 129 | + return; |
114 | 130 | } |
115 | 131 |
|
116 | | - private static void Run(this SemaphoreSlim sync, Func<bool> doubleCheckPredicate, Action action) |
| 132 | + try |
117 | 133 | { |
118 | | - if (!doubleCheckPredicate()) |
| 134 | + if (doubleCheckPredicate()) |
119 | 135 | { |
120 | | - return; |
| 136 | + action(); |
121 | 137 | } |
122 | | - |
| 138 | + } |
| 139 | + finally |
| 140 | + { |
123 | 141 | try |
124 | 142 | { |
125 | | - sync.Wait(); |
| 143 | + _ = sync.Release(); |
126 | 144 | } |
127 | 145 | catch (ObjectDisposedException) |
128 | 146 | { |
129 | | - return; |
130 | | - } |
131 | | - |
132 | | - try |
133 | | - { |
134 | | - if (doubleCheckPredicate()) |
135 | | - { |
136 | | - action(); |
137 | | - } |
138 | | - } |
139 | | - finally |
140 | | - { |
141 | | - try |
142 | | - { |
143 | | - _ = sync.Release(); |
144 | | - } |
145 | | - catch (ObjectDisposedException) |
146 | | - { |
147 | | - } |
148 | 147 | } |
149 | 148 | } |
150 | 149 | } |
|
0 commit comments