/
TaskTreeScriptHost.cs
127 lines (112 loc) · 4.21 KB
/
TaskTreeScriptHost.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
// 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 System.Linq;
using System.Threading.Tasks;
using Cake.Core;
using Cake.Core.Graph;
using Cake.Core.Scripting;
namespace Cake.Scripting
{
/// <summary>
/// The script host used for showing task descriptions.
/// </summary>
public sealed class TaskTreeScriptHost : ScriptHost
{
private const int _maxDepth = 0;
private const string _cross = "├─";
private const string _corner = "└─";
private const string _vertical = "│ ";
private readonly IConsole _console;
/// <summary>
/// Initializes a new instance of the <see cref="TaskTreeScriptHost"/> class.
/// </summary>
/// <param name="engine">The engine.</param>
/// <param name="context">The context.</param>
/// <param name="console">The console.</param>
public TaskTreeScriptHost(ICakeEngine engine, ICakeContext context, IConsole console)
: base(engine, context)
{
if (console == null)
{
throw new ArgumentNullException(nameof(console));
}
_console = console;
}
/// <summary>
/// Runs the specified target.
/// </summary>
/// <param name="target">The target to run.</param>
/// <returns>The resulting report.</returns>
public override Task<CakeReport> RunTargetAsync(string target)
{
var topLevelTasks = GetTopLevelTasks();
_console.WriteLine();
foreach (ICakeTaskInfo task in topLevelTasks)
{
PrintTask(task, string.Empty, false, 0);
_console.WriteLine();
}
return System.Threading.Tasks.Task.FromResult<CakeReport>(null);
}
private List<ICakeTaskInfo> GetTopLevelTasks()
{
// Display "Default" first, then alphabetical
var graph = CakeGraphBuilder.Build(Tasks);
return Tasks.Where(task => !graph.Edges.Any(
edge => edge.Start.Equals(task.Name, StringComparison.OrdinalIgnoreCase)))
.OrderByDescending(task => task.Name.Equals("Default", StringComparison.OrdinalIgnoreCase))
.ThenBy(task => task.Name, StringComparer.OrdinalIgnoreCase)
.ToList();
}
private void PrintTask(ICakeTaskInfo task, string indent, bool isLast, int depth)
{
// Builds ASCII graph
_console.Write(indent);
if (isLast)
{
_console.Write(_corner);
indent += " ";
}
else if (depth > 0)
{
_console.Write(_cross);
indent += _vertical;
}
PrintName(task, depth);
if ((_maxDepth > 0) && (depth >= _maxDepth))
{
return;
}
for (var i = 0; i < task.Dependencies.Count; i++)
{
// First() is safe as CakeGraphBuilder has already validated graph is valid
var childTask = Tasks
.Where(x => x.Name.Equals(task.Dependencies[i].Name, StringComparison.OrdinalIgnoreCase))
.First();
PrintTask(childTask, indent, i == (task.Dependencies.Count - 1), depth + 1);
}
}
private void PrintName(ICakeTaskInfo task, int depth)
{
var originalColor = _console.ForegroundColor;
if (depth == 0)
{
_console.ForegroundColor = ConsoleColor.Cyan;
}
else if (task is CakeTask cakeTask &&
(cakeTask.Actions.Any() || cakeTask.DelayedActions.Any()))
{
_console.ForegroundColor = ConsoleColor.Green;
}
else
{
_console.ForegroundColor = ConsoleColor.Gray;
}
_console.WriteLine(task.Name);
_console.ForegroundColor = originalColor;
}
}
}