-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandQueue.cs
72 lines (61 loc) · 1.83 KB
/
CommandQueue.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Common.Shared.Min.Extensions;
using SRAM.Comparison.Helpers;
using SRAM.Comparison.Properties;
// ReSharper disable PossibleMultipleEnumeration
namespace SRAM.Comparison
{
/// <summary>Starts a UI-less cmd queue.</summary>
public class CommandQueue
{
private static CommandQueue? _instance;
public static CommandQueue Instance => _instance ??= new();
public void Start(IOptions options)
{
var commandHandler = ComparisonServices.CommandHandler;
var consolePrinter = ComparisonServices.ConsolePrinter;
commandHandler.ThrowIfNull(nameof(commandHandler));
consolePrinter.ThrowIfNull(nameof(consolePrinter));
options.BatchCommands.ThrowIfNull(nameof(options.BatchCommands));
if (options.CurrentFilePath.IsNullOrEmpty())
{
consolePrinter.PrintFatalError(Resources.ErrorMissingPathArguments);
Console.ReadKey();
return;
}
if (options.UILanguage is not null)
CultureHelper.TrySetCulture(options.UILanguage, consolePrinter);
var commands = options.BatchCommands?.Split('-') ?? Enumerable.Empty<string>();
var queuedCommands = new Queue<string>(commands);
consolePrinter.PrintLine(@$"{Resources.QueuedCommands}: {queuedCommands.Count} ({string.Join(", ", commands)})");
while (true)
{
try
{
string? command = null;
if (queuedCommands.Count > 0)
queuedCommands.TryDequeue(out command);
if (commandHandler.RunCommand(command!, options) == false)
break;
#if !DEBUG
if (queuedCommands.Count == 0)
break;
#endif
}
catch (IOException ex)
{
consolePrinter.PrintError(ex.Message);
consolePrinter.PrintSectionHeader();
}
catch (Exception ex)
{
consolePrinter.PrintError(ex);
consolePrinter.PrintSectionHeader();
}
}
}
}
}