-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cs
447 lines (394 loc) · 15.1 KB
/
build.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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/* (c) 2017 James Renwick */
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace cci
{
public class SystemCommand
{
/// <summary>
/// Gets the name of this command, or null if none given.
/// </summary>
[JsonProperty]
public string Name { get; private set; }
/// <summary>
/// Gets the command to be run.
/// </summary>
[JsonProperty, JsonRequired]
public string Command { get; private set; }
/// <summary>
/// Gets the command timeout in milliseconds.
/// </summary>
[JsonProperty]
public int Timeout { get; private set; }
/// <summary>
/// Gets the command's working directory, or null if the
/// current is to be used.
/// </summary>
[JsonProperty]
public string WorkingDirectory { get; private set; }
/// <summary>
/// Gets the additional environment variables under which the command
/// will be executed, or null if none specified.
/// </summary>
[JsonProperty]
public Dictionary<string, string> Environment { get; private set; }
[JsonConstructor]
private SystemCommand()
{
}
public SystemCommand(string command)
{
Command = command;
}
public SystemCommand(string command, string workingDirectory) : this(command)
{
WorkingDirectory = workingDirectory;
}
public SystemCommand(string name, string command, int timeout,
string workingDirectory, Dictionary<string, string> environment)
{
Name = name;
Command = command;
Timeout = timeout;
WorkingDirectory = workingDirectory;
Environment = environment;
}
}
public class ProjectDefinition
{
/// <summary>
/// Gets the title of the project.
/// </summary>
[JsonProperty, JsonRequired]
public string Title { get; private set; }
/// <summary>
/// Gets the git repository URI of the project.
/// </summary>
[JsonProperty, JsonRequired]
public string Repository { get; private set; }
/// <summary>
/// Gets the commit reference (commit hash or branch)
/// to be checked out, or null for default.
/// </summary>
[JsonProperty]
public string CommitReference { get; private set; }
/// <summary>
/// Gets the list of commands to run to build the project.
/// </summary>
[JsonProperty]
public SystemCommand[] BuildSteps { get; private set; }
/// <summary>
/// Gets the list of commands to run to test the project.
/// </summary>
[JsonProperty]
public SystemCommand[] TestSteps { get; private set; }
/// <summary>
/// Gets the default timeout for commands run as part of the project's
/// build or test procedures.
/// </summary>
[JsonProperty]
public int DefaultCommandTimeout { get; private set; }
[JsonConstructor]
private ProjectDefinition()
{
}
/// <summary>
/// Creates a ProjectDefinition from its JSON definition.
/// </summary>
/// <param name="json">The JSON representation of a ProjectDefinition.</param>
/// <returns>The ProjectDefinition represented by the given JSON.</returns>
public static ProjectDefinition FromJSON(string json)
{
return JsonConvert.DeserializeObject<ProjectDefinition>(json);
}
}
public enum OutputStream
{
Stdout,
Stderr
}
public struct OutputLine
{
/// <summary>
/// Gets the stream source of the line.
/// </summary>
public OutputStream Source { get; private set; }
/// <summary>
/// Gets the line's string value.
/// </summary>
public string Text { get; private set; }
/// <summary>
/// Creates a new OutputLine.
/// </summary>
/// <param name="source">The source stream.</param>
/// <param name="line">The string value.</param>
public OutputLine(OutputStream source, string line)
{
Source = source;
Text = line;
}
}
public class CommandResult
{
/// <summary>
/// Gets the command executed.
/// </summary>
public SystemCommand Command { get; private set; }
/// <summary>
/// Gets the output of the command.
/// </summary>
public OutputLine[] Lines { get; private set; }
/// <summary>
/// Gets the exit code of the command. Zero indicates success.
/// </summary>
public int ExitCode { get; private set; }
/// <summary>
/// Gets whether the command timed out.
/// </summary>
public bool TimedOut { get; private set; }
/// <summary>
/// Gets the execution duration of the command.
/// </summary>
public TimeSpan Duration { get; private set; }
/// <summary>
/// Gets the execution start time of the command.
/// </summary>
public DateTime StartTime { get; private set; }
/// <summary>
/// Creates a new CommandResult.
/// </summary>
/// <param name="command">The command exected.</param>
/// <param name="lines">The command's output.</param>
/// <param name="exitCode">The command's exit code.</param>
/// <param name="timedOut">Whether execution of the command timed out.</param>
/// <param name="duration">The execution duration of the command.</param>
/// <param name="startTime">The start time of the command's execution.</param>
public CommandResult(SystemCommand command, OutputLine[] lines, int exitCode,
bool timedOut, TimeSpan duration, DateTime startTime)
{
Command = command;
Lines = lines;
ExitCode = exitCode;
TimedOut = timedOut;
Duration = duration;
StartTime = startTime;
}
}
public class CommandRunner
{
private async void ConsumeStream(OutputStream stream, StreamReader reader, List<OutputLine> lines)
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
lock (lines) { lines.Add(new OutputLine(stream, line)); }
}
}
public CommandResult RunCommand(SystemCommand cmd,
int timeout = 0, string baseDir = null)
{
try {
var parts = cmd.Command.Split(new char[] { ' ' }, 2);
var proc = new ProcessStartInfo(parts[0], parts.Length > 1 ? parts[1] : "");
// Get working directory
string dir = baseDir ?? ".";
if (cmd.WorkingDirectory != null) {
dir = System.IO.Path.Combine(dir, cmd.WorkingDirectory);
}
// Get environment
if (cmd.Environment != null) {
proc.Environment.Concat(cmd.Environment);
}
proc.WorkingDirectory = dir;
proc.CreateNoWindow = true;
proc.RedirectStandardError = true;
proc.RedirectStandardOutput = true;
proc.UseShellExecute = false;
using (var process = new Process())
{
var lines = new List<OutputLine>(20);
process.StartInfo = proc;
DateTime startTime = DateTime.UtcNow;
process.Start();
ConsumeStream(OutputStream.Stderr, process.StandardError, lines);
ConsumeStream(OutputStream.Stdout, process.StandardOutput, lines);
var exited = process.WaitForExit(timeout != 0 ? timeout : (
cmd.Timeout != 0 ? cmd.Timeout : int.MaxValue));
if (!exited) try {
process.Kill();
} catch { }
// Return result
TimeSpan duration = DateTime.UtcNow - startTime;
return new CommandResult(cmd, lines.ToArray(),
process.ExitCode, !exited, duration, startTime);
}
}
catch (Exception e) {
Console.WriteLine($"[ERROR] Exception running \"{cmd.Command}: {e}\"");
return new CommandResult(cmd, new OutputLine[0], -1, false, TimeSpan.FromSeconds(0), DateTime.UtcNow);
}
}
}
/// <summary>
/// Object representing the execution of one of a project's stages.
/// </summary>
public class StageResults
{
/// <summary>
/// Gets the name of the stage that was executed.
/// </summary>
public string Stage { get; private set; }
/// <summary>
/// Gets the results of the commands which were executed.
/// </summary>
public CommandResult[] Results { get; private set; }
/// <summary>
/// Gets whether all commands in the stage executed successfully.
/// </summary>
public bool Successful {
get { return Results.All((r) => r.ExitCode == 0); }
}
/// <summary>
/// Creates a new StageResults object.
/// </summary>
/// <param name="stage">The name of the stage.</param>
/// <param name="results">The results of the stage's execution.</param>
public StageResults(string stage, CommandResult[] results)
{
Stage = stage;
Results = results;
}
private static CommandResult[] emptyResults = new CommandResult[0];
public static StageResults Empty(string stage)
{
return new StageResults(stage, emptyResults);
}
}
public class RunResults
{
/// <summary>
/// Gets the project that was run.
/// </summary>
public ProjectDefinition Project { get; private set; }
/// <summary>
/// Gets the results of the project's setup stage.
/// </summary>
public StageResults SetupResults { get; private set; }
/// <summary>
/// Gets the results of the project's build stage.
/// </summary>
public StageResults BuildResults { get; private set; }
/// <summary>
/// Gets the results of the project's test stage.
/// </summary>
public StageResults TestResults { get; private set; }
public RunResults(ProjectDefinition project, StageResults setupResults,
StageResults buildResults, StageResults testResults)
{
Project = project;
SetupResults = setupResults;
BuildResults = buildResults;
TestResults = testResults;
}
}
public class ProjectRunner : IDisposable
{
public ProjectDefinition Project { get; protected set; }
public string WorkspaceDir { get; private set; }
public ProjectRunner(ProjectDefinition project)
{
Project = project;
WorkspaceDir = Guid.NewGuid().ToString();
}
public StageResults RunSetupStage()
{
var step = new SystemCommand(
"git clone -q --recurse-submodules --depth 1 " +
"--single-branch " +
$"-o {Project.CommitReference} -- {Project.Repository} {WorkspaceDir}");
var results = new[] {
new CommandRunner().RunCommand(step, 1000 * 10)
};
return new StageResults("setup", results);
}
public StageResults RunBuildStage()
{
var results = new List<CommandResult>();
foreach (var step in Project.BuildSteps)
{
var result = new CommandRunner().RunCommand(
step, Project.DefaultCommandTimeout, WorkspaceDir);
results.Add(result);
// Break on first failure
if (result.ExitCode != 0) break;
}
return new StageResults("build", results.ToArray());
}
public StageResults RunTestStage()
{
return new StageResults("test",
Project.TestSteps.Select((step) =>
new CommandRunner().RunCommand(step,
Project.DefaultCommandTimeout, WorkspaceDir)).ToArray());
}
public RunResults RunAll()
{
Console.WriteLine("[INFO ] Running setup stage...");
var setupResults = RunSetupStage();
var buildResults = StageResults.Empty("build");
var testResults = StageResults.Empty("test");
if (setupResults.Successful) {
Console.WriteLine("[INFO ] Running build stage...");
buildResults = RunBuildStage();
}
if (buildResults.Successful) {
Console.WriteLine("[INFO ] Running test stage...");
testResults = RunTestStage();
}
return new RunResults(this.Project, setupResults,
buildResults, testResults);
}
private static void SetFileAttributes(System.IO.DirectoryInfo dir,
System.IO.FileAttributes attributes)
{
foreach (var subDir in dir.GetDirectories())
{
SetFileAttributes(subDir, attributes);
subDir.Attributes = attributes;
}
foreach (var file in dir.GetFiles()) {
file.Attributes = attributes;
}
}
protected virtual void Dispose(bool disposing)
{
// If present, try to remove all workspace files
if (System.IO.Directory.Exists(WorkspaceDir))
{
try {
// Clear read-only attributes
SetFileAttributes(new System.IO.DirectoryInfo(WorkspaceDir),
System.IO.FileAttributes.Normal);
// Delete
System.IO.Directory.Delete(WorkspaceDir, true);
}
catch (Exception e) {
System.Console.Error.WriteLine(
$"error: Unable to delete workspace: {e.ToString()}");
}
}
}
~ProjectRunner() {
Dispose(false);
}
public virtual void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}