| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.IO; |
| | | 4 | | using DirectSight.CodeAnalysis; |
| | | 5 | | using DirectSight.Common; |
| | | 6 | | using DirectSight.Logging; |
| | | 7 | | using DirectSight.Parser; |
| | | 8 | | using DirectSight.Parser.FileReading; |
| | | 9 | | using DirectSight.Reporting; |
| | | 10 | | |
| | | 11 | | namespace DirectSight; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// The report generator implementation. |
| | | 15 | | /// </summary> |
| | | 16 | | public class Generator |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Generates a report using given configuration. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="reportConfiguration">The report configuration.</param> |
| | | 22 | | /// <returns><c>true</c> if report was generated successfully; otherwise <c>false</c>.</returns> |
| | | 23 | | public static bool GenerateReport(ReportConfiguration reportConfiguration) |
| | 2 | 24 | | { |
| | | 25 | | try |
| | 2 | 26 | | { |
| | 2 | 27 | | ArgumentNullException.ThrowIfNull(reportConfiguration); |
| | | 28 | | |
| | | 29 | | // Set log level before validation is performed |
| | 2 | 30 | | if (reportConfiguration.DebugMode == true) |
| | 0 | 31 | | { |
| | 0 | 32 | | ConsoleLogger.ShowDebugMessages = true; |
| | 0 | 33 | | } |
| | | 34 | | |
| | 2 | 35 | | ConsoleLogger.Debug($"Executable: {AppContext.BaseDirectory}"); |
| | 2 | 36 | | ConsoleLogger.Debug($"Working Directory: {Directory.GetCurrentDirectory()}"); |
| | | 37 | | |
| | 2 | 38 | | if (!ReportConfigurationValidator.Validate(reportConfiguration)) |
| | 0 | 39 | | { |
| | 0 | 40 | | return false; |
| | | 41 | | } |
| | | 42 | | |
| | 2 | 43 | | var stopWatch = Stopwatch.StartNew(); |
| | | 44 | | |
| | 2 | 45 | | var parserResult = new CoverageReportParser() |
| | 2 | 46 | | .ParseFiles(reportConfiguration.ReportFiles); |
| | | 47 | | |
| | 2 | 48 | | ConsoleLogger.Debug("Report parsing took {0:f1} seconds", stopWatch.ElapsedMilliseconds / 1000d); |
| | | 49 | | |
| | 2 | 50 | | GenerateReport( |
| | 2 | 51 | | reportConfiguration, |
| | 2 | 52 | | parserResult); |
| | | 53 | | |
| | 2 | 54 | | stopWatch.Stop(); |
| | 2 | 55 | | ConsoleLogger.Info("Report generation took {0:f1} seconds", stopWatch.ElapsedMilliseconds / 1000d); |
| | | 56 | | |
| | 2 | 57 | | return true; |
| | | 58 | | } |
| | 0 | 59 | | catch (Exception ex) |
| | 0 | 60 | | { |
| | 0 | 61 | | ConsoleLogger.Error(ex.GetExceptionMessageForDisplay()); |
| | 0 | 62 | | ConsoleLogger.Error(ex.StackTrace); |
| | | 63 | | |
| | 0 | 64 | | return false; |
| | | 65 | | } |
| | 2 | 66 | | } |
| | | 67 | | |
| | | 68 | | /// <summary> |
| | | 69 | | /// Executes the report generation. |
| | | 70 | | /// </summary> |
| | | 71 | | /// <param name="reportConfiguration">The report configuration.</param> |
| | | 72 | | /// <param name="parserResult">The parser result generated by <see cref="CoverageReportParser"/>.</param> |
| | | 73 | | public static void GenerateReport( |
| | | 74 | | ReportConfiguration reportConfiguration, |
| | | 75 | | ParserResult parserResult) |
| | 2 | 76 | | { |
| | 2 | 77 | | ArgumentNullException.ThrowIfNull(reportConfiguration); |
| | 2 | 78 | | ArgumentNullException.ThrowIfNull(parserResult); |
| | | 79 | | |
| | 2 | 80 | | MathExtensions.MaximumDecimalPlaces = 1; |
| | | 81 | | |
| | 2 | 82 | | var reportContext = new ReportContext(reportConfiguration) |
| | 2 | 83 | | { |
| | 2 | 84 | | RiskHotspotAnalysisResult = new RiskHotspotsAnalyzer() |
| | 2 | 85 | | .PerformRiskHotspotAnalysis(parserResult.Assemblies) |
| | 2 | 86 | | }; |
| | | 87 | | |
| | 2 | 88 | | DateTime executionTime = DateTime.Now; |
| | | 89 | | |
| | 2 | 90 | | var fileReader = new LocalFileReader(); |
| | | 91 | | |
| | 2 | 92 | | new ReportGenerator( |
| | 2 | 93 | | fileReader, |
| | 2 | 94 | | parserResult, |
| | 2 | 95 | | ReportBuilderFactory.GetReportBuilders(reportContext)) |
| | 2 | 96 | | .CreateReport(executionTime); |
| | 2 | 97 | | } |
| | | 98 | | } |