| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using DotNetApiDiff.Interfaces; |
| | | 3 | | using DotNetApiDiff.Models; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace DotNetApiDiff.Reporting; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Implementation of the report generator that supports multiple output formats |
| | | 10 | | /// </summary> |
| | | 11 | | public class ReportGenerator : IReportGenerator |
| | | 12 | | { |
| | | 13 | | private readonly ILogger<ReportGenerator> _logger; |
| | | 14 | | private readonly Dictionary<ReportFormat, IReportFormatter> _formatters; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="ReportGenerator"/> class. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="logger">The logger.</param> |
| | | 20 | | /// <param name="formatters">Optional dictionary of formatters to use. If null, default formatters will be created.< |
| | 6 | 21 | | public ReportGenerator(ILogger<ReportGenerator> logger, Dictionary<ReportFormat, IReportFormatter>? formatters = nul |
| | 6 | 22 | | { |
| | 6 | 23 | | _logger = logger; |
| | | 24 | | |
| | 6 | 25 | | if (formatters != null) |
| | 4 | 26 | | { |
| | 4 | 27 | | _formatters = formatters; |
| | 4 | 28 | | } |
| | | 29 | | else |
| | 2 | 30 | | { |
| | 2 | 31 | | _formatters = new Dictionary<ReportFormat, IReportFormatter> |
| | 2 | 32 | | { |
| | 2 | 33 | | { ReportFormat.Console, new ConsoleFormatter() }, |
| | 2 | 34 | | { ReportFormat.Json, new JsonFormatter() }, |
| | 2 | 35 | | { ReportFormat.Html, new HtmlFormatterScriban() }, |
| | 2 | 36 | | { ReportFormat.Markdown, new MarkdownFormatter() } |
| | 2 | 37 | | |
| | 2 | 38 | | // Other formatters can be added in subsequent tasks |
| | 2 | 39 | | // { ReportFormat.Xml, new XmlFormatter() } |
| | 2 | 40 | | }; |
| | 2 | 41 | | } |
| | 6 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Generates a report from the comparison result |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="result">The comparison result to generate a report from</param> |
| | | 48 | | /// <param name="format">The desired output format</param> |
| | | 49 | | /// <returns>Generated report as a string</returns> |
| | | 50 | | public string GenerateReport(ComparisonResult result, ReportFormat format) |
| | 3 | 51 | | { |
| | 3 | 52 | | _logger.LogInformation("Generating report in {Format} format", format); |
| | | 53 | | |
| | 3 | 54 | | if (_formatters.TryGetValue(format, out var formatter)) |
| | 2 | 55 | | { |
| | 2 | 56 | | return formatter.Format(result); |
| | | 57 | | } |
| | | 58 | | |
| | 1 | 59 | | _logger.LogWarning("Requested format {Format} is not supported, falling back to Console format", format); |
| | 1 | 60 | | return _formatters[ReportFormat.Console].Format(result); |
| | 3 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Saves a report to the specified file path |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="result">The comparison result to generate a report from</param> |
| | | 67 | | /// <param name="format">The desired output format</param> |
| | | 68 | | /// <param name="filePath">Path where the report should be saved</param> |
| | | 69 | | public async Task SaveReportAsync(ComparisonResult result, ReportFormat format, string filePath) |
| | 1 | 70 | | { |
| | 1 | 71 | | _logger.LogInformation("Saving {Format} report to {FilePath}", format, filePath); |
| | | 72 | | |
| | 1 | 73 | | var report = GenerateReport(result, format); |
| | | 74 | | |
| | | 75 | | try |
| | 1 | 76 | | { |
| | | 77 | | // Ensure directory exists |
| | 1 | 78 | | var directory = Path.GetDirectoryName(filePath); |
| | 1 | 79 | | if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory)) |
| | 0 | 80 | | { |
| | 0 | 81 | | Directory.CreateDirectory(directory); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 1 | 84 | | await File.WriteAllTextAsync(filePath, report); |
| | 1 | 85 | | _logger.LogInformation("Report saved successfully to {FilePath}", filePath); |
| | 1 | 86 | | } |
| | 0 | 87 | | catch (Exception ex) |
| | 0 | 88 | | { |
| | 0 | 89 | | _logger.LogError(ex, "Failed to save report to {FilePath}", filePath); |
| | 0 | 90 | | throw; |
| | | 91 | | } |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// Gets the supported report formats |
| | | 96 | | /// </summary> |
| | | 97 | | /// <returns>Collection of supported report formats</returns> |
| | | 98 | | public IEnumerable<ReportFormat> GetSupportedFormats() |
| | 1 | 99 | | { |
| | 1 | 100 | | return _formatters.Keys; |
| | 1 | 101 | | } |
| | | 102 | | } |