|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Globalization; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using System.Text; |
| 7 | +using Palmmedia.ReportGenerator.Core.Common; |
| 8 | +using Palmmedia.ReportGenerator.Core.Logging; |
| 9 | +using Palmmedia.ReportGenerator.Core.Parser.Analysis; |
| 10 | +using Palmmedia.ReportGenerator.Core.Properties; |
| 11 | + |
| 12 | +namespace Palmmedia.ReportGenerator.Core.Reporting.Builders |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Creates summary report in Markdown format (no reports for classes are generated). |
| 16 | + /// </summary> |
| 17 | + public class MarkdownSummaryReportBuilder : IReportBuilder |
| 18 | + { |
| 19 | + /// <summary> |
| 20 | + /// The Logger. |
| 21 | + /// </summary> |
| 22 | + private static readonly ILogger Logger = LoggerFactory.GetLogger(typeof(MarkdownSummaryReportBuilder)); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Gets the report type. |
| 26 | + /// </summary> |
| 27 | + /// <value> |
| 28 | + /// The report type. |
| 29 | + /// </value> |
| 30 | + public string ReportType => "MarkdownSummary"; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets or sets the report context. |
| 34 | + /// </summary> |
| 35 | + /// <value> |
| 36 | + /// The report context. |
| 37 | + /// </value> |
| 38 | + public IReportContext ReportContext { get; set; } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Creates a class report. |
| 42 | + /// </summary> |
| 43 | + /// <param name="class">The class.</param> |
| 44 | + /// <param name="fileAnalyses">The file analyses that correspond to the class.</param> |
| 45 | + public void CreateClassReport(Class @class, IEnumerable<FileAnalysis> fileAnalyses) |
| 46 | + { |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// Creates the summary report. |
| 51 | + /// </summary> |
| 52 | + /// <param name="summaryResult">The summary result.</param> |
| 53 | + public void CreateSummaryReport(SummaryResult summaryResult) |
| 54 | + { |
| 55 | + if (summaryResult == null) |
| 56 | + { |
| 57 | + throw new ArgumentNullException(nameof(summaryResult)); |
| 58 | + } |
| 59 | + |
| 60 | + string targetDirectory = this.ReportContext.ReportConfiguration.TargetDirectory; |
| 61 | + |
| 62 | + if (this.ReportContext.Settings.CreateSubdirectoryForAllReportTypes) |
| 63 | + { |
| 64 | + targetDirectory = Path.Combine(targetDirectory, this.ReportType); |
| 65 | + |
| 66 | + if (!Directory.Exists(targetDirectory)) |
| 67 | + { |
| 68 | + try |
| 69 | + { |
| 70 | + Directory.CreateDirectory(targetDirectory); |
| 71 | + } |
| 72 | + catch (Exception ex) |
| 73 | + { |
| 74 | + Logger.ErrorFormat(Resources.TargetDirectoryCouldNotBeCreated, targetDirectory, ex.GetExceptionMessageForDisplay()); |
| 75 | + return; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + string targetPath = Path.Combine(targetDirectory, "Summary.md"); |
| 81 | + |
| 82 | + Logger.InfoFormat(Resources.WritingReportFile, targetPath); |
| 83 | + |
| 84 | + using (var reportTextWriter = new StreamWriter(new FileStream(targetPath, FileMode.Create), Encoding.UTF8)) |
| 85 | + { |
| 86 | + reportTextWriter.WriteLine("# {0}", ReportResources.Summary); |
| 87 | + reportTextWriter.WriteLine("|||"); |
| 88 | + reportTextWriter.WriteLine("|:---|:---|"); |
| 89 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.GeneratedOn, DateTime.Now.ToShortDateString() + " - " + DateTime.Now.ToLongTimeString()); |
| 90 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Parser, summaryResult.UsedParser); |
| 91 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Assemblies2, summaryResult.Assemblies.Count().ToString(CultureInfo.InvariantCulture)); |
| 92 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Classes, summaryResult.Assemblies.SelectMany(a => a.Classes).Count().ToString(CultureInfo.InvariantCulture)); |
| 93 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Files2, summaryResult.Assemblies.SelectMany(a => a.Classes).SelectMany(a => a.Files).Distinct().Count().ToString(CultureInfo.InvariantCulture)); |
| 94 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.CoveredLines, summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)); |
| 95 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.UncoveredLines, (summaryResult.CoverableLines - summaryResult.CoveredLines).ToString(CultureInfo.InvariantCulture)); |
| 96 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.CoverableLines, summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)); |
| 97 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.TotalLines, summaryResult.TotalLines.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); |
| 98 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Coverage2, summaryResult.CoverageQuota.HasValue ? $"{summaryResult.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture)}% ({summaryResult.CoveredLines.ToString(CultureInfo.InvariantCulture)} {ReportResources.Of} {summaryResult.CoverableLines.ToString(CultureInfo.InvariantCulture)})" : string.Empty); |
| 99 | + |
| 100 | + if (summaryResult.SupportsBranchCoverage) |
| 101 | + { |
| 102 | + if (summaryResult.CoveredBranches.HasValue && summaryResult.TotalBranches.HasValue) |
| 103 | + { |
| 104 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.CoveredBranches2, summaryResult.CoveredBranches.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); |
| 105 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.TotalBranches, summaryResult.TotalBranches.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)); |
| 106 | + |
| 107 | + decimal? branchCoverage = summaryResult.BranchCoverageQuota; |
| 108 | + |
| 109 | + if (branchCoverage.HasValue) |
| 110 | + { |
| 111 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.BranchCoverage2, $"{branchCoverage.Value.ToString(CultureInfo.InvariantCulture)}% ({summaryResult.CoveredBranches.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)} {ReportResources.Of} {summaryResult.TotalBranches.GetValueOrDefault().ToString(CultureInfo.InvariantCulture)})"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + if (this.ReportContext.ReportConfiguration.Tag != null) |
| 117 | + { |
| 118 | + reportTextWriter.WriteLine("| {0} | {1} |", ReportResources.Tag, this.ReportContext.ReportConfiguration.Tag); |
| 119 | + } |
| 120 | + |
| 121 | + reportTextWriter.WriteLine(); |
| 122 | + |
| 123 | + if (summaryResult.Assemblies.Any()) |
| 124 | + { |
| 125 | + var maximumNameLength = summaryResult.Assemblies |
| 126 | + .SelectMany(a => a.Classes).Select(c => c.DisplayName) |
| 127 | + .Union(summaryResult.Assemblies.Select(a => a.Name)) |
| 128 | + .Max(n => n.Length); |
| 129 | + |
| 130 | + reportTextWriter.Write( |
| 131 | + "|**{0}**|**{1}**|**{2}**|**{3}**|**{4}**|**{5}**|", |
| 132 | + ReportResources.Name, |
| 133 | + ReportResources.Covered, |
| 134 | + ReportResources.Uncovered, |
| 135 | + ReportResources.Coverable, |
| 136 | + ReportResources.Total, |
| 137 | + ReportResources.Coverage); |
| 138 | + |
| 139 | + if (summaryResult.SupportsBranchCoverage) |
| 140 | + { |
| 141 | + reportTextWriter.WriteLine( |
| 142 | + "**{0}**|**{1}**|**{2}**|", |
| 143 | + ReportResources.Covered, |
| 144 | + ReportResources.Total, |
| 145 | + ReportResources.BranchCoverage); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + reportTextWriter.WriteLine(string.Empty); |
| 150 | + } |
| 151 | + |
| 152 | + reportTextWriter.Write("|:---|---:|---:|---:|---:|---:|"); |
| 153 | + |
| 154 | + if (summaryResult.SupportsBranchCoverage) |
| 155 | + { |
| 156 | + reportTextWriter.WriteLine("---:|---:|---:|"); |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + reportTextWriter.WriteLine(string.Empty); |
| 161 | + } |
| 162 | + |
| 163 | + foreach (var assembly in summaryResult.Assemblies) |
| 164 | + { |
| 165 | + reportTextWriter.Write("|**{0}**", assembly.Name); |
| 166 | + reportTextWriter.Write("|**{0}**", assembly.CoveredLines); |
| 167 | + reportTextWriter.Write("|**{0}**", assembly.CoverableLines - assembly.CoveredLines); |
| 168 | + reportTextWriter.Write("|**{0}**", assembly.CoverableLines); |
| 169 | + reportTextWriter.Write("|**{0}**", assembly.TotalLines.GetValueOrDefault()); |
| 170 | + reportTextWriter.Write("|**{0}**", assembly.CoverageQuota.HasValue ? assembly.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) + "%" : string.Empty); |
| 171 | + |
| 172 | + if (summaryResult.SupportsBranchCoverage) |
| 173 | + { |
| 174 | + reportTextWriter.Write("|**{0}**", assembly.CoveredBranches); |
| 175 | + reportTextWriter.Write("|**{0}**", assembly.TotalBranches); |
| 176 | + reportTextWriter.Write("|**{0}**", assembly.BranchCoverageQuota.HasValue ? assembly.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) + "%" : string.Empty); |
| 177 | + } |
| 178 | + |
| 179 | + reportTextWriter.WriteLine("|"); |
| 180 | + |
| 181 | + foreach (var @class in assembly.Classes) |
| 182 | + { |
| 183 | + reportTextWriter.Write("|{0}", @class.Name); |
| 184 | + reportTextWriter.Write("|{0}", @class.CoveredLines); |
| 185 | + reportTextWriter.Write("|{0}", @class.CoverableLines - @class.CoveredLines); |
| 186 | + reportTextWriter.Write("|{0}", @class.CoverableLines); |
| 187 | + reportTextWriter.Write("|{0}", @class.TotalLines.GetValueOrDefault()); |
| 188 | + reportTextWriter.Write("|{0}", @class.CoverageQuota.HasValue ? @class.CoverageQuota.Value.ToString(CultureInfo.InvariantCulture) + "%" : string.Empty); |
| 189 | + |
| 190 | + if (summaryResult.SupportsBranchCoverage) |
| 191 | + { |
| 192 | + reportTextWriter.Write("|{0}", @class.CoveredBranches); |
| 193 | + reportTextWriter.Write("|{0}", @class.TotalBranches); |
| 194 | + reportTextWriter.Write("|{0}", @class.BranchCoverageQuota.HasValue ? @class.BranchCoverageQuota.Value.ToString(CultureInfo.InvariantCulture) + "%" : string.Empty); |
| 195 | + } |
| 196 | + |
| 197 | + reportTextWriter.WriteLine("|"); |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + else |
| 202 | + { |
| 203 | + reportTextWriter.WriteLine(ReportResources.NoCoveredAssemblies); |
| 204 | + } |
| 205 | + |
| 206 | + reportTextWriter.Flush(); |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments