From 5c21cf97b853222cc9ed2b56e80060b5d591a930 Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 10 Nov 2026 00:47:49 -0500 Subject: [PATCH 1/7] Change SilkTouchRegistrationExtensions to use MSDI --- sources/SilkTouch/SilkTouch/SilkTouchHost.cs | 7 +- .../SilkTouchRegistrationExtensions.cs | 260 +++++------------- 2 files changed, 75 insertions(+), 192 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs index 144883a650..526cae7b83 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs @@ -142,7 +142,12 @@ await Parallel.ForEachAsync( jobKvp.Key, jobKvp.Value.Config, container, - builder => jobKvp.Value.Configure.Invoke(builder.SilkTouch()) + builder => + { + var services = new ServiceCollection(); + jobKvp.Value.Configure.Invoke(services.SilkTouch()); + builder.Populate(services); + } ); await job.RunAsync(ct); diff --git a/sources/SilkTouch/SilkTouch/SilkTouchRegistrationExtensions.cs b/sources/SilkTouch/SilkTouch/SilkTouchRegistrationExtensions.cs index c6dc9061ba..7b397ccac5 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchRegistrationExtensions.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchRegistrationExtensions.cs @@ -1,8 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using Autofac; -using Autofac.Builder; +using Microsoft.Extensions.DependencyInjection; using Silk.NET.SilkTouch.Clang; using Silk.NET.SilkTouch.Mods; using Silk.NET.SilkTouch.Mods.Transformation; @@ -18,8 +17,8 @@ public static class SilkTouchRegistrationExtensions /// Entrypoint to the rest of the SilkTouch registration methods. /// Begins a . /// - public static SilkTouchRegistrationContext SilkTouch(this ContainerBuilder builder) => - new(builder); + public static SilkTouchRegistrationContext SilkTouch(this IServiceCollection services) => + new(services); // Non-mods extension(SilkTouchRegistrationContext context) @@ -42,11 +41,7 @@ public SilkTouchRegistrationContext WithDefaultFunctionTransformers( /// public SilkTouchRegistrationContext WithPtrRefTransformer() { - context - .Builder.RegisterType() - .AsImplementedInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsInterfaces(); return context; } @@ -58,17 +53,9 @@ public SilkTouchRegistrationContext WithBoolTransformer( BoolTransformer.Configuration? boolTransformerConfig = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter( - typeof(BoolTransformer.Configuration), - boolTransformerConfig ?? new() - ) - ) - .AsImplementedInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsInterfaces( + boolTransformerConfig ?? new() + ); return context; } @@ -78,11 +65,7 @@ public SilkTouchRegistrationContext WithBoolTransformer( /// public SilkTouchRegistrationContext WithArrayParameterTransformer() { - context - .Builder.RegisterType() - .AsImplementedInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsInterfaces(); return context; } } @@ -97,14 +80,7 @@ public SilkTouchRegistrationContext AddApiProfiles( AddApiProfiles.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(AddApiProfiles.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -113,12 +89,7 @@ public SilkTouchRegistrationContext AddApiProfiles( /// public SilkTouchRegistrationContext ClangScraper(ClangScraper.Configuration config) { - context - .Builder.RegisterType() - .WithParameter(new TypedParameter(typeof(ClangScraper.Configuration), config)) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -127,14 +98,7 @@ public SilkTouchRegistrationContext ClangScraper(ClangScraper.Configuration conf /// public SilkTouchRegistrationContext AddIncludes(AddIncludes.Configuration? config = null) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(AddIncludes.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -143,12 +107,7 @@ public SilkTouchRegistrationContext AddIncludes(AddIncludes.Configuration? confi /// public SilkTouchRegistrationContext AddOpaqueStructs(AddOpaqueStructs.Configuration config) { - context - .Builder.RegisterType() - .WithParameter(new TypedParameter(typeof(AddOpaqueStructs.Configuration), config)) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -157,14 +116,7 @@ public SilkTouchRegistrationContext AddOpaqueStructs(AddOpaqueStructs.Configurat /// public SilkTouchRegistrationContext AddVTables(AddVTables.Configuration? config = null) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(AddVTables.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -173,12 +125,7 @@ public SilkTouchRegistrationContext AddVTables(AddVTables.Configuration? config /// public SilkTouchRegistrationContext BakeSourceSets(BakeSourceSets.Configuration config) { - context - .Builder.RegisterType() - .WithParameter(new TypedParameter(typeof(BakeSourceSets.Configuration), config)) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -187,12 +134,7 @@ public SilkTouchRegistrationContext BakeSourceSets(BakeSourceSets.Configuration /// public SilkTouchRegistrationContext ChangeNamespace(ChangeNamespace.Configuration config) { - context - .Builder.RegisterType() - .WithParameter(new TypedParameter(typeof(ChangeNamespace.Configuration), config)) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -203,12 +145,7 @@ public SilkTouchRegistrationContext ChangeNativeClass( ChangeNativeClass.Configuration config ) { - context - .Builder.RegisterType() - .WithParameter(new TypedParameter(typeof(ChangeNativeClass.Configuration), config)) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -217,11 +154,7 @@ ChangeNativeClass.Configuration config /// public SilkTouchRegistrationContext ExtractEnumConstants() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -230,11 +163,7 @@ public SilkTouchRegistrationContext ExtractEnumConstants() /// public SilkTouchRegistrationContext ExtractFunctionPointers() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -243,11 +172,7 @@ public SilkTouchRegistrationContext ExtractFunctionPointers() /// public SilkTouchRegistrationContext ExtractHandles() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -256,11 +181,7 @@ public SilkTouchRegistrationContext ExtractHandles() /// public SilkTouchRegistrationContext ExtractNestedTypes() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -271,14 +192,7 @@ public SilkTouchRegistrationContext IdentifySharedPrefixes( IdentifySharedPrefixes.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(IdentifySharedPrefixes.Configuration), config) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -289,14 +203,7 @@ public SilkTouchRegistrationContext InterceptNativeFunctions( InterceptNativeFunctions.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(InterceptNativeFunctions.Configuration), config) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } @@ -305,11 +212,7 @@ public SilkTouchRegistrationContext InterceptNativeFunctions( /// public SilkTouchRegistrationContext MarkNativeNames() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -320,14 +223,7 @@ public SilkTouchRegistrationContext MixKhronosData( MixKhronosData.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(MixKhronosData.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -338,14 +234,7 @@ public SilkTouchRegistrationContext PrettifyNames( PrettifyNames.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(PrettifyNames.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -356,14 +245,7 @@ public SilkTouchRegistrationContext StripAttributes( StripAttributes.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(StripAttributes.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -374,14 +256,7 @@ public SilkTouchRegistrationContext TransformEnums( TransformEnums.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(TransformEnums.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config ?? new()); return context; } @@ -390,11 +265,7 @@ public SilkTouchRegistrationContext TransformEnums( /// public SilkTouchRegistrationContext TransformFunctions() { - context - .Builder.RegisterType() - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(); return context; } @@ -405,14 +276,9 @@ public SilkTouchRegistrationContext TransformHandles( TransformHandles.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(TransformHandles.Configuration), config ?? new()) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces( + config ?? new() + ); return context; } @@ -423,46 +289,58 @@ public SilkTouchRegistrationContext TransformProperties( TransformProperties.Configuration? config = null ) { - context - .Builder.RegisterType() - .WithParameter( - new TypedParameter(typeof(TransformProperties.Configuration), config) - ) - .AsHierarchyAndInterfaces() - .SingleInstance(); - + context.Services.AddSingletonAsHierarchyAndInterfaces(config); return context; } } - extension( - IRegistrationBuilder registration - ) - where TConcreteActivatorData : IConcreteActivatorData + extension(IServiceCollection services) { - private IRegistrationBuilder< - TLimit, - TConcreteActivatorData, - SingleRegistrationStyle - > AsHierarchyAndInterfaces() - { - var type = registration.ActivatorData.Activator.LimitType; - var typesToRegister = new HashSet(); - typesToRegister.UnionWith(type.GetInterfaces()); + private IServiceCollection AddSingletonAsInterfaces(object? parameter = null) + where T : class => services.AddSingletonAs(typeof(T).GetInterfaces(), parameter); - var current = type; + private IServiceCollection AddSingletonAsHierarchyAndInterfaces(object? parameter = null) + where T : class + { + var types = new HashSet(typeof(T).GetInterfaces()); + var current = typeof(T); while (current != null) { - typesToRegister.Add(current); + types.Add(current); current = current.BaseType; } - return registration.As(typesToRegister.ToArray()); + return services.AddSingletonAs(types, parameter); + } + + private IServiceCollection AddSingletonAs(IEnumerable types, object? parameter) + where T : class + { + var key = new object(); + object[] parameters = parameter is null ? [] : [parameter]; + + services.AddKeyedSingleton( + typeof(T), + key, + (sp, _) => ActivatorUtilities.CreateInstance(sp, parameters) + ); + + services.AddSingleton(typeof(T), sp => sp.GetRequiredKeyedService(key)); + + foreach (var type in types) + { + if (type != typeof(T)) + { + services.AddSingleton(type, sp => sp.GetRequiredKeyedService(key)); + } + } + + return services; } } /// - /// Helper for registering SilkTouch to a + /// Helper for registering SilkTouch to a /// - public record struct SilkTouchRegistrationContext(ContainerBuilder Builder); + public record struct SilkTouchRegistrationContext(IServiceCollection Services); } From d1b7fa03f9e854078ac43e399ed260523fe09190 Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 10 Nov 2026 01:00:46 -0500 Subject: [PATCH 2/7] Change SilkTouchJob to use MSDI --- sources/SilkTouch/SilkTouch/SilkTouchJob.cs | 40 ++++++++++++--------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs index d98398146f..386acb4754 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs @@ -2,7 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; -using Autofac; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Silk.NET.SilkTouch.Mods; using Silk.NET.SilkTouch.Mods.Transformation; @@ -16,7 +16,7 @@ namespace Silk.NET.SilkTouch; /// internal class SilkTouchJob : IDisposable { - private readonly ILifetimeScope _container; + private readonly ServiceProvider _provider; private readonly ILogger _logger; private readonly IInputResolver _inputResolver; @@ -43,36 +43,42 @@ internal class SilkTouchJob : IDisposable /// /// The job key. /// The job config. - /// The parent container. Should be the one from the . + /// The parent service provider. Should be the one from the . /// - /// The action that configures the dependency container used by this job. + /// The action that configures the services used by this job. /// You'll want to register SilkTouch mods during this action. See . /// public SilkTouchJob( string key, SilkTouchConfiguration config, - ILifetimeScope parentContainer, - Action configure + IServiceProvider parent, + Action configure ) { Key = key; Config = config; - _logger = parentContainer.Resolve>(); - _inputResolver = parentContainer.Resolve(); - _container = parentContainer.BeginLifetimeScope(builder => + var services = new ServiceCollection(); { // Register default dependencies - builder.RegisterType().SingleInstance(); - builder.RegisterType().SingleInstance(); + services.AddSingleton(parent.GetRequiredService()); + services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); + + services.AddSingleton(parent.GetRequiredService()); + + services.AddSingleton(); + services.AddSingleton(); // Register user defined dependencies - configure.Invoke(builder); - }); + configure.Invoke(services); + } - _modContextFactory = _container.Resolve(); - _documentFactory = _container.Resolve(); - Mods = _container.Resolve(); + _provider = services.BuildServiceProvider(); + _logger = _provider.GetRequiredService>(); + _inputResolver = _provider.GetRequiredService(); + _modContextFactory = _provider.GetRequiredService(); + _documentFactory = _provider.GetRequiredService(); + Mods = _provider.GetRequiredService(); } /// @@ -175,5 +181,5 @@ await File.ReadAllTextAsync(Config.DefaultLicenseHeader, ct) } /// - public void Dispose() => _container.Dispose(); + public void Dispose() => _provider.Dispose(); } From 8e9897b50e3f5f16a986107e6123800ecd54b98b Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 10 Nov 2026 01:10:49 -0500 Subject: [PATCH 3/7] Work on translating the registrations --- sources/SilkTouch/SilkTouch/SilkTouchHost.cs | 67 ++++++++++++++++++-- sources/SilkTouch/SilkTouch/SilkTouchJob.cs | 7 -- 2 files changed, 62 insertions(+), 12 deletions(-) diff --git a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs index 526cae7b83..727ab7ab5e 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs @@ -141,12 +141,13 @@ await Parallel.ForEachAsync( using var job = new SilkTouchJob( jobKvp.Key, jobKvp.Value.Config, - container, - builder => + services => { - var services = new ServiceCollection(); + ConfigureForward( + new AutofacServiceProvider(container), + services + ); jobKvp.Value.Configure.Invoke(services.SilkTouch()); - builder.Populate(services); } ); @@ -178,7 +179,8 @@ private static void Exit(int exitCode) => Environment.Exit(exitCode); /// - /// Dependencies defined here will be shared by all jobs. + /// Dependencies defined here are only accessible to the host. + /// Make them available to the jobs by using . /// private void Configure(ContainerBuilder builder, InvocationContext ctx) { @@ -262,6 +264,61 @@ private void Configure(ContainerBuilder builder, InvocationContext ctx) } } + /// + /// Dependencies defined here will be available to jobs. + /// + private void ConfigureForward(IServiceProvider host, IServiceCollection services) + { + // Prefer to match the registration order above + services.AddSingleton(host.GetRequiredService()); + services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); + + services.AddSingleton(host.GetRequiredService()); + + services.AddSingleton(host.GetRequiredService()); + services.AddSingleton(host.GetRequiredService()); + + builder.RegisterType().As().SingleInstance(); + + builder.RegisterType().SingleInstance(); + + builder + .RegisterType() + .AsSelf() + .AsImplementedInterfaces() + .SingleInstance(); + + builder.RegisterType().As().SingleInstance(); + + builder.RegisterType().As().SingleInstance(); + + builder.RegisterType().As().SingleInstance(); + + builder.RegisterType().As().SingleInstance(); + + if (OperatingSystem.IsWindows()) + { + builder + .RegisterType() + .As() + .SingleInstance(); + } + else if (OperatingSystem.IsMacOS()) + { + builder + .RegisterType() + .As() + .SingleInstance(); + } + else + { + builder + .RegisterType() + .As() + .SingleInstance(); + } + } + private record JobDefinition( SilkTouchConfiguration Config, Action Configure diff --git a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs index 386acb4754..58e4734fbf 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs @@ -43,7 +43,6 @@ internal class SilkTouchJob : IDisposable /// /// The job key. /// The job config. - /// The parent service provider. Should be the one from the . /// /// The action that configures the services used by this job. /// You'll want to register SilkTouch mods during this action. See . @@ -51,7 +50,6 @@ internal class SilkTouchJob : IDisposable public SilkTouchJob( string key, SilkTouchConfiguration config, - IServiceProvider parent, Action configure ) { @@ -61,11 +59,6 @@ Action configure var services = new ServiceCollection(); { // Register default dependencies - services.AddSingleton(parent.GetRequiredService()); - services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); - - services.AddSingleton(parent.GetRequiredService()); - services.AddSingleton(); services.AddSingleton(); From 4e64a462ff21a1e97394a657aece57f7b9a62f6a Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 10 Nov 2026 01:21:35 -0500 Subject: [PATCH 4/7] Finish removing Autofac --- Directory.Packages.props | 17 ++- .../SilkTouch/Silk.NET.SilkTouch.csproj | 2 - sources/SilkTouch/SilkTouch/SilkTouchHost.cs | 111 ++++-------------- sources/SilkTouch/SilkTouch/SilkTouchJob.cs | 6 +- 4 files changed, 37 insertions(+), 99 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index ae946f6d17..237a523ec7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -17,12 +17,13 @@ - + - - @@ -34,11 +35,17 @@ - + - + diff --git a/sources/SilkTouch/SilkTouch/Silk.NET.SilkTouch.csproj b/sources/SilkTouch/SilkTouch/Silk.NET.SilkTouch.csproj index afc4f29d09..c5cf2978db 100644 --- a/sources/SilkTouch/SilkTouch/Silk.NET.SilkTouch.csproj +++ b/sources/SilkTouch/SilkTouch/Silk.NET.SilkTouch.csproj @@ -6,8 +6,6 @@ - - diff --git a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs index 727ab7ab5e..6deca44061 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchHost.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchHost.cs @@ -4,8 +4,6 @@ using System.CommandLine; using System.CommandLine.Invocation; using System.Diagnostics.CodeAnalysis; -using Autofac; -using Autofac.Extensions.DependencyInjection; using Microsoft.Build.Locator; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -13,7 +11,6 @@ using Silk.NET.SilkTouch.Caching; using Silk.NET.SilkTouch.Clang; using Silk.NET.SilkTouch.Mods; -using Silk.NET.SilkTouch.Mods.Bakery; using Silk.NET.SilkTouch.Sources; namespace Silk.NET.SilkTouch; @@ -102,10 +99,10 @@ public async Task RunAsync(string[] args) // Register MSBuild MSBuildLocator.RegisterDefaults(); - // Build container - var builder = new ContainerBuilder(); - Configure(builder, ctx); - await using ILifetimeScope container = builder.Build(); + // Build service provider + var services = new ServiceCollection(); + Configure(services, ctx); + await using var provider = services.BuildServiceProvider(); var jobKeysToRun = new HashSet( ctx.ParseResult.GetValueForOption(OnlyOption) ?? [], @@ -127,7 +124,7 @@ public async Task RunAsync(string[] args) return jobKeysToRun.Count == 0 || jobKeysToRun.Contains(job.Key); }); - var logger = container.Resolve>(); + var logger = provider.GetRequiredService>(); try { await Parallel.ForEachAsync( @@ -143,10 +140,7 @@ await Parallel.ForEachAsync( jobKvp.Value.Config, services => { - ConfigureForward( - new AutofacServiceProvider(container), - services - ); + ConfigureForward(provider, services); jobKvp.Value.Configure.Invoke(services.SilkTouch()); } ); @@ -182,9 +176,9 @@ private static void Exit(int exitCode) => /// Dependencies defined here are only accessible to the host. /// Make them available to the jobs by using . /// - private void Configure(ContainerBuilder builder, InvocationContext ctx) + private void Configure(IServiceCollection services, InvocationContext ctx) { - var services = new ServiceCollection() + services .AddLogging(builder => { builder.AddSimpleConsole(opts => @@ -200,8 +194,6 @@ private void Configure(ContainerBuilder builder, InvocationContext ctx) }) .AddOptions(); - builder.Populate(services); - { // Create config var config = new SilkTouchHostConfiguration() @@ -217,49 +209,25 @@ private void Configure(ContainerBuilder builder, InvocationContext ctx) ); } - builder.RegisterInstance(config).SingleInstance(); - - builder - .RegisterType() - .As() - .SingleInstance(); - - builder.RegisterType().SingleInstance(); - - builder - .RegisterType() - .AsSelf() - .AsImplementedInterfaces() - .SingleInstance(); - - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); + services.AddSingleton(config); - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); if (OperatingSystem.IsWindows()) { - builder - .RegisterType() - .As() - .SingleInstance(); + services.AddSingleton(); } else if (OperatingSystem.IsMacOS()) { - builder - .RegisterType() - .As() - .SingleInstance(); + services.AddSingleton(); } else { - builder - .RegisterType() - .As() - .SingleInstance(); + services.AddSingleton(); } } } @@ -273,50 +241,11 @@ private void ConfigureForward(IServiceProvider host, IServiceCollection services services.AddSingleton(host.GetRequiredService()); services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); - services.AddSingleton(host.GetRequiredService()); - services.AddSingleton(host.GetRequiredService()); services.AddSingleton(host.GetRequiredService()); - - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().SingleInstance(); - - builder - .RegisterType() - .AsSelf() - .AsImplementedInterfaces() - .SingleInstance(); - - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); - - builder.RegisterType().As().SingleInstance(); - - if (OperatingSystem.IsWindows()) - { - builder - .RegisterType() - .As() - .SingleInstance(); - } - else if (OperatingSystem.IsMacOS()) - { - builder - .RegisterType() - .As() - .SingleInstance(); - } - else - { - builder - .RegisterType() - .As() - .SingleInstance(); - } + services.AddSingleton(host.GetRequiredService()); + services.AddSingleton(host.GetRequiredService()); + services.AddSingleton(host.GetRequiredService()); } private record JobDefinition( diff --git a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs index 58e4734fbf..5e66213f28 100644 --- a/sources/SilkTouch/SilkTouch/SilkTouchJob.cs +++ b/sources/SilkTouch/SilkTouch/SilkTouchJob.cs @@ -4,7 +4,9 @@ using System.Diagnostics; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using Silk.NET.SilkTouch.Clang; using Silk.NET.SilkTouch.Mods; +using Silk.NET.SilkTouch.Mods.Bakery; using Silk.NET.SilkTouch.Mods.Transformation; using Silk.NET.SilkTouch.Sources; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; @@ -61,6 +63,8 @@ Action configure // Register default dependencies services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton(); // Register user defined dependencies configure.Invoke(services); @@ -71,7 +75,7 @@ Action configure _inputResolver = _provider.GetRequiredService(); _modContextFactory = _provider.GetRequiredService(); _documentFactory = _provider.GetRequiredService(); - Mods = _provider.GetRequiredService(); + Mods = _provider.GetRequiredService>().ToArray(); } /// From 8fc23d51147f28cfc004df7b074a138ca8a93c57 Mon Sep 17 00:00:00 2001 From: William Chen Date: Wed, 11 Nov 2026 00:09:31 -0500 Subject: [PATCH 5/7] Add way for mods to register command line options --- .../SilkTouch/SilkTouch/Clang/ClangScraper.cs | 41 ++- .../CommandLine/ICommandLineConfigurable.cs | 26 ++ sources/SilkTouch/SilkTouch/SilkTouchHost.cs | 235 +++++++++--------- .../SilkTouch/SilkTouchHostConfiguration.cs | 28 --- sources/SilkTouch/SilkTouch/SilkTouchJob.cs | 23 +- 5 files changed, 194 insertions(+), 159 deletions(-) create mode 100644 sources/SilkTouch/SilkTouch/CommandLine/ICommandLineConfigurable.cs delete mode 100644 sources/SilkTouch/SilkTouch/SilkTouchHostConfiguration.cs diff --git a/sources/SilkTouch/SilkTouch/Clang/ClangScraper.cs b/sources/SilkTouch/SilkTouch/Clang/ClangScraper.cs index e9b134734b..d0a935ea98 100644 --- a/sources/SilkTouch/SilkTouch/Clang/ClangScraper.cs +++ b/sources/SilkTouch/SilkTouch/Clang/ClangScraper.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Collections.Concurrent; +using System.CommandLine; +using System.CommandLine.Invocation; using System.Diagnostics; using ClangSharp; using ClangSharp.Interop; @@ -10,6 +12,7 @@ using Microsoft.CodeAnalysis.Text; using Microsoft.Extensions.Logging; using Silk.NET.SilkTouch.Caching; +using Silk.NET.SilkTouch.CommandLine; using Silk.NET.SilkTouch.Mods; using Silk.NET.SilkTouch.Sources; using static ClangSharp.Interop.CXDiagnosticSeverity; @@ -25,7 +28,6 @@ namespace Silk.NET.SilkTouch.Clang; /// /// The document factory to use. /// The configuration to use. -/// The host configuration to use. /// The logger to use. /// The input resolver to use. /// The mods that modify response files before they are fed to ClangSharp. @@ -34,15 +36,29 @@ public sealed class ClangScraper( ResponseFileHandler rspHandler, DocumentFactory documentFactory, ClangScraper.Configuration config, - SilkTouchHostConfiguration hostConfig, ILogger logger, IInputResolver inputResolver, ICacheProvider? cacheProvider = null, IEnumerable? responseFileMods = null -) : IMod +) : IMod, ICommandLineConfigurable { // TODO add Windows SDK and Xcode configuration + private static Option UseCacheOnlyOption { get; } = + new(["--use-cache-only"], () => false) + { + Description = $"Limits {nameof(ClangScraper)} to only using the cache.", + }; + + private static Option GenerateCacheOnlyOption { get; } = + new(["--generate-cache-only"], () => false) + { + Description = $"Limits {nameof(ClangScraper)} to only generating the cache.", + }; + + private bool _useCacheOnly = false; + private bool _generateCacheOnly = false; + /// /// Configuration for the mod. /// @@ -88,12 +104,25 @@ public record Configuration /// Whether this mod should end the generation job after a "generate cache only" run. /// /// - /// A "generate cache only" run is a run where is set to true. /// The intended use pattern is setting this to true for the last mod in a job. /// public bool ExitAfterGenerateCacheOnlyRun { get; init; } } + /// + public void RegisterCommandLineOptions(Action