| | 1 | | using System.Diagnostics; |
| | 2 | |
|
| | 3 | | namespace Dotnet.Installer.Core.Types; |
| | 4 | |
|
| | 5 | | public static class Terminal |
| | 6 | | { |
| | 7 | | public static async Task<InvocationResult> Invoke(string program, params string[] arguments) |
| 0 | 8 | | { |
| 0 | 9 | | return await Invoke(program, options: null, arguments); |
| 0 | 10 | | } |
| | 11 | |
|
| | 12 | | public static async Task<InvocationResult> Invoke(string program, InvocationOptions? options = default, |
| | 13 | | params string[] arguments) |
| 0 | 14 | | { |
| 0 | 15 | | options ??= InvocationOptions.Default; |
| | 16 | |
|
| 0 | 17 | | var process = new Process(); |
| | 18 | |
|
| 0 | 19 | | process.StartInfo.FileName = program; |
| | 20 | |
|
| 0 | 21 | | foreach (var argument in arguments) |
| 0 | 22 | | process.StartInfo.ArgumentList.Add(argument); |
| | 23 | |
|
| 0 | 24 | | process.StartInfo.CreateNoWindow = true; |
| 0 | 25 | | process.StartInfo.RedirectStandardOutput = options.RedirectStandardOutput; |
| 0 | 26 | | process.StartInfo.RedirectStandardError = options.RedirectStandardError; |
| | 27 | |
|
| 0 | 28 | | process.Start(); |
| 0 | 29 | | await process.WaitForExitAsync(); |
| | 30 | |
|
| 0 | 31 | | return new InvocationResult |
| 0 | 32 | | { |
| 0 | 33 | | ExitCode = process.ExitCode, |
| 0 | 34 | | RedirectedStandardError = options.RedirectStandardError, |
| 0 | 35 | | RedirectedStandardOutput = options.RedirectStandardOutput, |
| 0 | 36 | | StandardError = options.RedirectStandardError ? |
| 0 | 37 | | await process.StandardError.ReadToEndAsync() : default, |
| 0 | 38 | | StandardOutput = options.RedirectStandardOutput ? |
| 0 | 39 | | await process.StandardOutput.ReadToEndAsync() : default |
| 0 | 40 | | }; |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public class InvocationOptions |
| | 44 | | { |
| 0 | 45 | | public static InvocationOptions Default => new(); |
| | 46 | |
|
| 0 | 47 | | public bool RedirectStandardOutput { get; set; } = false; |
| 0 | 48 | | public bool RedirectStandardError { get; set; } = false; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public class InvocationResult |
| | 52 | | { |
| 0 | 53 | | public InvocationResult() |
| 0 | 54 | | { } |
| | 55 | |
|
| 15 | 56 | | public InvocationResult(int exitCode, string standardOutput, string standardError) |
| 15 | 57 | | { |
| 15 | 58 | | ExitCode = exitCode; |
| 15 | 59 | | StandardOutput = standardOutput; |
| 15 | 60 | | StandardError = standardError; |
| | 61 | |
|
| 15 | 62 | | RedirectedStandardError = true; |
| 15 | 63 | | RedirectedStandardOutput = true; |
| 15 | 64 | | } |
| | 65 | |
|
| 29 | 66 | | public bool IsSuccess => ExitCode == 0; |
| 44 | 67 | | public int ExitCode { get; init; } |
| 15 | 68 | | public bool RedirectedStandardOutput { get; init; } |
| 15 | 69 | | public bool RedirectedStandardError { get; init; } |
| 15 | 70 | | public string? StandardOutput { get; init; } |
| 15 | 71 | | public string? StandardError { get; init; } |
| | 72 | | } |
| | 73 | | } |