| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using DotNetApiDiff.Interfaces; |
| | | 3 | | using DotNetApiDiff.Models; |
| | | 4 | | using DotNetApiDiff.Models.Configuration; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Spectre.Console; |
| | | 8 | | using Spectre.Console.Cli; |
| | | 9 | | using System.ComponentModel; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Reflection; |
| | | 12 | | |
| | | 13 | | namespace DotNetApiDiff.Commands; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Settings for the compare command |
| | | 17 | | /// </summary> |
| | | 18 | | [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicPro |
| | | 19 | | public class CompareCommandSettings : CommandSettings |
| | | 20 | | { |
| | | 21 | | [CommandArgument(0, "<sourceAssembly>")] |
| | | 22 | | [Description("Path to the source/baseline assembly")] |
| | | 23 | | public string? SourceAssemblyPath { get; set; } |
| | | 24 | | |
| | | 25 | | [CommandArgument(1, "<targetAssembly>")] |
| | | 26 | | [Description("Path to the target/current assembly")] |
| | | 27 | | public string? TargetAssemblyPath { get; set; } |
| | | 28 | | |
| | | 29 | | [CommandOption("-c|--config <configFile>")] |
| | | 30 | | [Description("Path to configuration file")] |
| | | 31 | | public string? ConfigFile { get; set; } |
| | | 32 | | |
| | | 33 | | [CommandOption("-o|--output <format>")] |
| | | 34 | | [Description("Output format (console, json, html, markdown)")] |
| | | 35 | | public string? OutputFormat { get; set; } |
| | | 36 | | |
| | | 37 | | [CommandOption("-p|--output-file <path>")] |
| | | 38 | | [Description("Output file path (required for json, html, markdown formats)")] |
| | | 39 | | public string? OutputFile { get; set; } |
| | | 40 | | |
| | | 41 | | [CommandOption("-f|--filter <namespace>")] |
| | | 42 | | [Description("Filter to specific namespaces (can be specified multiple times)")] |
| | | 43 | | public string[]? NamespaceFilters { get; set; } |
| | | 44 | | |
| | | 45 | | [CommandOption("-e|--exclude <pattern>")] |
| | | 46 | | [Description("Exclude types matching pattern (can be specified multiple times)")] |
| | | 47 | | public string[]? ExcludePatterns { get; set; } |
| | | 48 | | |
| | | 49 | | [CommandOption("-t|--type <pattern>")] |
| | | 50 | | [Description("Filter to specific type patterns (can be specified multiple times)")] |
| | | 51 | | public string[]? TypePatterns { get; set; } |
| | | 52 | | |
| | | 53 | | [CommandOption("--include-internals")] |
| | | 54 | | [Description("Include internal types in the comparison")] |
| | | 55 | | [DefaultValue(false)] |
| | | 56 | | public bool IncludeInternals { get; set; } |
| | | 57 | | |
| | | 58 | | [CommandOption("--include-compiler-generated")] |
| | | 59 | | [Description("Include compiler-generated types in the comparison")] |
| | | 60 | | [DefaultValue(false)] |
| | | 61 | | public bool IncludeCompilerGenerated { get; set; } |
| | | 62 | | |
| | | 63 | | [CommandOption("--no-color")] |
| | | 64 | | [Description("Disable colored output")] |
| | | 65 | | [DefaultValue(false)] |
| | | 66 | | public bool NoColor { get; set; } |
| | | 67 | | |
| | | 68 | | [CommandOption("-v|--verbose")] |
| | | 69 | | [Description("Enable verbose output")] |
| | | 70 | | [DefaultValue(false)] |
| | | 71 | | public bool Verbose { get; set; } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Command to compare two assemblies |
| | | 76 | | /// </summary> |
| | | 77 | | public class CompareCommand : Command<CompareCommandSettings> |
| | | 78 | | { |
| | | 79 | | private readonly IServiceProvider _serviceProvider; |
| | | 80 | | private readonly ILogger<CompareCommand> _logger; |
| | | 81 | | private readonly IExitCodeManager _exitCodeManager; |
| | | 82 | | private readonly IGlobalExceptionHandler _exceptionHandler; |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Initializes a new instance of the <see cref="CompareCommand"/> class. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="serviceProvider">The service provider.</param> |
| | | 88 | | /// <param name="logger">The logger.</param> |
| | | 89 | | /// <param name="exitCodeManager">The exit code manager.</param> |
| | | 90 | | /// <param name="exceptionHandler">The global exception handler.</param> |
| | 0 | 91 | | public CompareCommand( |
| | 0 | 92 | | IServiceProvider serviceProvider, |
| | 0 | 93 | | ILogger<CompareCommand> logger, |
| | 0 | 94 | | IExitCodeManager exitCodeManager, |
| | 0 | 95 | | IGlobalExceptionHandler exceptionHandler) |
| | 0 | 96 | | { |
| | 0 | 97 | | _serviceProvider = serviceProvider; |
| | 0 | 98 | | _logger = logger; |
| | 0 | 99 | | _exitCodeManager = exitCodeManager; |
| | 0 | 100 | | _exceptionHandler = exceptionHandler; |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Validates the command settings |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="context">The command context</param> |
| | | 107 | | /// <param name="settings">The command settings</param> |
| | | 108 | | /// <returns>ValidationResult indicating success or failure</returns> |
| | | 109 | | public override ValidationResult Validate([NotNull] CommandContext context, [NotNull] CompareCommandSettings setting |
| | 0 | 110 | | { |
| | | 111 | | // Validate source assembly path |
| | 0 | 112 | | if (string.IsNullOrEmpty(settings.SourceAssemblyPath)) |
| | 0 | 113 | | { |
| | 0 | 114 | | return ValidationResult.Error("Source assembly path is required"); |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | if (!File.Exists(settings.SourceAssemblyPath)) |
| | 0 | 118 | | { |
| | 0 | 119 | | return ValidationResult.Error($"Source assembly file not found: {settings.SourceAssemblyPath}"); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | // Validate target assembly path |
| | 0 | 123 | | if (string.IsNullOrEmpty(settings.TargetAssemblyPath)) |
| | 0 | 124 | | { |
| | 0 | 125 | | return ValidationResult.Error("Target assembly path is required"); |
| | | 126 | | } |
| | | 127 | | |
| | 0 | 128 | | if (!File.Exists(settings.TargetAssemblyPath)) |
| | 0 | 129 | | { |
| | 0 | 130 | | return ValidationResult.Error($"Target assembly file not found: {settings.TargetAssemblyPath}"); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // Validate config file if specified |
| | 0 | 134 | | if (!string.IsNullOrEmpty(settings.ConfigFile) && !File.Exists(settings.ConfigFile)) |
| | 0 | 135 | | { |
| | 0 | 136 | | return ValidationResult.Error($"Configuration file not found: {settings.ConfigFile}"); |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | // Validate output format if provided |
| | 0 | 140 | | if (!string.IsNullOrEmpty(settings.OutputFormat)) |
| | 0 | 141 | | { |
| | 0 | 142 | | string format = settings.OutputFormat.ToLowerInvariant(); |
| | 0 | 143 | | if (format != "console" && format != "json" && format != "html" && format != "markdown") |
| | 0 | 144 | | { |
| | 0 | 145 | | return ValidationResult.Error($"Invalid output format: {settings.OutputFormat}. Valid formats are: conso |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | // Validate output file requirements |
| | 0 | 149 | | if (format == "html") |
| | 0 | 150 | | { |
| | | 151 | | // HTML format requires an output file |
| | 0 | 152 | | if (string.IsNullOrEmpty(settings.OutputFile)) |
| | 0 | 153 | | { |
| | 0 | 154 | | return ValidationResult.Error($"Output file is required for {settings.OutputFormat} format. Use --ou |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | // Validate output directory exists |
| | 0 | 158 | | var outputDir = Path.GetDirectoryName(settings.OutputFile); |
| | 0 | 159 | | if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) |
| | 0 | 160 | | { |
| | 0 | 161 | | return ValidationResult.Error($"Output directory does not exist: {outputDir}"); |
| | | 162 | | } |
| | 0 | 163 | | } |
| | 0 | 164 | | } |
| | 0 | 165 | | else if (!string.IsNullOrEmpty(settings.OutputFile)) |
| | 0 | 166 | | { |
| | | 167 | | // If output file is specified for non-HTML formats, validate the directory exists |
| | 0 | 168 | | var outputDir = Path.GetDirectoryName(settings.OutputFile); |
| | 0 | 169 | | if (!string.IsNullOrEmpty(outputDir) && !Directory.Exists(outputDir)) |
| | 0 | 170 | | { |
| | 0 | 171 | | return ValidationResult.Error($"Output directory does not exist: {outputDir}"); |
| | | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | return ValidationResult.Success(); |
| | 0 | 176 | | } |
| | | 177 | | |
| | | 178 | | /// <summary> |
| | | 179 | | /// Executes the command |
| | | 180 | | /// </summary> |
| | | 181 | | /// <param name="context">The command context</param> |
| | | 182 | | /// <param name="settings">The command settings</param> |
| | | 183 | | /// <returns>Exit code (0 for success, non-zero for failure)</returns> |
| | | 184 | | public override int Execute([NotNull] CommandContext context, [NotNull] CompareCommandSettings settings) |
| | 0 | 185 | | { |
| | | 186 | | try |
| | 0 | 187 | | { |
| | | 188 | | // Create a logging scope for this command execution |
| | 0 | 189 | | using (_logger.BeginScope("Compare command execution")) |
| | 0 | 190 | | { |
| | | 191 | | // Set up logging level based on verbose flag |
| | 0 | 192 | | if (settings.Verbose) |
| | 0 | 193 | | { |
| | 0 | 194 | | _logger.LogInformation("Verbose logging enabled"); |
| | 0 | 195 | | } |
| | | 196 | | |
| | | 197 | | // Configure console output |
| | 0 | 198 | | if (settings.NoColor) |
| | 0 | 199 | | { |
| | 0 | 200 | | _logger.LogDebug("Disabling colored output"); |
| | 0 | 201 | | AnsiConsole.Profile.Capabilities.ColorSystem = ColorSystem.NoColors; |
| | 0 | 202 | | } |
| | | 203 | | |
| | | 204 | | // Load configuration |
| | | 205 | | ComparisonConfiguration config; |
| | 0 | 206 | | if (!string.IsNullOrEmpty(settings.ConfigFile)) |
| | 0 | 207 | | { |
| | 0 | 208 | | using (_logger.BeginScope("Configuration loading")) |
| | 0 | 209 | | { |
| | 0 | 210 | | _logger.LogInformation("Loading configuration from {ConfigFile}", settings.ConfigFile); |
| | | 211 | | |
| | | 212 | | try |
| | 0 | 213 | | { |
| | | 214 | | // Verify the file exists and is accessible |
| | 0 | 215 | | if (!File.Exists(settings.ConfigFile)) |
| | 0 | 216 | | { |
| | 0 | 217 | | throw new FileNotFoundException($"Configuration file not found: {settings.ConfigFile}", |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | // Try to load the configuration |
| | 0 | 221 | | config = ComparisonConfiguration.LoadFromJsonFile(settings.ConfigFile); |
| | 0 | 222 | | _logger.LogInformation("Configuration loaded successfully"); |
| | 0 | 223 | | } |
| | 0 | 224 | | catch (Exception ex) |
| | 0 | 225 | | { |
| | 0 | 226 | | _logger.LogError(ex, "Error loading configuration from {ConfigFile}", settings.ConfigFile); |
| | 0 | 227 | | AnsiConsole.MarkupLine($"[red]Error loading configuration:[/] {ex.Message}"); |
| | | 228 | | |
| | | 229 | | // Use the ExitCodeManager to determine the appropriate exit code for errors |
| | 0 | 230 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | | 231 | | } |
| | 0 | 232 | | } |
| | 0 | 233 | | } |
| | | 234 | | else |
| | 0 | 235 | | { |
| | 0 | 236 | | _logger.LogInformation("Using default configuration"); |
| | 0 | 237 | | config = ComparisonConfiguration.CreateDefault(); |
| | 0 | 238 | | } |
| | | 239 | | |
| | | 240 | | // Apply command-line filters and options |
| | 0 | 241 | | ApplyCommandLineOptions(settings, config); |
| | | 242 | | |
| | | 243 | | // NOW CREATE THE COMMAND-SPECIFIC CONTAINER |
| | 0 | 244 | | _logger.LogInformation("Creating command-specific service container with loaded configuration"); |
| | | 245 | | |
| | 0 | 246 | | var commandServices = new ServiceCollection(); |
| | | 247 | | |
| | | 248 | | // Reuse shared services from root container |
| | 0 | 249 | | var loggerFactory = _serviceProvider.GetRequiredService<ILoggerFactory>(); |
| | 0 | 250 | | commandServices.AddSingleton(loggerFactory); |
| | 0 | 251 | | commandServices.AddLogging(); // This adds ILogger<T> services |
| | | 252 | | |
| | | 253 | | // Add the loaded configuration |
| | 0 | 254 | | commandServices.AddSingleton(config); // Add all business logic services with configurati |
| | 0 | 255 | | commandServices.AddScoped<IAssemblyLoader, AssemblyLoading.AssemblyLoader>(); |
| | 0 | 256 | | commandServices.AddScoped<IApiExtractor, ApiExtraction.ApiExtractor>(); |
| | 0 | 257 | | commandServices.AddScoped<IMemberSignatureBuilder, ApiExtraction.MemberSignatureBuilder>(); |
| | 0 | 258 | | commandServices.AddScoped<ITypeAnalyzer, ApiExtraction.TypeAnalyzer>(); |
| | 0 | 259 | | commandServices.AddScoped<IDifferenceCalculator, ApiExtraction.DifferenceCalculator>(); |
| | 0 | 260 | | commandServices.AddScoped<IReportGenerator, Reporting.ReportGenerator>(); |
| | | 261 | | |
| | | 262 | | // Add configuration-specific services |
| | 0 | 263 | | commandServices.AddScoped<INameMapper>(provider => |
| | 0 | 264 | | { |
| | 0 | 265 | | return new ApiExtraction.NameMapper( |
| | 0 | 266 | | config.Mappings, |
| | 0 | 267 | | loggerFactory.CreateLogger<ApiExtraction.NameMapper>()); |
| | 0 | 268 | | }); |
| | | 269 | | |
| | 0 | 270 | | commandServices.AddScoped<IChangeClassifier>(provider => |
| | 0 | 271 | | new ApiExtraction.ChangeClassifier( |
| | 0 | 272 | | config.BreakingChangeRules, |
| | 0 | 273 | | config.Exclusions, |
| | 0 | 274 | | loggerFactory.CreateLogger<ApiExtraction.ChangeClassifier>())); |
| | | 275 | | |
| | | 276 | | // Add the main comparison service that depends on configured services |
| | 0 | 277 | | commandServices.AddScoped<IApiComparer>(provider => |
| | 0 | 278 | | new ApiExtraction.ApiComparer( |
| | 0 | 279 | | provider.GetRequiredService<IApiExtractor>(), |
| | 0 | 280 | | provider.GetRequiredService<IDifferenceCalculator>(), |
| | 0 | 281 | | provider.GetRequiredService<INameMapper>(), |
| | 0 | 282 | | provider.GetRequiredService<IChangeClassifier>(), |
| | 0 | 283 | | config, |
| | 0 | 284 | | provider.GetRequiredService<ILogger<ApiExtraction.ApiComparer>>())); |
| | | 285 | | |
| | | 286 | | // Execute the command with the configured services |
| | 0 | 287 | | using (var commandProvider = commandServices.BuildServiceProvider()) |
| | 0 | 288 | | { |
| | 0 | 289 | | return ExecuteWithConfiguredServices(settings, config, commandProvider); |
| | | 290 | | } |
| | | 291 | | } |
| | | 292 | | } |
| | 0 | 293 | | catch (Exception ex) |
| | 0 | 294 | | { |
| | | 295 | | // Use our centralized exception handler for any unhandled exceptions |
| | 0 | 296 | | return _exceptionHandler.HandleException(ex, "Compare command execution"); |
| | | 297 | | } |
| | 0 | 298 | | } |
| | | 299 | | |
| | | 300 | | /// <summary> |
| | | 301 | | /// Extracts the member name from a full element name |
| | | 302 | | /// </summary> |
| | | 303 | | /// <param name="elementName">The full element name</param> |
| | | 304 | | /// <returns>The member name</returns> |
| | | 305 | | private static string ExtractMemberName(string elementName) |
| | 0 | 306 | | { |
| | 0 | 307 | | if (string.IsNullOrEmpty(elementName)) |
| | 0 | 308 | | { |
| | 0 | 309 | | return "Unknown"; // Or throw an exception if this is not expected |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | // For full names like "Namespace.Class.Method", extract just "Method" |
| | 0 | 313 | | var lastDotIndex = elementName.LastIndexOf('.'); |
| | 0 | 314 | | return lastDotIndex >= 0 ? elementName.Substring(lastDotIndex + 1) : elementName; |
| | 0 | 315 | | } |
| | | 316 | | |
| | | 317 | | /// <summary> |
| | | 318 | | /// Executes the comparison logic using the configured services |
| | | 319 | | /// </summary> |
| | | 320 | | /// <param name="settings">Command settings</param> |
| | | 321 | | /// <param name="config">Loaded configuration</param> |
| | | 322 | | /// <param name="serviceProvider">Command-specific service provider</param> |
| | | 323 | | /// <returns>Exit code</returns> |
| | | 324 | | private int ExecuteWithConfiguredServices(CompareCommandSettings settings, ComparisonConfiguration config, IServiceP |
| | 0 | 325 | | { |
| | | 326 | | // Load assemblies |
| | | 327 | | Assembly sourceAssembly; |
| | | 328 | | Assembly targetAssembly; |
| | | 329 | | |
| | 0 | 330 | | using (_logger.BeginScope("Assembly loading")) |
| | 0 | 331 | | { |
| | 0 | 332 | | _logger.LogInformation("Loading source assembly: {Path}", settings.SourceAssemblyPath); |
| | 0 | 333 | | _logger.LogInformation("Loading target assembly: {Path}", settings.TargetAssemblyPath); |
| | | 334 | | |
| | 0 | 335 | | var assemblyLoader = serviceProvider.GetRequiredService<IAssemblyLoader>(); |
| | | 336 | | |
| | | 337 | | try |
| | 0 | 338 | | { |
| | 0 | 339 | | sourceAssembly = assemblyLoader.LoadAssembly(settings.SourceAssemblyPath!); |
| | 0 | 340 | | } |
| | 0 | 341 | | catch (Exception ex) |
| | 0 | 342 | | { |
| | 0 | 343 | | _logger.LogError(ex, "Failed to load source assembly: {Path}", settings.SourceAssemblyPath); |
| | 0 | 344 | | AnsiConsole.MarkupLine($"[red]Error loading source assembly:[/] {ex.Message}"); |
| | | 345 | | |
| | 0 | 346 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | try |
| | 0 | 350 | | { |
| | 0 | 351 | | targetAssembly = assemblyLoader.LoadAssembly(settings.TargetAssemblyPath!); |
| | 0 | 352 | | } |
| | 0 | 353 | | catch (Exception ex) |
| | 0 | 354 | | { |
| | 0 | 355 | | _logger.LogError(ex, "Failed to load target assembly: {Path}", settings.TargetAssemblyPath); |
| | 0 | 356 | | AnsiConsole.MarkupLine($"[red]Error loading target assembly:[/] {ex.Message}"); |
| | | 357 | | |
| | 0 | 358 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | | 359 | | } |
| | 0 | 360 | | } |
| | | 361 | | |
| | | 362 | | // Extract API information |
| | 0 | 363 | | using (_logger.BeginScope("API extraction")) |
| | 0 | 364 | | { |
| | 0 | 365 | | _logger.LogInformation("Extracting API information from assemblies"); |
| | 0 | 366 | | var apiExtractor = serviceProvider.GetRequiredService<IApiExtractor>(); |
| | | 367 | | |
| | | 368 | | // Pass the filter configuration to the API extractor |
| | 0 | 369 | | var sourceApi = apiExtractor.ExtractApiMembers(sourceAssembly, config.Filters); |
| | 0 | 370 | | var targetApi = apiExtractor.ExtractApiMembers(targetAssembly, config.Filters); |
| | | 371 | | |
| | | 372 | | // Log the number of API members extracted |
| | 0 | 373 | | _logger.LogInformation( |
| | 0 | 374 | | "Extracted {SourceCount} API members from source and {TargetCount} API members from target", |
| | 0 | 375 | | sourceApi.Count(), |
| | 0 | 376 | | targetApi.Count()); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | // Compare APIs |
| | | 380 | | Models.ComparisonResult comparisonResult; |
| | 0 | 381 | | using (_logger.BeginScope("API comparison")) |
| | 0 | 382 | | { |
| | 0 | 383 | | _logger.LogInformation("Comparing APIs"); |
| | 0 | 384 | | var apiComparer = serviceProvider.GetRequiredService<IApiComparer>(); |
| | | 385 | | |
| | | 386 | | try |
| | 0 | 387 | | { |
| | | 388 | | // Use the single CompareAssemblies method - configuration is now injected into dependencies |
| | 0 | 389 | | comparisonResult = apiComparer.CompareAssemblies(sourceAssembly, targetAssembly); |
| | | 390 | | |
| | | 391 | | // Update configuration with actual command-line values ONLY if explicitly provided by user |
| | 0 | 392 | | if (!string.IsNullOrEmpty(settings.OutputFormat) && Enum.TryParse<ReportFormat>(settings.OutputFormat, t |
| | 0 | 393 | | { |
| | 0 | 394 | | comparisonResult.Configuration.OutputFormat = outputFormat; |
| | 0 | 395 | | } |
| | | 396 | | |
| | 0 | 397 | | if (!string.IsNullOrEmpty(settings.OutputFile)) |
| | 0 | 398 | | { |
| | 0 | 399 | | comparisonResult.Configuration.OutputPath = settings.OutputFile; |
| | 0 | 400 | | } |
| | 0 | 401 | | } |
| | 0 | 402 | | catch (Exception ex) |
| | 0 | 403 | | { |
| | 0 | 404 | | _logger.LogError(ex, "Error comparing assemblies"); |
| | 0 | 405 | | AnsiConsole.MarkupLine($"[red]Error comparing assemblies:[/] {ex.Message}"); |
| | | 406 | | |
| | 0 | 407 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | | 408 | | } |
| | 0 | 409 | | } |
| | | 410 | | |
| | | 411 | | // Create ApiComparison from ComparisonResult |
| | 0 | 412 | | var comparison = CreateApiComparisonFromResult(comparisonResult); |
| | | 413 | | |
| | | 414 | | // Generate report |
| | 0 | 415 | | using (_logger.BeginScope("Report generation")) |
| | 0 | 416 | | { |
| | | 417 | | // Use the configuration from the comparison result, which now has the correct precedence applied |
| | 0 | 418 | | var effectiveFormat = comparisonResult.Configuration.OutputFormat; |
| | 0 | 419 | | var effectiveOutputFile = comparisonResult.Configuration.OutputPath; |
| | | 420 | | |
| | 0 | 421 | | _logger.LogInformation("Generating {Format} report", effectiveFormat); |
| | 0 | 422 | | var reportGenerator = serviceProvider.GetRequiredService<IReportGenerator>(); |
| | | 423 | | |
| | | 424 | | string report; |
| | | 425 | | try |
| | 0 | 426 | | { |
| | 0 | 427 | | if (string.IsNullOrEmpty(effectiveOutputFile)) |
| | 0 | 428 | | { |
| | | 429 | | // No output file specified - output to console regardless of format |
| | 0 | 430 | | report = reportGenerator.GenerateReport(comparisonResult, effectiveFormat); |
| | | 431 | | |
| | | 432 | | // Output the formatted report to the console |
| | | 433 | | // Use Console.Write to avoid format string interpretation issues |
| | 0 | 434 | | Console.Write(report); |
| | 0 | 435 | | } |
| | | 436 | | else |
| | 0 | 437 | | { |
| | | 438 | | // Output file specified - save to the specified file |
| | 0 | 439 | | reportGenerator.SaveReportAsync(comparisonResult, effectiveFormat, effectiveOutputFile).GetAwaiter() |
| | 0 | 440 | | _logger.LogInformation("Report saved to {OutputFile}", effectiveOutputFile); |
| | 0 | 441 | | } |
| | 0 | 442 | | } |
| | 0 | 443 | | catch (Exception ex) |
| | 0 | 444 | | { |
| | 0 | 445 | | _logger.LogError(ex, "Error generating {Format} report", effectiveFormat); |
| | 0 | 446 | | AnsiConsole.MarkupLine($"[red]Error generating report:[/] {ex.Message}"); |
| | | 447 | | |
| | 0 | 448 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | | 449 | | } |
| | 0 | 450 | | } |
| | | 451 | | |
| | | 452 | | // Use the ExitCodeManager to determine the appropriate exit code |
| | 0 | 453 | | int exitCode = _exitCodeManager.GetExitCode(comparison); |
| | | 454 | | |
| | 0 | 455 | | if (comparison.HasBreakingChanges) |
| | 0 | 456 | | { |
| | 0 | 457 | | _logger.LogWarning("{Count} breaking changes detected", comparison.BreakingChangesCount); |
| | 0 | 458 | | } |
| | | 459 | | else |
| | 0 | 460 | | { |
| | 0 | 461 | | _logger.LogInformation("Comparison completed successfully with no breaking changes"); |
| | 0 | 462 | | } |
| | | 463 | | |
| | 0 | 464 | | _logger.LogInformation( |
| | 0 | 465 | | "Exiting with code {ExitCode}: {Description}", |
| | 0 | 466 | | exitCode, |
| | 0 | 467 | | _exitCodeManager.GetExitCodeDescription(exitCode)); |
| | | 468 | | |
| | 0 | 469 | | return exitCode; |
| | 0 | 470 | | } |
| | | 471 | | |
| | | 472 | | /// <summary> |
| | | 473 | | /// Applies command-line options to the configuration |
| | | 474 | | /// </summary> |
| | | 475 | | /// <param name="settings">Command settings</param> |
| | | 476 | | /// <param name="config">Configuration to update</param> |
| | | 477 | | /// <param name="logger">Logger for diagnostic information</param> |
| | | 478 | | private void ApplyCommandLineOptions(CompareCommandSettings settings, Models.Configuration.ComparisonConfiguration c |
| | 0 | 479 | | { |
| | 0 | 480 | | using (_logger.BeginScope("Applying command-line options")) |
| | 0 | 481 | | { |
| | | 482 | | // Apply namespace filters if specified |
| | 0 | 483 | | if (settings.NamespaceFilters != null && settings.NamespaceFilters.Length > 0) |
| | 0 | 484 | | { |
| | 0 | 485 | | _logger.LogInformation("Applying namespace filters: {Filters}", string.Join(", ", settings.NamespaceFilt |
| | | 486 | | |
| | | 487 | | // Add namespace filters to the configuration |
| | 0 | 488 | | config.Filters.IncludeNamespaces.AddRange(settings.NamespaceFilters); |
| | | 489 | | |
| | | 490 | | // If we have explicit includes, we're filtering to only those namespaces |
| | 0 | 491 | | if (config.Filters.IncludeNamespaces.Count > 0) |
| | 0 | 492 | | { |
| | 0 | 493 | | _logger.LogInformation("Filtering to only include specified namespaces"); |
| | 0 | 494 | | } |
| | 0 | 495 | | } |
| | | 496 | | |
| | | 497 | | // Apply type pattern filters if specified |
| | 0 | 498 | | if (settings.TypePatterns != null && settings.TypePatterns.Length > 0) |
| | 0 | 499 | | { |
| | 0 | 500 | | _logger.LogInformation("Applying type pattern filters: {Patterns}", string.Join(", ", settings.TypePatte |
| | | 501 | | |
| | | 502 | | // Add type pattern filters to the configuration |
| | 0 | 503 | | config.Filters.IncludeTypes.AddRange(settings.TypePatterns); |
| | | 504 | | |
| | 0 | 505 | | _logger.LogInformation("Filtering to only include types matching specified patterns"); |
| | 0 | 506 | | } |
| | | 507 | | |
| | | 508 | | // Apply command-line exclusions if specified |
| | 0 | 509 | | if (settings.ExcludePatterns != null && settings.ExcludePatterns.Length > 0) |
| | 0 | 510 | | { |
| | 0 | 511 | | _logger.LogInformation("Applying exclusion patterns: {Patterns}", string.Join(", ", settings.ExcludePatt |
| | | 512 | | |
| | | 513 | | // Add exclusion patterns to the configuration |
| | 0 | 514 | | foreach (var pattern in settings.ExcludePatterns) |
| | 0 | 515 | | { |
| | | 516 | | // Determine if this is a namespace or type pattern based on presence of dot |
| | 0 | 517 | | if (pattern.Contains('.')) |
| | 0 | 518 | | { |
| | | 519 | | // Assume it's a type pattern if it contains a dot |
| | 0 | 520 | | config.Exclusions.ExcludedTypePatterns.Add(pattern); |
| | 0 | 521 | | } |
| | | 522 | | else |
| | 0 | 523 | | { |
| | | 524 | | // Otherwise assume it's a namespace pattern |
| | 0 | 525 | | config.Filters.ExcludeNamespaces.Add(pattern); |
| | 0 | 526 | | } |
| | 0 | 527 | | } |
| | 0 | 528 | | } |
| | | 529 | | |
| | | 530 | | // Apply internal types inclusion if specified |
| | 0 | 531 | | if (settings.IncludeInternals) |
| | 0 | 532 | | { |
| | 0 | 533 | | _logger.LogInformation("Including internal types in comparison"); |
| | 0 | 534 | | config.Filters.IncludeInternals = true; |
| | 0 | 535 | | } |
| | | 536 | | |
| | | 537 | | // Apply compiler-generated types inclusion if specified |
| | 0 | 538 | | if (settings.IncludeCompilerGenerated) |
| | 0 | 539 | | { |
| | 0 | 540 | | _logger.LogInformation("Including compiler-generated types in comparison"); |
| | 0 | 541 | | config.Filters.IncludeCompilerGenerated = true; |
| | 0 | 542 | | } |
| | 0 | 543 | | } |
| | 0 | 544 | | } |
| | | 545 | | |
| | | 546 | | /// <summary> |
| | | 547 | | /// Creates an ApiComparison object from a ComparisonResult |
| | | 548 | | /// </summary> |
| | | 549 | | /// <param name="comparisonResult">The comparison result to convert</param> |
| | | 550 | | /// <returns>An ApiComparison object</returns> |
| | | 551 | | private Models.ApiComparison CreateApiComparisonFromResult(Models.ComparisonResult comparisonResult) |
| | 0 | 552 | | { |
| | 0 | 553 | | return new Models.ApiComparison |
| | 0 | 554 | | { |
| | 0 | 555 | | Additions = comparisonResult.Differences |
| | 0 | 556 | | .Where(d => d.ChangeType == Models.ChangeType.Added) |
| | 0 | 557 | | .Select(d => new Models.ApiChange |
| | 0 | 558 | | { |
| | 0 | 559 | | Type = Models.ChangeType.Added, |
| | 0 | 560 | | Description = d.Description, |
| | 0 | 561 | | TargetMember = new Models.ApiMember |
| | 0 | 562 | | { |
| | 0 | 563 | | Name = ExtractMemberName(d.ElementName), |
| | 0 | 564 | | FullName = d.ElementName, |
| | 0 | 565 | | Signature = d.NewSignature ?? "Unknown" |
| | 0 | 566 | | }, |
| | 0 | 567 | | IsBreakingChange = d.IsBreakingChange |
| | 0 | 568 | | }).ToList(), |
| | 0 | 569 | | Removals = comparisonResult.Differences |
| | 0 | 570 | | .Where(d => d.ChangeType == Models.ChangeType.Removed) |
| | 0 | 571 | | .Select(d => new Models.ApiChange |
| | 0 | 572 | | { |
| | 0 | 573 | | Type = Models.ChangeType.Removed, |
| | 0 | 574 | | Description = d.Description, |
| | 0 | 575 | | SourceMember = new Models.ApiMember |
| | 0 | 576 | | { |
| | 0 | 577 | | Name = ExtractMemberName(d.ElementName), |
| | 0 | 578 | | FullName = d.ElementName, |
| | 0 | 579 | | Signature = d.OldSignature ?? "Unknown" |
| | 0 | 580 | | }, |
| | 0 | 581 | | IsBreakingChange = d.IsBreakingChange |
| | 0 | 582 | | }).ToList(), |
| | 0 | 583 | | Modifications = comparisonResult.Differences |
| | 0 | 584 | | .Where(d => d.ChangeType == Models.ChangeType.Modified) |
| | 0 | 585 | | .Select(d => new Models.ApiChange |
| | 0 | 586 | | { |
| | 0 | 587 | | Type = Models.ChangeType.Modified, |
| | 0 | 588 | | Description = d.Description, |
| | 0 | 589 | | SourceMember = new Models.ApiMember |
| | 0 | 590 | | { |
| | 0 | 591 | | Name = ExtractMemberName(d.ElementName), |
| | 0 | 592 | | FullName = d.ElementName, |
| | 0 | 593 | | Signature = d.OldSignature ?? "Unknown" |
| | 0 | 594 | | }, |
| | 0 | 595 | | TargetMember = new Models.ApiMember |
| | 0 | 596 | | { |
| | 0 | 597 | | Name = ExtractMemberName(d.ElementName), |
| | 0 | 598 | | FullName = d.ElementName, |
| | 0 | 599 | | Signature = d.NewSignature ?? "Unknown" |
| | 0 | 600 | | }, |
| | 0 | 601 | | IsBreakingChange = d.IsBreakingChange |
| | 0 | 602 | | }).ToList(), |
| | 0 | 603 | | Excluded = comparisonResult.Differences |
| | 0 | 604 | | .Where(d => d.ChangeType == Models.ChangeType.Excluded) |
| | 0 | 605 | | .Select(d => new Models.ApiChange |
| | 0 | 606 | | { |
| | 0 | 607 | | Type = Models.ChangeType.Excluded, |
| | 0 | 608 | | Description = d.Description, |
| | 0 | 609 | | SourceMember = new Models.ApiMember |
| | 0 | 610 | | { |
| | 0 | 611 | | Name = ExtractMemberName(d.ElementName), |
| | 0 | 612 | | FullName = d.ElementName, |
| | 0 | 613 | | Signature = "Unknown" |
| | 0 | 614 | | }, |
| | 0 | 615 | | IsBreakingChange = false |
| | 0 | 616 | | }).ToList() |
| | 0 | 617 | | }; |
| | 0 | 618 | | } |
| | | 619 | | } |