| | 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 | | /// Executes the comparison logic using the configured services |
| | 302 | | /// </summary> |
| | 303 | | /// <param name="settings">Command settings</param> |
| | 304 | | /// <param name="config">Loaded configuration</param> |
| | 305 | | /// <param name="serviceProvider">Command-specific service provider</param> |
| | 306 | | /// <returns>Exit code</returns> |
| | 307 | | private int ExecuteWithConfiguredServices(CompareCommandSettings settings, ComparisonConfiguration config, IServiceP |
| 0 | 308 | | { |
| | 309 | | // Load assemblies |
| | 310 | | Assembly sourceAssembly; |
| | 311 | | Assembly targetAssembly; |
| | 312 | |
|
| 0 | 313 | | using (_logger.BeginScope("Assembly loading")) |
| 0 | 314 | | { |
| 0 | 315 | | _logger.LogInformation("Loading source assembly: {Path}", settings.SourceAssemblyPath); |
| 0 | 316 | | _logger.LogInformation("Loading target assembly: {Path}", settings.TargetAssemblyPath); |
| | 317 | |
|
| 0 | 318 | | var assemblyLoader = serviceProvider.GetRequiredService<IAssemblyLoader>(); |
| | 319 | |
|
| | 320 | | try |
| 0 | 321 | | { |
| 0 | 322 | | sourceAssembly = assemblyLoader.LoadAssembly(settings.SourceAssemblyPath!); |
| 0 | 323 | | } |
| 0 | 324 | | catch (Exception ex) |
| 0 | 325 | | { |
| 0 | 326 | | _logger.LogError(ex, "Failed to load source assembly: {Path}", settings.SourceAssemblyPath); |
| 0 | 327 | | AnsiConsole.MarkupLine($"[red]Error loading source assembly:[/] {ex.Message}"); |
| | 328 | |
|
| 0 | 329 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | 330 | | } |
| | 331 | |
|
| | 332 | | try |
| 0 | 333 | | { |
| 0 | 334 | | targetAssembly = assemblyLoader.LoadAssembly(settings.TargetAssemblyPath!); |
| 0 | 335 | | } |
| 0 | 336 | | catch (Exception ex) |
| 0 | 337 | | { |
| 0 | 338 | | _logger.LogError(ex, "Failed to load target assembly: {Path}", settings.TargetAssemblyPath); |
| 0 | 339 | | AnsiConsole.MarkupLine($"[red]Error loading target assembly:[/] {ex.Message}"); |
| | 340 | |
|
| 0 | 341 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | 342 | | } |
| 0 | 343 | | } |
| | 344 | |
|
| | 345 | | // Extract API information |
| 0 | 346 | | using (_logger.BeginScope("API extraction")) |
| 0 | 347 | | { |
| 0 | 348 | | _logger.LogInformation("Extracting API information from assemblies"); |
| 0 | 349 | | var apiExtractor = serviceProvider.GetRequiredService<IApiExtractor>(); |
| | 350 | |
|
| | 351 | | // Pass the filter configuration to the API extractor |
| 0 | 352 | | var sourceApi = apiExtractor.ExtractApiMembers(sourceAssembly, config.Filters); |
| 0 | 353 | | var targetApi = apiExtractor.ExtractApiMembers(targetAssembly, config.Filters); |
| | 354 | |
|
| | 355 | | // Log the number of API members extracted |
| 0 | 356 | | _logger.LogInformation( |
| 0 | 357 | | "Extracted {SourceCount} API members from source and {TargetCount} API members from target", |
| 0 | 358 | | sourceApi.Count(), |
| 0 | 359 | | targetApi.Count()); |
| 0 | 360 | | } |
| | 361 | |
|
| | 362 | | // Compare APIs |
| | 363 | | Models.ComparisonResult comparisonResult; |
| 0 | 364 | | using (_logger.BeginScope("API comparison")) |
| 0 | 365 | | { |
| 0 | 366 | | _logger.LogInformation("Comparing APIs"); |
| 0 | 367 | | var apiComparer = serviceProvider.GetRequiredService<IApiComparer>(); |
| | 368 | |
|
| | 369 | | try |
| 0 | 370 | | { |
| | 371 | | // Use the single CompareAssemblies method - configuration is now injected into dependencies |
| 0 | 372 | | comparisonResult = apiComparer.CompareAssemblies(sourceAssembly, targetAssembly); |
| | 373 | |
|
| | 374 | | // Update configuration with actual command-line values ONLY if explicitly provided by user |
| 0 | 375 | | if (!string.IsNullOrEmpty(settings.OutputFormat) && Enum.TryParse<ReportFormat>(settings.OutputFormat, t |
| 0 | 376 | | { |
| 0 | 377 | | comparisonResult.Configuration.OutputFormat = outputFormat; |
| 0 | 378 | | } |
| | 379 | |
|
| 0 | 380 | | if (!string.IsNullOrEmpty(settings.OutputFile)) |
| 0 | 381 | | { |
| 0 | 382 | | comparisonResult.Configuration.OutputPath = settings.OutputFile; |
| 0 | 383 | | } |
| 0 | 384 | | } |
| 0 | 385 | | catch (Exception ex) |
| 0 | 386 | | { |
| 0 | 387 | | _logger.LogError(ex, "Error comparing assemblies"); |
| 0 | 388 | | AnsiConsole.MarkupLine($"[red]Error comparing assemblies:[/] {ex.Message}"); |
| | 389 | |
|
| 0 | 390 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | 391 | | } |
| 0 | 392 | | } |
| | 393 | |
|
| | 394 | | // Create ApiComparison from ComparisonResult |
| 0 | 395 | | var comparison = CreateApiComparisonFromResult(comparisonResult); |
| | 396 | |
|
| | 397 | | // Generate report |
| 0 | 398 | | using (_logger.BeginScope("Report generation")) |
| 0 | 399 | | { |
| | 400 | | // Use the configuration from the comparison result, which now has the correct precedence applied |
| 0 | 401 | | var effectiveFormat = comparisonResult.Configuration.OutputFormat; |
| 0 | 402 | | var effectiveOutputFile = comparisonResult.Configuration.OutputPath; |
| | 403 | |
|
| 0 | 404 | | _logger.LogInformation("Generating {Format} report", effectiveFormat); |
| 0 | 405 | | var reportGenerator = serviceProvider.GetRequiredService<IReportGenerator>(); |
| | 406 | |
|
| | 407 | | string report; |
| | 408 | | try |
| 0 | 409 | | { |
| 0 | 410 | | if (string.IsNullOrEmpty(effectiveOutputFile)) |
| 0 | 411 | | { |
| | 412 | | // No output file specified - output to console regardless of format |
| 0 | 413 | | report = reportGenerator.GenerateReport(comparisonResult, effectiveFormat); |
| | 414 | |
|
| | 415 | | // Output the formatted report to the console |
| | 416 | | // Use Console.Write to avoid format string interpretation issues |
| 0 | 417 | | Console.Write(report); |
| 0 | 418 | | } |
| | 419 | | else |
| 0 | 420 | | { |
| | 421 | | // Output file specified - save to the specified file |
| 0 | 422 | | reportGenerator.SaveReportAsync(comparisonResult, effectiveFormat, effectiveOutputFile).GetAwaiter() |
| 0 | 423 | | _logger.LogInformation("Report saved to {OutputFile}", effectiveOutputFile); |
| 0 | 424 | | } |
| 0 | 425 | | } |
| 0 | 426 | | catch (Exception ex) |
| 0 | 427 | | { |
| 0 | 428 | | _logger.LogError(ex, "Error generating {Format} report", effectiveFormat); |
| 0 | 429 | | AnsiConsole.MarkupLine($"[red]Error generating report:[/] {ex.Message}"); |
| | 430 | |
|
| 0 | 431 | | return _exitCodeManager.GetExitCodeForException(ex); |
| | 432 | | } |
| 0 | 433 | | } |
| | 434 | |
|
| | 435 | | // Use the ExitCodeManager to determine the appropriate exit code |
| 0 | 436 | | int exitCode = _exitCodeManager.GetExitCode(comparison); |
| | 437 | |
|
| 0 | 438 | | if (comparison.HasBreakingChanges) |
| 0 | 439 | | { |
| 0 | 440 | | _logger.LogWarning("{Count} breaking changes detected", comparison.BreakingChangesCount); |
| 0 | 441 | | } |
| | 442 | | else |
| 0 | 443 | | { |
| 0 | 444 | | _logger.LogInformation("Comparison completed successfully with no breaking changes"); |
| 0 | 445 | | } |
| | 446 | |
|
| 0 | 447 | | _logger.LogInformation( |
| 0 | 448 | | "Exiting with code {ExitCode}: {Description}", |
| 0 | 449 | | exitCode, |
| 0 | 450 | | _exitCodeManager.GetExitCodeDescription(exitCode)); |
| | 451 | |
|
| 0 | 452 | | return exitCode; |
| 0 | 453 | | } |
| | 454 | |
|
| | 455 | | /// <summary> |
| | 456 | | /// Applies command-line options to the configuration |
| | 457 | | /// </summary> |
| | 458 | | /// <param name="settings">Command settings</param> |
| | 459 | | /// <param name="config">Configuration to update</param> |
| | 460 | | /// <param name="logger">Logger for diagnostic information</param> |
| | 461 | | private void ApplyCommandLineOptions(CompareCommandSettings settings, Models.Configuration.ComparisonConfiguration c |
| 0 | 462 | | { |
| 0 | 463 | | using (_logger.BeginScope("Applying command-line options")) |
| 0 | 464 | | { |
| | 465 | | // Apply namespace filters if specified |
| 0 | 466 | | if (settings.NamespaceFilters != null && settings.NamespaceFilters.Length > 0) |
| 0 | 467 | | { |
| 0 | 468 | | _logger.LogInformation("Applying namespace filters: {Filters}", string.Join(", ", settings.NamespaceFilt |
| | 469 | |
|
| | 470 | | // Add namespace filters to the configuration |
| 0 | 471 | | config.Filters.IncludeNamespaces.AddRange(settings.NamespaceFilters); |
| | 472 | |
|
| | 473 | | // If we have explicit includes, we're filtering to only those namespaces |
| 0 | 474 | | if (config.Filters.IncludeNamespaces.Count > 0) |
| 0 | 475 | | { |
| 0 | 476 | | _logger.LogInformation("Filtering to only include specified namespaces"); |
| 0 | 477 | | } |
| 0 | 478 | | } |
| | 479 | |
|
| | 480 | | // Apply type pattern filters if specified |
| 0 | 481 | | if (settings.TypePatterns != null && settings.TypePatterns.Length > 0) |
| 0 | 482 | | { |
| 0 | 483 | | _logger.LogInformation("Applying type pattern filters: {Patterns}", string.Join(", ", settings.TypePatte |
| | 484 | |
|
| | 485 | | // Add type pattern filters to the configuration |
| 0 | 486 | | config.Filters.IncludeTypes.AddRange(settings.TypePatterns); |
| | 487 | |
|
| 0 | 488 | | _logger.LogInformation("Filtering to only include types matching specified patterns"); |
| 0 | 489 | | } |
| | 490 | |
|
| | 491 | | // Apply command-line exclusions if specified |
| 0 | 492 | | if (settings.ExcludePatterns != null && settings.ExcludePatterns.Length > 0) |
| 0 | 493 | | { |
| 0 | 494 | | _logger.LogInformation("Applying exclusion patterns: {Patterns}", string.Join(", ", settings.ExcludePatt |
| | 495 | |
|
| | 496 | | // Add exclusion patterns to the configuration |
| 0 | 497 | | foreach (var pattern in settings.ExcludePatterns) |
| 0 | 498 | | { |
| | 499 | | // Determine if this is a namespace or type pattern based on presence of dot |
| 0 | 500 | | if (pattern.Contains('.')) |
| 0 | 501 | | { |
| | 502 | | // Assume it's a type pattern if it contains a dot |
| 0 | 503 | | config.Exclusions.ExcludedTypePatterns.Add(pattern); |
| 0 | 504 | | } |
| | 505 | | else |
| 0 | 506 | | { |
| | 507 | | // Otherwise assume it's a namespace pattern |
| 0 | 508 | | config.Filters.ExcludeNamespaces.Add(pattern); |
| 0 | 509 | | } |
| 0 | 510 | | } |
| 0 | 511 | | } |
| | 512 | |
|
| | 513 | | // Apply internal types inclusion if specified |
| 0 | 514 | | if (settings.IncludeInternals) |
| 0 | 515 | | { |
| 0 | 516 | | _logger.LogInformation("Including internal types in comparison"); |
| 0 | 517 | | config.Filters.IncludeInternals = true; |
| 0 | 518 | | } |
| | 519 | |
|
| | 520 | | // Apply compiler-generated types inclusion if specified |
| 0 | 521 | | if (settings.IncludeCompilerGenerated) |
| 0 | 522 | | { |
| 0 | 523 | | _logger.LogInformation("Including compiler-generated types in comparison"); |
| 0 | 524 | | config.Filters.IncludeCompilerGenerated = true; |
| 0 | 525 | | } |
| 0 | 526 | | } |
| 0 | 527 | | } |
| | 528 | |
|
| | 529 | | /// <summary> |
| | 530 | | /// Creates an ApiComparison object from a ComparisonResult |
| | 531 | | /// </summary> |
| | 532 | | /// <param name="comparisonResult">The comparison result to convert</param> |
| | 533 | | /// <returns>An ApiComparison object</returns> |
| | 534 | | private Models.ApiComparison CreateApiComparisonFromResult(Models.ComparisonResult comparisonResult) |
| 0 | 535 | | { |
| 0 | 536 | | return new Models.ApiComparison |
| 0 | 537 | | { |
| 0 | 538 | | Additions = comparisonResult.Differences |
| 0 | 539 | | .Where(d => d.ChangeType == Models.ChangeType.Added) |
| 0 | 540 | | .Select(d => new Models.ApiChange |
| 0 | 541 | | { |
| 0 | 542 | | Type = Models.ChangeType.Added, |
| 0 | 543 | | TargetMember = new Models.ApiMember { Name = d.ElementName }, |
| 0 | 544 | | IsBreakingChange = d.IsBreakingChange |
| 0 | 545 | | }).ToList(), |
| 0 | 546 | | Removals = comparisonResult.Differences |
| 0 | 547 | | .Where(d => d.ChangeType == Models.ChangeType.Removed) |
| 0 | 548 | | .Select(d => new Models.ApiChange |
| 0 | 549 | | { |
| 0 | 550 | | Type = Models.ChangeType.Removed, |
| 0 | 551 | | SourceMember = new Models.ApiMember { Name = d.ElementName }, |
| 0 | 552 | | IsBreakingChange = d.IsBreakingChange |
| 0 | 553 | | }).ToList(), |
| 0 | 554 | | Modifications = comparisonResult.Differences |
| 0 | 555 | | .Where(d => d.ChangeType == Models.ChangeType.Modified) |
| 0 | 556 | | .Select(d => new Models.ApiChange |
| 0 | 557 | | { |
| 0 | 558 | | Type = Models.ChangeType.Modified, |
| 0 | 559 | | SourceMember = new Models.ApiMember { Name = d.ElementName }, |
| 0 | 560 | | TargetMember = new Models.ApiMember { Name = d.ElementName }, |
| 0 | 561 | | IsBreakingChange = d.IsBreakingChange |
| 0 | 562 | | }).ToList(), |
| 0 | 563 | | Excluded = comparisonResult.Differences |
| 0 | 564 | | .Where(d => d.ChangeType == Models.ChangeType.Excluded) |
| 0 | 565 | | .Select(d => new Models.ApiChange |
| 0 | 566 | | { |
| 0 | 567 | | Type = Models.ChangeType.Excluded, |
| 0 | 568 | | SourceMember = new Models.ApiMember { Name = d.ElementName }, |
| 0 | 569 | | IsBreakingChange = false |
| 0 | 570 | | }).ToList() |
| 0 | 571 | | }; |
| 0 | 572 | | } |
| | 573 | | } |