|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text; |
| 4 | +using Cake.Common.Build; |
| 5 | +using Cake.Core; |
| 6 | +using Cake.Core.Diagnostics; |
| 7 | +using Cake.Module.Shared; |
| 8 | +using JetBrains.Annotations; |
| 9 | + |
| 10 | +namespace Cake.GitHubActions.Module |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// The GitHub Actions report printer. |
| 14 | + /// </summary> |
| 15 | + [UsedImplicitly] |
| 16 | + public class GitHubActionsReportPrinter : CakeReportPrinterBase |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Initializes a new instance of the <see cref="GitHubActionsReportPrinter"/> class. |
| 20 | + /// </summary> |
| 21 | + /// <param name="console">The console.</param> |
| 22 | + /// <param name="context">The context.</param> |
| 23 | + public GitHubActionsReportPrinter(IConsole console, ICakeContext context) |
| 24 | + : base(console, context) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /// <inheritdoc /> |
| 29 | + public override void Write(CakeReport report) |
| 30 | + { |
| 31 | + if (report == null) |
| 32 | + { |
| 33 | + throw new ArgumentNullException(nameof(report)); |
| 34 | + } |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + if (_context.GitHubActions().IsRunningOnGitHubActions) |
| 39 | + { |
| 40 | + WriteToMarkdown(report); |
| 41 | + } |
| 42 | + |
| 43 | + WriteToConsole(report); |
| 44 | + } |
| 45 | + finally |
| 46 | + { |
| 47 | + _console.ResetColor(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /// <inheritdoc /> |
| 52 | + public override void WriteLifeCycleStep(string name, Verbosity verbosity) |
| 53 | + { |
| 54 | + // Intentionally left blank |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + public override void WriteSkippedStep(string name, Verbosity verbosity) |
| 59 | + { |
| 60 | + // Intentionally left blank |
| 61 | + } |
| 62 | + |
| 63 | + /// <inheritdoc /> |
| 64 | + public override void WriteStep(string name, Verbosity verbosity) |
| 65 | + { |
| 66 | + // Intentionally left blank |
| 67 | + } |
| 68 | + |
| 69 | + private void WriteToMarkdown(CakeReport report) |
| 70 | + { |
| 71 | + var includeSkippedReasonColumn = report.Any(r => !string.IsNullOrEmpty(r.SkippedMessage)); |
| 72 | + |
| 73 | + var sb = new StringBuilder(); |
| 74 | + sb.AppendLine(string.Empty); |
| 75 | + |
| 76 | + if (includeSkippedReasonColumn) |
| 77 | + { |
| 78 | + sb.AppendLine("|Task|Duration|Status|Skip Reason|"); |
| 79 | + sb.AppendLine("|----|--------|------|-----------|"); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + sb.AppendLine("|Task|Duration|Status|"); |
| 84 | + sb.AppendLine("|----|--------|------|"); |
| 85 | + } |
| 86 | + |
| 87 | + foreach (var item in report) |
| 88 | + { |
| 89 | + if (ShouldWriteTask(item)) |
| 90 | + { |
| 91 | + if (includeSkippedReasonColumn) |
| 92 | + { |
| 93 | + sb.AppendLine(string.Format("|{0}|{1}|{2}|{3}|", item.TaskName, FormatDuration(item), item.ExecutionStatus.ToReportStatus(), item.SkippedMessage)); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + sb.AppendLine(string.Format("|{0}|{1}|{2}|", item.TaskName, FormatDuration(item), item.ExecutionStatus.ToReportStatus())); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (includeSkippedReasonColumn) |
| 103 | + { |
| 104 | + sb.AppendLine("|||||"); |
| 105 | + sb.AppendLine(string.Format("|**_{0}_**|**_{1}_**|||", "Total:", GetTotalTime(report))); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + sb.AppendLine("||||"); |
| 110 | + sb.AppendLine(string.Format("|**_{0}_**|**_{1}_**||", "Total:", GetTotalTime(report))); |
| 111 | + } |
| 112 | + |
| 113 | + _context.GitHubActions().Commands.SetStepSummary(sb.ToString()); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments