-
-
Notifications
You must be signed in to change notification settings - Fork 729
/
FrostingEngine.cs
179 lines (155 loc) · 6.26 KB
/
FrostingEngine.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.Scripting;
namespace Cake.Frosting.Internal
{
internal interface IFrostingEngine
{
ExecutionSettings Settings { get; }
CakeReport Run(string target);
}
internal abstract class FrostingEngine<THost> : IFrostingEngine
where THost : IScriptHost
{
private readonly List<IFrostingTask> _tasks;
private readonly IFrostingContext _context;
private readonly ICakeLog _log;
private readonly IFrostingSetup _setup;
private readonly IFrostingTeardown _teardown;
private readonly IFrostingTaskSetup _taskSetup;
private readonly IFrostingTaskTeardown _taskTeardown;
private readonly THost _host;
private readonly ICakeEngine _engine;
public ExecutionSettings Settings => _host.Settings;
protected FrostingEngine(
THost host,
ICakeEngine engine, IFrostingContext context, ICakeLog log,
IEnumerable<IFrostingTask> tasks,
IFrostingSetup setup = null,
IFrostingTeardown teardown = null,
IFrostingTaskSetup taskSetup = null,
IFrostingTaskTeardown taskTeardown = null)
{
_host = host;
_engine = engine;
_context = context;
_log = log;
_setup = setup;
_teardown = teardown;
_taskSetup = taskSetup;
_taskTeardown = taskTeardown;
_tasks = new List<IFrostingTask>(tasks ?? Array.Empty<IFrostingTask>());
}
public CakeReport Run(string target)
{
ConfigureTasks();
ConfigureLifetime();
ConfigureTaskLifetime();
return _host.RunTarget(target);
}
private void ConfigureTaskLifetime()
{
if (_taskSetup != null)
{
_log.Debug("Registering task setup: {0}", _taskSetup.GetType().Name);
_engine.RegisterTaskSetupAction(info => _taskSetup.Setup(_context, info));
}
if (_taskTeardown != null)
{
_log.Debug("Registering task setup: {0}", _taskTeardown.GetType().Name);
_engine.RegisterTaskTeardownAction(info => _taskTeardown.Teardown(_context, info));
}
}
private void ConfigureLifetime()
{
if (_setup != null)
{
_log.Debug("Registering setup: {0}", _setup.GetType().Name);
_engine.RegisterSetupAction(info => _setup.Setup(_context));
}
if (_teardown != null)
{
_log.Debug("Registering teardown: {0}", _teardown.GetType().Name);
_engine.RegisterTeardownAction(info => _teardown.Teardown(_context, info));
}
}
private void ConfigureTasks()
{
if (_tasks == null)
{
return;
}
foreach (var task in _tasks)
{
var name = task.GetTaskName();
_log.Debug("Registering task: {0}", name);
// Get the task's context type.
if (!task.HasCompatibleContext(_context))
{
const string format = "Task cannot be used since the context isn't convertible to {0}.";
_log.Warning(format, task.GetContextType().FullName);
}
else
{
// Register task with the Cake engine.
var cakeTask = _engine.RegisterTask(name);
var description = task.GetTaskDescription();
if (!string.IsNullOrWhiteSpace(description))
{
cakeTask.Description(description);
}
// Is the run method overridden?
if (task.IsRunOverridden(_context))
{
cakeTask.Does(task.RunAsync);
}
// Is the criteria method overridden?
if (task.IsShouldRunOverridden(_context))
{
cakeTask.WithCriteria(task.ShouldRun);
}
// Continue on error?
if (task.IsContinueOnError())
{
cakeTask.ContinueOnError();
}
// Is the on error method overridden?
if (task.IsOnErrorOverridden(_context))
{
cakeTask.OnError(exception => task.OnError(exception, _context));
}
// Is the finally method overridden?
if (task.IsFinallyOverridden(_context))
{
cakeTask.Finally(() => task.Finally(_context));
}
// Add dependencies
foreach (var dependency in task.GetDependencies())
{
var dependencyName = dependency.GetTaskName();
if (!typeof(IFrostingTask).IsAssignableFrom(dependency.Task))
{
throw new FrostingException($"The dependency '{dependencyName}' is not a valid task.");
}
cakeTask.IsDependentOn(dependencyName);
}
// Add reverse dependencies
foreach (var dependee in task.GetReverseDependencies())
{
var dependeeName = dependee.GetTaskName();
if (!typeof(IFrostingTask).IsAssignableFrom(dependee.Task))
{
throw new FrostingException($"The reverse dependency '{dependeeName}' is not a valid task.");
}
cakeTask.IsDependeeOf(dependeeName);
}
}
}
}
}
}