forked from cake-contrib/Cake.BuildSystems.Module
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzurePipelinesReportPrinter.cs
More file actions
117 lines (104 loc) · 3.87 KB
/
Copy pathAzurePipelinesReportPrinter.cs
File metadata and controls
117 lines (104 loc) · 3.87 KB
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
using System;
using System.IO;
using System.Linq;
using System.Text;
using Cake.Common.Build;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Module.Shared;
using JetBrains.Annotations;
namespace Cake.AzurePipelines.Module
{
/// <summary>
/// The TF Build/Azure Pipelines report printer.
/// </summary>
[UsedImplicitly]
public class AzurePipelinesReportPrinter : CakeReportPrinterBase
{
/// <summary>
/// Initializes a new instance of the <see cref="AzurePipelinesReportPrinter"/> class.
/// </summary>
/// <param name="console">The console.</param>
/// <param name="context">The context.</param>
public AzurePipelinesReportPrinter(IConsole console, ICakeContext context)
: base(console, context)
{
}
/// <summary>
/// Writes the specified report to a target.
/// </summary>
/// <param name="report">The report to write.</param>
public override void Write(CakeReport report)
{
if (report == null)
{
throw new ArgumentNullException(nameof(report));
}
try
{
if (_context.AzurePipelines().IsRunningOnAzurePipelines)
{
WriteToMarkdown(report);
}
WriteToConsole(report);
}
finally
{
_console.ResetColor();
}
}
private void WriteToMarkdown(CakeReport report)
{
var includeSkippedReasonColumn = report.Any(r => !string.IsNullOrEmpty(r.SkippedMessage));
var maxTaskNameLength = 29;
foreach (var item in report)
{
if (item.TaskName.Length > maxTaskNameLength)
{
maxTaskNameLength = item.TaskName.Length;
}
}
maxTaskNameLength++;
string lineFormat = "|{0,-" + maxTaskNameLength + "}|{1,20}|";
var sb = new StringBuilder();
sb.AppendLine("");
if (includeSkippedReasonColumn)
{
sb.AppendLine("|Task|Duration|Status|Skip Reason|");
sb.AppendLine("|:---|-------:|:-----|:----------|");
}
else
{
sb.AppendLine("|Task|Duration|Status|");
sb.AppendLine("|:---|-------:|:-----|");
}
var targetName = string.Empty;
foreach (var item in report)
{
if (ShouldWriteTask(item))
{
if (includeSkippedReasonColumn)
{
sb.AppendLine(string.Format(lineFormat, item.TaskName, FormatDuration(item), item.ExecutionStatus.ToReportStatus(), item.SkippedMessage));
}
else
{
sb.AppendLine(string.Format(lineFormat, item.TaskName, FormatDuration(item), item.ExecutionStatus.ToReportStatus()));
}
}
targetName = item.TaskName; // Use the last task name it is the target name
}
sb.AppendLine(string.Empty);
var b = _context.BuildSystem().AzurePipelines;
FilePath agentWorkPath = b.Environment.Build.ArtifactStagingDirectory + "/tasksummary.md";
var absFilePath = agentWorkPath.MakeAbsolute(_context.Environment);
var file = _context.FileSystem.GetFile(absFilePath);
using (var writer = new StreamWriter(file.OpenWrite()))
{
writer.Write(sb.ToString());
}
_console.WriteLine($"##vso[task.addattachment type=Distributedtask.Core.Summary;name=Cake {targetName} Build Summary;]{absFilePath.MakeAbsolute(_context.Environment).FullPath}");
}
}
}