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