| | | 1 | | // Copyright DotNet API Diff Project Contributors - SPDX Identifier: MIT |
| | | 2 | | using DotNetApiDiff.Commands; |
| | | 3 | | using DotNetApiDiff.ExitCodes; |
| | | 4 | | using DotNetApiDiff.Interfaces; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | using Spectre.Console.Cli; |
| | | 8 | | |
| | | 9 | | namespace DotNetApiDiff; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Main entry point for the DotNet API Diff tool |
| | | 13 | | /// </summary> |
| | | 14 | | public class Program |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Application entry point |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="args">Command line arguments</param> |
| | | 20 | | /// <returns>Exit code (0 for success, non-zero for failure)</returns> |
| | | 21 | | public static int Main(string[] args) |
| | 9 | 22 | | { |
| | | 23 | | // Set up dependency injection |
| | 9 | 24 | | var services = new ServiceCollection(); |
| | 9 | 25 | | ConfigureServices(services); |
| | | 26 | | |
| | 9 | 27 | | var serviceProvider = services.BuildServiceProvider(); |
| | 9 | 28 | | var logger = serviceProvider.GetRequiredService<ILogger<Program>>(); |
| | 9 | 29 | | var exceptionHandler = serviceProvider.GetRequiredService<IGlobalExceptionHandler>(); |
| | | 30 | | |
| | | 31 | | // Set up global exception handling |
| | 9 | 32 | | exceptionHandler.SetupGlobalExceptionHandling(); |
| | | 33 | | |
| | | 34 | | try |
| | 9 | 35 | | { |
| | | 36 | | // Log application start with version information |
| | 9 | 37 | | var version = typeof(Program).Assembly.GetName().Version; |
| | 9 | 38 | | var buildTime = GetBuildTime(); |
| | | 39 | | |
| | 9 | 40 | | logger.LogInformation( |
| | 9 | 41 | | "DotNet API Diff Tool v{Version} started at {Time} (Build: {BuildTime})", |
| | 9 | 42 | | version, |
| | 9 | 43 | | DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), |
| | 9 | 44 | | buildTime); |
| | | 45 | | |
| | | 46 | | // Log environment information |
| | 9 | 47 | | logger.LogDebug( |
| | 9 | 48 | | "Environment: OS={OS}, Framework={Framework}, ProcessorCount={ProcessorCount}", |
| | 9 | 49 | | Environment.OSVersion, |
| | 9 | 50 | | Environment.Version, |
| | 9 | 51 | | Environment.ProcessorCount); |
| | | 52 | | |
| | | 53 | | // Get the exit code manager |
| | 9 | 54 | | var exitCodeManager = serviceProvider.GetRequiredService<IExitCodeManager>(); |
| | | 55 | | |
| | | 56 | | // Configure Spectre.Console command app |
| | 9 | 57 | | var app = new CommandApp(new TypeRegistrar(serviceProvider)); |
| | | 58 | | |
| | 9 | 59 | | app.Configure(config => |
| | 9 | 60 | | { |
| | 9 | 61 | | config.SetApplicationName("dotnet-api-diff"); |
| | 9 | 62 | | config.PropagateExceptions(); |
| | 9 | 63 | | |
| | 9 | 64 | | config.AddExample(new[] { "compare", "source.dll", "target.dll" }); |
| | 9 | 65 | | config.AddExample(new[] { "compare", "source.dll", "target.dll", "--output", "json" }); |
| | 9 | 66 | | config.AddExample(new[] { "compare", "source.dll", "target.dll", "--config", "config.json" }); |
| | 9 | 67 | | |
| | 9 | 68 | | config.SetExceptionHandler(ex => |
| | 0 | 69 | | { |
| | 9 | 70 | | // Use our centralized exception handler |
| | 0 | 71 | | return exceptionHandler.HandleException(ex, "Command execution"); |
| | 9 | 72 | | }); |
| | 9 | 73 | | |
| | 9 | 74 | | // Register the compare command |
| | 9 | 75 | | config.AddCommand<CompareCommand>("compare") |
| | 9 | 76 | | .WithDescription("Compare two .NET assemblies and report API differences") |
| | 9 | 77 | | .WithExample(new[] { "compare", "source.dll", "target.dll" }) |
| | 9 | 78 | | .WithExample(new[] { "compare", "source.dll", "target.dll", "--output", "json" }) |
| | 9 | 79 | | .WithExample(new[] { "compare", "source.dll", "target.dll", "--filter", "System.Collections" }); |
| | 18 | 80 | | }); |
| | | 81 | | |
| | | 82 | | // Run the command and return the exit code |
| | 9 | 83 | | return app.Run(args); |
| | | 84 | | } |
| | 3 | 85 | | catch (Exception ex) |
| | 3 | 86 | | { |
| | | 87 | | // Use our centralized exception handler for any unhandled exceptions |
| | 3 | 88 | | return exceptionHandler.HandleException(ex, "Application startup"); |
| | | 89 | | } |
| | 9 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the build time of the assembly from the file timestamp |
| | | 94 | | /// </summary> |
| | | 95 | | /// <returns>Build time as a string</returns> |
| | | 96 | | private static string GetBuildTime() |
| | 10 | 97 | | { |
| | | 98 | | try |
| | 10 | 99 | | { |
| | 10 | 100 | | var assembly = typeof(Program).Assembly; |
| | 10 | 101 | | var assemblyLocation = assembly.Location; |
| | 10 | 102 | | if (!string.IsNullOrEmpty(assemblyLocation)) |
| | 10 | 103 | | { |
| | 10 | 104 | | var buildTime = File.GetLastWriteTime(assemblyLocation); |
| | 10 | 105 | | return buildTime.ToString("yyyy-MM-dd HH:mm:ss"); |
| | | 106 | | } |
| | 0 | 107 | | } |
| | 0 | 108 | | catch (Exception) |
| | 0 | 109 | | { |
| | | 110 | | // Ignore errors getting build time |
| | 0 | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | return "Unknown"; |
| | 10 | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Configures dependency injection services for the root container |
| | | 118 | | /// Only includes services needed for command instantiation |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="services">Service collection to configure</param> |
| | | 121 | | public static void ConfigureServices(IServiceCollection services) |
| | 25 | 122 | | { |
| | | 123 | | // Configure logging with structured logging support |
| | 25 | 124 | | services.AddLogging(builder => |
| | 25 | 125 | | { |
| | 25 | 126 | | builder.AddConsole(options => |
| | 24 | 127 | | { |
| | 25 | 128 | | // Use ConsoleFormatterOptions instead of deprecated ConsoleLoggerOptions |
| | 24 | 129 | | options.FormatterName = "simple"; |
| | 49 | 130 | | }); |
| | 25 | 131 | | |
| | 25 | 132 | | // Configure the simple console formatter |
| | 25 | 133 | | builder.AddSimpleConsole(options => |
| | 24 | 134 | | { |
| | 24 | 135 | | options.IncludeScopes = true; |
| | 24 | 136 | | options.TimestampFormat = "[yyyy-MM-dd HH:mm:ss] "; |
| | 24 | 137 | | options.SingleLine = false; |
| | 24 | 138 | | options.UseUtcTimestamp = false; |
| | 49 | 139 | | }); |
| | 25 | 140 | | |
| | 25 | 141 | | // Set minimum level based on environment variable if present |
| | 25 | 142 | | var logLevelEnv = Environment.GetEnvironmentVariable("DOTNET_API_DIFF_LOG_LEVEL"); |
| | 25 | 143 | | var logLevel = LogLevel.Information; // Default level |
| | 25 | 144 | | |
| | 25 | 145 | | if (!string.IsNullOrEmpty(logLevelEnv) && |
| | 25 | 146 | | Enum.TryParse<LogLevel>(logLevelEnv, true, out var parsedLevel)) |
| | 6 | 147 | | { |
| | 6 | 148 | | logLevel = parsedLevel; |
| | 6 | 149 | | } |
| | 25 | 150 | | |
| | 25 | 151 | | builder.SetMinimumLevel(logLevel); |
| | 50 | 152 | | }); |
| | | 153 | | |
| | | 154 | | // Register only services needed for command instantiation |
| | 25 | 155 | | services.AddSingleton<IExitCodeManager, ExitCodeManager>(); |
| | 25 | 156 | | services.AddSingleton<IGlobalExceptionHandler, GlobalExceptionHandler>(); |
| | | 157 | | |
| | | 158 | | // Note: Business logic services (AssemblyLoader, ApiExtractor, etc.) will be registered |
| | | 159 | | // in the command-specific container to avoid configuration timing issues |
| | 25 | 160 | | } |
| | | 161 | | } |