-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
58 lines (53 loc) · 1.98 KB
/
Program.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
using System;
using System.Linq;
using System.Threading.Tasks;
using WordPressPluginAnalytics.Commands;
using System.Reflection;
using WordPressPluginAnalytics.Lib;
using System.Text.RegularExpressions;
namespace WordPressPluginAnalytics
{
class Program
{
static void Main(string[] args)
{
try
{
var program = new Program();
program.MainAsync(args[0]).Wait();
}
catch(Exception e)
{
Console.WriteLine($"Failure:\n{e.ToString()}");
}
}
async Task MainAsync(string command)
{
var assembly = Assembly.GetEntryAssembly();
var config = new Config();
var commandTypes = from type in assembly.GetTypes()
where type.Namespace == "WordPressPluginAnalytics.Commands"
where !type.GetTypeInfo().IsAbstract
where type.GetInterfaces().Contains(typeof(ICommand))
select type;
var lookup = commandTypes.ToDictionary(GetCommandName, t => t);
if(lookup.TryGetValue(command, out var commandType))
{
var commandObject = (ICommand)assembly.CreateInstance(commandType.FullName);
await commandObject.RunAsync(config);
return;
}
var commandList = string.Join(", ", lookup.Keys);
Console.WriteLine($"Commands: {commandList}");
}
private static string GetCommandName(Type type)
{
var command = type.Name;
command = command.Replace("Command", "");
command = Regex.Replace(command, "([A-Z])", "-$1");
command = command.Substring(1).ToLower();
command = command.Replace("word-press", "wordpress");
return command;
}
}
}