/
Program.cs
271 lines (225 loc) · 9.89 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using CommandLine;
using Giles.Core.Configuration;
using Giles.Core.Utility;
using Giles.Core.Watchers;
using Giles.Options;
using Ninject;
namespace Giles
{
class Program
{
static SourceWatcher sourceWatcher;
static GilesConfig config;
static bool quitRequested;
static IList<InteractiveMenuOption> menuOptions;
static StandardKernel kernel;
static void Main(string[] args)
{
var options = new CLOptions();
Console.WriteLine(GetGilesFunnyLine());
var parser = new CommandLineParser(
new CommandLineParserSettings(false, Console.Error));
if (!parser.ParseArguments(args, options))
{
Console.WriteLine("Unable to determine what command lines arguments were used, check the help above!\nThe minimum needed is the -s [solution file path].");
Environment.Exit(1);
}
config = GetGilesConfigFor(options);
kernel = new StandardKernel(new SlayerModule(config));
ConsoleSetup();
sourceWatcher = StartSourceWatcher();
menuOptions = GetInteractiveMenuOptions();
DisplayInteractiveMenuOptions();
MainFeedbackLoop();
}
static GilesConfig GetGilesConfigFor(CLOptions options)
{
var solutionPath = options.SolutionPath.Replace("\"", string.Empty);
var testAssemblies = GetTestAssemblies(options);
solutionPath = Path.GetFullPath(solutionPath);
for (var i = 0; i < testAssemblies.Count() - 1; i++)
{
testAssemblies[i] = Path.GetFullPath(testAssemblies[i]);
}
return SetupGilesConfig(solutionPath, testAssemblies);
}
static string GetGilesFunnyLine()
{
var name = Assembly.GetExecutingAssembly().GetName();
return string.Format(@"Grr, argh... v{0}.{1}.{2}.{3}",
name.Version.Major,
name.Version.Minor,
name.Version.Revision,
name.Version.Build);
}
static SourceWatcher StartSourceWatcher()
{
var watcher = kernel.Get<SourceWatcher>();
// HACK: Only *.cs files? Really?
watcher.Watch(config.SolutionPath, @"*.cs");
return watcher;
}
static List<string> GetTestAssemblies(CLOptions options)
{
var testAssemblies = options.GetTestAssemblies();
testAssemblies = testAssemblies.Count > 0 ?
testAssemblies :
FindTestAssembly(options.SolutionPath);
if (testAssemblies == null)
{
Console.Error.Write(options.GetUsage());
Console.Error.WriteLine("No test assemblies detected. Please specify"
+ " the TestAssemblyPaths command line option.");
Console.Error.WriteLine();
Environment.Exit(1);
}
return testAssemblies;
}
private static List<string> FindTestAssembly(string solutionPath)
{
var finder = new TestAssemblyFinder();
var assemblies = finder.FindTestAssembliesIn(solutionPath);
return assemblies;
}
static GilesConfig SetupGilesConfig(string solutionPath, List<string> testAssemblies)
{
var builder = new GilesConfigBuilder(solutionPath, testAssemblies);
return builder.Build();
}
static void ConsoleSetup()
{
Console.Clear();
Console.Title = GetGilesFunnyLine();
Console.WriteLine("Giles - your own personal watcher");
Console.WriteLine("\t\"I'd like to test that theory...\"\n\n");
}
static InteractiveMenuOption[] GetInteractiveMenuOptions()
{
return new[]
{
new InteractiveMenuOption { HandlesKey = key => key == "?", Task = DisplayInteractiveMenuOptions },
new InteractiveMenuOption { HandlesKey = key => key == "i", Task = DisplayConfig },
new InteractiveMenuOption { HandlesKey = key => key == "c", Task = Console.Clear },
new InteractiveMenuOption { HandlesKey = key => key == "r", Task = sourceWatcher.RunNow },
new InteractiveMenuOption { HandlesKey = key => key == "b", Task = SetBuildDelay },
new InteractiveMenuOption { HandlesKey = key => key == "q", Task = RequestQuit },
new InteractiveMenuOption { HandlesKey = key => key == "v", Task = DisplayVerboseResults },
new InteractiveMenuOption { HandlesKey = key => key == "e", Task = DisplayErrors },
new InteractiveMenuOption { HandlesKey = key => key == "f", Task = SetTestFilters },
new InteractiveMenuOption { HandlesKey = key => key == "h", Task = ClearTestFilters },
};
}
static void MainFeedbackLoop()
{
while (!quitRequested)
{
var keyValue = Console.ReadKey(true).KeyChar.ToString().ToLower();
menuOptions
.Where(option => option.HandlesKey(keyValue))
.Each(option => option.Task());
}
Console.WriteLine("Until next time...");
}
static void RequestQuit()
{
quitRequested = true;
}
static void SetTestFilters()
{
config.Filters = GetUserValues("Filters", config.Filters);
Console.WriteLine("Filters set to:");
config.Filters.Each(x => Console.WriteLine("\t{0}", x));
}
static void ClearTestFilters()
{
config.Filters = new List<string>();
Console.WriteLine("Test filters cleared");
}
static void SetBuildDelay()
{
config.BuildDelay = GetUserValue("Build Delay", config.BuildDelay);
}
static void DisplayErrors()
{
if (LastRunResults.GilesTestListener != null)
LastRunResults.GilesTestListener.DisplayErrors();
else
Console.WriteLine("Please run some tests first...");
}
static void DisplayVerboseResults()
{
if (LastRunResults.GilesTestListener != null)
LastRunResults.GilesTestListener.DisplayVerboseResults();
else
Console.WriteLine("Please run some tests first...");
}
static List<T> GetUserValues<T>(string description, List<T> defaultValues)
{
Console.WriteLine("Filters: Enter a namespace and type (MyNamespace.FooTests), one on each line. Use a blank line save.");
Console.WriteLine(" Current settings: {0}", defaultValues);
var newValues = new List<T>();
string newLine;
do
{
newLine = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newLine))
newValues.Add((T)Convert.ChangeType(newLine, typeof(T)));
}
while (!string.IsNullOrWhiteSpace(newLine));
return newValues;
}
static T GetUserValue<T>(string description, T defaultValue)
{
Console.Write("Enter new value for {0} ({1}): ", description, defaultValue);
var newValue = Console.ReadLine();
if (string.IsNullOrWhiteSpace(newValue))
return defaultValue;
return (T)Convert.ChangeType(newValue, typeof(T));
}
static void DisplayConfig()
{
Console.WriteLine("\nCurrent Configuration");
Console.WriteLine(" Build Delay: {0}", config.BuildDelay);
Console.WriteLine(" Solution: {0}", config.SolutionPath);
Console.WriteLine(" Test Assemblies: \n\t{0}", GetTestAssemblyListAsString());
config.TestRunners.Each(r => Console.WriteLine(" {0} Has been enabled", r.Key));
Console.WriteLine(" Test Filters: \n\t{0}", GetTestFilterListAsString());
Console.WriteLine();
}
static string GetTestAssemblyListAsString()
{
return config.TestAssemblies.Aggregate((next, working) => working = working + Environment.NewLine + "\t" + next);
}
static string GetTestFilterListAsString()
{
return config.Filters.Count == 0
? "<All Classes>"
: config.Filters.Aggregate((next, working) => working = working + Environment.NewLine + "\t" + next);
}
static void DisplayInteractiveMenuOptions()
{
Console.WriteLine("Interactive Console Options:");
Console.WriteLine(" ? = Display options");
Console.WriteLine(" C = Clear the window");
Console.WriteLine(" I = Show current configuration");
Console.WriteLine(" R = Run build & tests now");
Console.WriteLine(" V = Display all messages from last test run");
Console.WriteLine(" E = Display errors from last test run");
Console.WriteLine(" B = Set Build Delay");
Console.WriteLine(" F = Set Test Filters");
Console.WriteLine(" H = Clear Test Filters");
Console.WriteLine(" Q = Quit");
Console.WriteLine();
}
}
public class InteractiveMenuOption
{
public Func<string, bool> HandlesKey;
public Action Task;
}
}