| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using DirectSight.Common; |
| | | 6 | | using DirectSight.Logging; |
| | | 7 | | using DirectSight.Parser; |
| | | 8 | | using DirectSight.Parser.Analysis; |
| | | 9 | | using DirectSight.Parser.FileReading; |
| | | 10 | | |
| | | 11 | | namespace DirectSight.Reporting; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Converts a coverage report generated by OpenCoverand other tools into a readable report. |
| | | 15 | | /// </summary> |
| | | 16 | | internal class ReportGenerator |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// The file reader to use. |
| | | 20 | | /// </summary> |
| | | 21 | | private readonly IFileReader fileReader; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// The parser to use. |
| | | 25 | | /// </summary> |
| | | 26 | | private readonly ParserResult parserResult; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// The renderers. |
| | | 30 | | /// </summary> |
| | | 31 | | private readonly IEnumerable<IReportBuilder> renderers; |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes a new instance of the <see cref="ReportGenerator" /> class. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="fileReader">The file reader.</param> |
| | | 37 | | /// <param name="parserResult">The parser result to use.</param> |
| | | 38 | | /// <param name="renderers">The renderers.</param> |
| | 2 | 39 | | internal ReportGenerator(IFileReader fileReader, ParserResult parserResult, IEnumerable<IReportBuilder> renderers) |
| | 2 | 40 | | { |
| | 2 | 41 | | this.fileReader = fileReader ?? throw new ArgumentNullException(nameof(fileReader)); |
| | 2 | 42 | | this.parserResult = parserResult ?? throw new ArgumentNullException(nameof(parserResult)); |
| | 2 | 43 | | this.renderers = renderers ?? throw new ArgumentNullException(nameof(renderers)); |
| | 2 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Starts the generation of the report. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="executionTime">The execution time.</param> |
| | | 50 | | internal void CreateReport(DateTime executionTime) |
| | 2 | 51 | | { |
| | 4 | 52 | | int numberOfClasses = this.parserResult.Assemblies.SafeSum(a => a.Classes.Count()); |
| | | 53 | | |
| | 2 | 54 | | ConsoleLogger.Debug("Analyzing {0} classes", numberOfClasses); |
| | | 55 | | |
| | 2 | 56 | | int counter = 0; |
| | | 57 | | |
| | 10 | 58 | | foreach (var assembly in this.parserResult.Assemblies) |
| | 2 | 59 | | { |
| | 78 | 60 | | foreach (var @class in assembly.Classes) |
| | 36 | 61 | | { |
| | 36 | 62 | | counter++; |
| | | 63 | | |
| | 36 | 64 | | ConsoleLogger.Debug( |
| | 36 | 65 | | "Creating report {0}/{1} (Assembly: {2}, Class: {3})", |
| | 36 | 66 | | counter, |
| | 36 | 67 | | numberOfClasses, |
| | 36 | 68 | | @class.Assembly.ShortName, |
| | 36 | 69 | | @class.Name); |
| | | 70 | | |
| | 72 | 71 | | var fileAnalyses = @class.Files.Select(f => f.AnalyzeFile(this.fileReader)).ToArray(); |
| | | 72 | | |
| | 36 | 73 | | Parallel.ForEach( |
| | 36 | 74 | | this.renderers, |
| | 36 | 75 | | renderer => |
| | 72 | 76 | | { |
| | 36 | 77 | | try |
| | 72 | 78 | | { |
| | 72 | 79 | | renderer.CreateClassReport(@class, fileAnalyses); |
| | 72 | 80 | | } |
| | 0 | 81 | | catch (Exception ex) |
| | 0 | 82 | | { |
| | 0 | 83 | | ConsoleLogger.Error( |
| | 0 | 84 | | "Error during rendering report for class '{0}' (Report type: '{1}'): {2}", |
| | 0 | 85 | | @class.Name, |
| | 0 | 86 | | renderer.ReportType, |
| | 0 | 87 | | ex.GetExceptionMessageForDisplay()); |
| | 0 | 88 | | } |
| | 108 | 89 | | }); |
| | 36 | 90 | | } |
| | 2 | 91 | | } |
| | | 92 | | |
| | 2 | 93 | | ConsoleLogger.Debug("Creating summary"); |
| | 2 | 94 | | var summaryResult = new SummaryResult(this.parserResult); |
| | | 95 | | |
| | 14 | 96 | | foreach (var renderer in this.renderers) |
| | 4 | 97 | | { |
| | | 98 | | try |
| | 4 | 99 | | { |
| | 4 | 100 | | renderer.CreateSummaryReport(summaryResult); |
| | 4 | 101 | | } |
| | 0 | 102 | | catch (Exception ex) |
| | 0 | 103 | | { |
| | 0 | 104 | | ConsoleLogger.Error( |
| | 0 | 105 | | "Error during rendering summary report (Report type: '{0}'): {1}", |
| | 0 | 106 | | renderer.ReportType, |
| | 0 | 107 | | ex.GetExceptionMessageForDisplay()); |
| | | 108 | | |
| | 0 | 109 | | if (ConsoleLogger.ShowDebugMessages == true) |
| | 0 | 110 | | { |
| | 0 | 111 | | ConsoleLogger.Error(ex.StackTrace); |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | 4 | 114 | | } |
| | 2 | 115 | | } |
| | | 116 | | } |