|
1 | | -using System; |
2 | | -using System.Diagnostics; |
| 1 | +using System.Diagnostics; |
3 | 2 |
|
4 | 3 | namespace ShellRunner; |
| 4 | + |
5 | 5 | public class CommandBuilder |
6 | 6 | { |
7 | | - CommandBuilderOptions _options; |
| 7 | + CommandBuilderOptions options; |
| 8 | + List<ProcessCommand> commands; |
| 9 | + Dictionary<string, ProcessCommand> commandMap; |
8 | 10 |
|
9 | 11 | public ProcessStartInfo StartInfo { get; set; } |
10 | 12 | public Process Process { get; set; } |
| 13 | + public IReadOnlyList<IProcessedCommand> Commands => commands.AsReadOnly(); |
11 | 14 |
|
12 | 15 | internal CommandBuilder(CommandBuilderOptions options, Process process) |
13 | 16 | { |
14 | 17 | Process = process; |
15 | 18 |
|
16 | | - Commands = new List<ProcessCommand>(); |
17 | | - |
18 | | - _options = options; |
| 19 | + commands = new List<ProcessCommand>(); |
| 20 | + this.commandMap = new Dictionary<string, ProcessCommand>(); |
| 21 | + |
| 22 | + this.options = options; |
19 | 23 |
|
20 | 24 | StartInfo = new ProcessStartInfo |
21 | 25 | { |
22 | | - FileName = options.File, |
23 | | - Arguments = options.Args, |
| 26 | + FileName = this.options.File, |
| 27 | + Arguments = this.options.Args, |
24 | 28 | CreateNoWindow = true, |
25 | 29 | WindowStyle = ProcessWindowStyle.Hidden, |
26 | 30 | UseShellExecute = false, |
27 | | - RedirectStandardOutput = options.RedirectStandardOutput, |
28 | | - RedirectStandardError = options.RedirectStandardError, |
29 | | - RedirectStandardInput = options.RedirectStandardInput |
| 31 | + RedirectStandardOutput = this.options.RedirectStandardOutput, |
| 32 | + RedirectStandardError = this.options.RedirectStandardError, |
| 33 | + RedirectStandardInput = this.options.RedirectStandardInput |
30 | 34 | }; |
31 | 35 | } |
32 | 36 |
|
33 | | - public List<ProcessCommand> Commands { get; private set; } |
34 | | - |
35 | | - public CommandBuilder AddWorkingDirectory(string workingDirectory) |
| 37 | + /// <summary> |
| 38 | + /// Add a command to the command list. |
| 39 | + /// </summary> |
| 40 | + /// <param name="command"></param> |
| 41 | + /// <exception cref="ArgumentException">When a duplicate key is added.</exception> |
| 42 | + public void AddCommand(ProcessCommand command) |
36 | 43 | { |
37 | | - StartInfo.WorkingDirectory = workingDirectory; |
38 | | - return this; |
| 44 | + this.commands.Add(command); |
| 45 | + |
| 46 | + if(this.commandMap.ContainsKey(command.Key)) |
| 47 | + throw new ArgumentException($"Command with key \"{command.Key}\" already exists."); |
| 48 | + |
| 49 | + this.commandMap.Add(command.Key, command); |
39 | 50 | } |
40 | 51 |
|
41 | | - private void Proc_ErrorDataReceived(object sender, DataReceivedEventArgs e) |
| 52 | + /// <summary> |
| 53 | + /// Get a specific command by key. |
| 54 | + /// </summary> |
| 55 | + /// <param name="key">The key for the command ran earlier.</param> |
| 56 | + /// <returns></returns> |
| 57 | + /// <exception cref="KeyNotFoundException">When a key isn't present in the dictionary.</exception> |
| 58 | + public IProcessedCommand GetCommand(string key) |
42 | 59 | { |
43 | 60 | try |
44 | 61 | { |
45 | | - if (e.Data is null) |
46 | | - return; |
47 | | - |
48 | | - var fg = Console.ForegroundColor; |
49 | | - Console.ForegroundColor = ConsoleColor.Yellow; |
50 | | - Console.WriteLine($"Error: {e.Data}"); |
51 | | - Console.ForegroundColor = fg; |
| 62 | + return this.commandMap[key]; |
52 | 63 | } |
53 | | - catch(Exception ex) |
| 64 | + catch (KeyNotFoundException) |
54 | 65 | { |
55 | | - var fg = Console.ForegroundColor; |
56 | | - Console.ForegroundColor = ConsoleColor.Red; |
57 | | - Console.WriteLine("Error while processing error data."); |
58 | | - Console.WriteLine(ex); |
59 | | - Console.ForegroundColor = fg; |
| 66 | + throw new KeyNotFoundException($"Command with key \"{key}\" not found."); |
60 | 67 | } |
61 | 68 | } |
62 | 69 |
|
63 | | - private void Proc_OutputDataReceived(object sender, DataReceivedEventArgs e) |
| 70 | + public CommandBuilder AddWorkingDirectory(string workingDirectory) |
64 | 71 | { |
65 | | - try |
66 | | - { |
67 | | - Console.WriteLine(e.Data); |
68 | | - } |
69 | | - catch (Exception ex) |
| 72 | + StartInfo.WorkingDirectory = workingDirectory; |
| 73 | + return this; |
| 74 | + } |
| 75 | + |
| 76 | + public async Task<CommandBuilder> RunAsync(CancellationToken cancellationToken = default) |
| 77 | + { |
| 78 | + foreach(var command in commands) |
70 | 79 | { |
71 | | - var fg = Console.ForegroundColor; |
72 | | - Console.ForegroundColor = ConsoleColor.Red; |
73 | | - Console.WriteLine("Error while processing output data."); |
74 | | - Console.WriteLine(ex); |
75 | | - Console.ForegroundColor = fg; |
| 80 | + if (cancellationToken.IsCancellationRequested) |
| 81 | + break; |
| 82 | + |
| 83 | + await command.RunCommandAsync(this.Process, cancellationToken); |
76 | 84 | } |
| 85 | + |
| 86 | + return this; |
77 | 87 | } |
| 88 | + |
78 | 89 | } |
0 commit comments