/
CommandMenu.cs
87 lines (72 loc) · 2.39 KB
/
CommandMenu.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
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Common.Shared.Min.Extensions;
using SRAM.Comparison.Enums;
using SRAM.Comparison.Helpers;
using SRAM.Comparison.Properties;
using SRAM.Comparison.Services;
namespace SRAM.Comparison
{
/// <summary>Starts a cmd menu loop.</summary>
public class CommandMenu
{
private static CommandMenu? _instance;
public static CommandMenu Instance => _instance ??= new();
private static ICommandHandler CommandHandler = ComparisonServices.CommandHandler!;
private static IConsolePrinter ConsolePrinter = ComparisonServices.ConsolePrinter;
protected virtual bool? OnRunCommand(ICommandHandler commandHandler, string command, IOptions options) => commandHandler.RunCommand(command!, options);
public void Show(IOptions options)
{
ConsoleHelper.Initialize(options);
CommandHandler.ThrowIfNull(nameof(CommandHandler));
ConsolePrinter.ThrowIfNull(nameof(ConsolePrinter));
options.BatchCommands.ThrowIfNotDefault(nameof(options.BatchCommands));
ConsolePrinter.PrintSectionHeader();
var version = ((CommandHandler)CommandHandler).AppVersion!;
#if DEBUG
ConsolePrinter.PrintConfigLine("CurrentDir", Environment.CurrentDirectory);
#endif
ConsolePrinter.PrintConfigLine("Version", "v" + version);
if (options.CurrentFilePath.IsNullOrEmpty())
{
ConsolePrinter.PrintFatalError(Resources.ErrorMissingPathArguments);
Console.ReadKey();
return;
}
CheckforUpdates();
ConsolePrinter.PrintConfig(options);
if (!File.Exists(FilePathHelper.GetComparisonFilePath(options)))
CommandHandler.RunCommand(nameof(Commands.OverwriteComp), options);
ConsolePrinter.PrintStartMessage();
if (options.AutoWatch)
CommandHandler.RunCommand(nameof(Commands.WatchFile), options);
while (true)
{
try
{
var command = Console.ReadLine();
if (!CommandHandler.RunCommand(command!, options))
break;
}
catch (TargetInvocationException ex)
{
ConsolePrinter.PrintError(ex.InnerException!.Message);
ConsolePrinter.PrintSectionHeader();
}
catch (Exception ex)
{
ConsolePrinter.PrintError(ex.Message);
ConsolePrinter.PrintSectionHeader();
}
}
}
private static void CheckforUpdates()
{
if (!File.Exists(Services.CommandHandler.DefaultUpdateFileName))
return;
((IAutoUpdater)CommandHandler).CheckForUpdates();
}
}
}