-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
324 lines (304 loc) · 9.21 KB
/
Program.cs
File metadata and controls
324 lines (304 loc) · 9.21 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.ServiceProcess;
namespace TestBench
{
partial class Program
{
// An internal pointer to the current Instance of the class
// used to reference Program and avoid using Activator Create Instance Calls
// 'this' will not work for static method calls
protected static Program m_this;
protected static string m_prompt = ":>";
protected static Type m_type = typeof(Program);
protected static List<MethodInfo> m_methods = null;
internal Program()
{
m_this = this;
}
[STAThread]
static void Main(string[] args)
{
// TestBench is running as a Service
if (Environment.UserInteractive == false)
{
ServiceBase[] ServicesToRun = new ServiceBase[] { new SampleService() };
ServiceBase.Run(ServicesToRun);
}
else
{
CreateMethodCache();
AddTraceListeners();
Console.Clear();
Console.ForegroundColor = ConsoleColor.White;
Console.BufferHeight = 300;
Console.BufferWidth = 100;
Console.Title = "Test Bench";
if (args.Length > 0)
{
RunCommand(args);
return;
}
MainLoop();
}
}
static void MainLoop()
{
bool pContinue = true;
while (pContinue)
{
Console.Write(m_prompt);
//string[] Coms = Console.ReadLine().Split(new char[] { ' ' });
var parms = ParseCommand(Console.ReadLine());
if (parms.Length == 0)
continue;
switch (parms[0].ToLower())
{
case "exit":
case "quit":
pContinue = false;
break;
default:
RunCommand(parms);
break;
}
Console.WriteLine("");
}
}
static string[] ParseCommand(string args)
{
var parts = Regex.Matches(args, @"[\""].+?[\""]|[^ ]+")
.Cast<Match>()
.Select(x => x.Value.Replace("\"", ""))
.ToArray();
return parts;
}
static void RunCommand(string[] args)
{
string CallingMethod = args[0];
var parms = args.Skip(1).Take(args.Count() - 1).ToArray();
var method = m_methods.FirstOrDefault(x => String.Compare(x.Name, CallingMethod, true) == 0 && x.GetParameters().Length == parms.Length);
// Abandon the call if we didn't find a match
if(method == null)
{
Console.WriteLine("Unknown Command");
Program.Help();
return;
}
try
{
method.Invoke(m_this, parms);
return;
}
catch (Exception e)
{
if (e.InnerException != null)
{
Console.WriteLine();
WriteExceptions(e.InnerException);
}
return;
}
}
[Description("Shows Help for All Commands")]
static void Help()
{
Console.WriteLine("Valid Commands");
foreach (MethodInfo m in m_methods)
{
DescriptionAttribute[] attribs = (DescriptionAttribute[])m.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attribs != null && attribs.Length > 0)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.Write(m.Name);
ParameterInfo[] parm = m.GetParameters();
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("(");
for (int i = 0; i < parm.Length; i++)
{
if (i > 0)
Console.Write(", ");
Console.Write("({0}){1}", parm[i].ParameterType.Name, parm[i].Name);
}
Console.Write(")");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("\n\t{0}", attribs[0].Description);
}
}
}
[Description("Clears the Current display Buffer")]
static void Clear()
{
Console.Clear();
}
[Description("Quits out of the application")]
static void Quit()
{
return;
}
[Description("List Local Drives")]
static void LocalDrives()
{
string[] drives = Environment.GetLogicalDrives();
IEnumerable<string> strs = drives.Select(s => s.Replace(":\\", ""));
foreach (String s in strs)
{
System.IO.DriveInfo drvi = new System.IO.DriveInfo(s);
if (drvi.DriveType == DriveType.CDRom)
continue;
Console.WriteLine("{0}:\\", s);
}
}
[Description("List Available Providers")]
static void LocalProviders()
{
var dt = System.Data.Common.DbProviderFactories.GetFactoryClasses();
// Name Description InvariantName
Console.WriteLine("{0} {1} {2}", "", "Name", "InvariantName");
Console.WriteLine("-------------------------------------");
foreach (System.Data.DataRow dr in dt.Rows)
{
Console.WriteLine("{0} {1} {2}", "", dr["Name"], dr["InvariantName"]);
}
}
[Description("Open Application Log Folder")]
static void OpenLogFolder()
{
var path = Path.Combine(GetCurrentPath(), "ApplicationLogs");
if(Directory.Exists(path) == false)
{
path = GetCurrentPath();
}
Process.Start(path);
}
[Description("Menu Sample")]
static void SampleMenu()
{
List<MenuChoice> _choices = new List<MenuChoice>();
_choices.Add(new MenuChoice(new System.Action(Program.LocalDrives), "List Local Drives"));
_choices.Add(new MenuChoice(new System.Action(Program.OpenLogFolder), "Opens the Current Log Folder"));
_choices.Add(new MenuChoice(new System.Action(Program.Help), "Help"));
_choices.Add(new MenuChoice(new System.Action(Program.LocalProviders), "List Available Providers"));
Menu m = new Menu(_choices);
m.RunMenu();
if (m.Canceled)
return;
foreach (MenuChoice mc in _choices)
{
if (mc.Selected == false)
continue;
mc.Execute();
}
}
static void writeHeader<T>(T data)
{
if (data == null)
{
Console.WriteLine("Unable to create Header from Null Object");
return;
}
var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in props)
{
Console.Write("{0, 8} ", prop.Name);
}
Console.WriteLine();
}
static void writeCollectionData<T>(IEnumerable<T> data)
{
if (data == null)
{
Console.WriteLine("Unable to write data from Null Object");
return;
}
var props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var i = 0;
foreach (var t in data)
{
++i;
Console.Write("{0}: ", i);
writeData(t, props);
Console.WriteLine();
}
}
static void writeData<T>(T data, IEnumerable<PropertyInfo> propertyInfo = null)
{
if (data == null)
{
Console.WriteLine("Unable to write data from Null Object");
return;
}
var props = (propertyInfo != null) ? propertyInfo : typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in props)
{
Console.Write("{0, 8} ", prop.GetValue(data));
}
}
static TimeSpan CalculateEta(DateTime startTime, int totalItems, int completeItems)
{
TimeSpan _eta = TimeSpan.MinValue;
// Avoid Divide by Zero Errors
if (completeItems > 0)
{
int _itemduration = (int)DateTime.Now.Subtract(startTime).TotalMilliseconds / completeItems;
_eta = TimeSpan.FromMilliseconds((double)((totalItems - completeItems) * _itemduration));
}
return _eta;
}
static void WriteExceptions(Exception e)
{
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Trace.Write("Source:");
Trace.Write(e.Source);
Trace.WriteLine("\nMessage:");
Trace.Write(e.Message);
Trace.WriteLine("\nStack Trace:");
Trace.Write(e.StackTrace);
Trace.WriteLine("\nUser Defined Data:");
foreach (System.Collections.DictionaryEntry de in e.Data)
{
Trace.WriteLine(string.Format("[{0}] :: {1}", de.Key, de.Value));
}
if (e.InnerException != null)
{
WriteExceptions(e.InnerException);
}
Console.ForegroundColor = ConsoleColor.White;
}
static string GetCurrentPath()
{
var asm = Assembly.GetExecutingAssembly();
var fi = new FileInfo(asm.Location);
return fi.DirectoryName;
}
static void HexDump(byte[] bytes)
{
for (int line = 0; line < bytes.Length; line += 16)
{
byte[] lineBytes = bytes.Skip(line).Take(16).ToArray();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("{0:x8} ", line);
sb.Append(string.Join(" ", lineBytes.Select(b => b.ToString("x2")).ToArray()).PadRight(16 * 3));
sb.Append(" ");
sb.Append(new string(lineBytes.Select(b => b < 32 ? '.' : (char)b).ToArray()));
Console.WriteLine(sb);
}
}
static void CreateMethodCache()
{
m_methods = m_type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic).ToList();
}
static void AddTraceListeners()
{
TextWriterTraceListener CWriter = new TextWriterTraceListener(Console.Out);
Trace.Listeners.Add(CWriter);
}
}
}