From caacace7cea0706e4da75bd287aaabf3d92af2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herceg?= Date: Sun, 27 Feb 2022 16:22:49 +0100 Subject: [PATCH 1/2] Refactored markup file loader to be extensible by third-party libraries --- .../DotVVMServiceCollectionExtensions.cs | 2 ++ .../Hosting/AggregateMarkupFileLoader.cs | 15 +++++++------ .../AggregateMarkupFileLoaderOptions.cs | 19 ++++++++++++++++ src/Framework/Framework/Hosting/MarkupFile.cs | 2 +- .../DotvvmServiceCollectionExtensions.cs | 12 +++++++++- .../HotReloadAggregateMarkupFileLoader.cs | 22 ------------------- .../DotvvmServiceCollectionExtensions.cs | 11 +++++++++- 7 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 src/Framework/Framework/Hosting/AggregateMarkupFileLoaderOptions.cs delete mode 100644 src/Tools/HotReload/Common/HotReloadAggregateMarkupFileLoader.cs diff --git a/src/Framework/Framework/DependencyInjection/DotVVMServiceCollectionExtensions.cs b/src/Framework/Framework/DependencyInjection/DotVVMServiceCollectionExtensions.cs index 3df18d2186..61eaafed55 100644 --- a/src/Framework/Framework/DependencyInjection/DotVVMServiceCollectionExtensions.cs +++ b/src/Framework/Framework/DependencyInjection/DotVVMServiceCollectionExtensions.cs @@ -63,6 +63,8 @@ public static IServiceCollection RegisterDotVVMServices(IServiceCollection servi services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); + services.TryAddSingleton(); + services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); diff --git a/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs b/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs index 0c25033cb8..9015cea941 100644 --- a/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs +++ b/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs @@ -3,18 +3,19 @@ using System.Linq; using System.Text; using DotVVM.Framework.Configuration; +using Microsoft.Extensions.Options; namespace DotVVM.Framework.Hosting { public class AggregateMarkupFileLoader : IMarkupFileLoader { - public List Loaders { get; private set; } = new List(); + private readonly List loaders; - public AggregateMarkupFileLoader() + public AggregateMarkupFileLoader(IOptions options, IServiceProvider serviceProvider) { - // the EmbeddedMarkupFileLoader must be registered before DefaultMarkupFileLoader (which gets wrapped by HotReloadMarkupFileLoader) - Loaders.Add(new EmbeddedMarkupFileLoader()); - Loaders.Add(new DefaultMarkupFileLoader()); + loaders = options.Value.LoaderTypes + .Select(p => (IMarkupFileLoader)serviceProvider.GetService(p)) + .ToList(); } /// @@ -22,9 +23,9 @@ public AggregateMarkupFileLoader() /// public MarkupFile? GetMarkup(DotvvmConfiguration configuration, string virtualPath) { - for (int i = 0; i < Loaders.Count; i++) + for (var i = 0; i < loaders.Count; i++) { - var result = Loaders[i].GetMarkup(configuration, virtualPath); + var result = loaders[i].GetMarkup(configuration, virtualPath); if (result != null) { return result; diff --git a/src/Framework/Framework/Hosting/AggregateMarkupFileLoaderOptions.cs b/src/Framework/Framework/Hosting/AggregateMarkupFileLoaderOptions.cs new file mode 100644 index 0000000000..31e9444d9f --- /dev/null +++ b/src/Framework/Framework/Hosting/AggregateMarkupFileLoaderOptions.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; + +namespace DotVVM.Framework.Hosting; + +public class AggregateMarkupFileLoaderOptions +{ + public List LoaderTypes { get; } + + public AggregateMarkupFileLoaderOptions() + { + LoaderTypes = new() + { + // the EmbeddedMarkupFileLoader must be registered before DefaultMarkupFileLoader (which gets wrapped by HotReloadMarkupFileLoader) + typeof(EmbeddedMarkupFileLoader), + typeof(DefaultMarkupFileLoader) + }; + } +} diff --git a/src/Framework/Framework/Hosting/MarkupFile.cs b/src/Framework/Framework/Hosting/MarkupFile.cs index 8821a97fe9..8dc82c8d58 100644 --- a/src/Framework/Framework/Hosting/MarkupFile.cs +++ b/src/Framework/Framework/Hosting/MarkupFile.cs @@ -67,7 +67,7 @@ public MarkupFile(string fileName, string fullPath) }; } - internal MarkupFile(string fileName, string fullPath, string contents) + public MarkupFile(string fileName, string fullPath, string contents) { FileName = fileName; FullPath = fullPath; diff --git a/src/Tools/HotReload/AspNetCore/Configuration/DotvvmServiceCollectionExtensions.cs b/src/Tools/HotReload/AspNetCore/Configuration/DotvvmServiceCollectionExtensions.cs index eae9fc2968..49d77e53da 100644 --- a/src/Tools/HotReload/AspNetCore/Configuration/DotvvmServiceCollectionExtensions.cs +++ b/src/Tools/HotReload/AspNetCore/Configuration/DotvvmServiceCollectionExtensions.cs @@ -20,7 +20,17 @@ public static void AddHotReload(this IDotvvmServiceCollection services) services.Services.AddSignalR(); services.Services.AddSingleton(); - services.Services.AddSingleton(); + services.Services.Configure(options => + { + var index = options.LoaderTypes.FindIndex(l => l == typeof(DefaultMarkupFileLoader)); + if (index < 0) + { + throw new InvalidOperationException("DotVVM Hot reload could not be initialized - the DefaultMarkupLoader was not found in the AggregateMarkupFileLoader Loaders collection."); + } + + options.LoaderTypes[index] = typeof(HotReloadMarkupFileLoader); + }); + services.Services.AddSingleton(); services.Services.Configure(RegisterResources); services.Services.AddTransient(provider => diff --git a/src/Tools/HotReload/Common/HotReloadAggregateMarkupFileLoader.cs b/src/Tools/HotReload/Common/HotReloadAggregateMarkupFileLoader.cs deleted file mode 100644 index 968b88cde8..0000000000 --- a/src/Tools/HotReload/Common/HotReloadAggregateMarkupFileLoader.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using DotVVM.Framework.Configuration; -using DotVVM.Framework.Hosting; - -namespace DotVVM.HotReload -{ - public class HotReloadAggregateMarkupFileLoader : AggregateMarkupFileLoader - { - - public HotReloadAggregateMarkupFileLoader(IMarkupFileChangeNotifier notifier, DotvvmConfiguration configuration) - { - var index = Loaders.FindIndex(l => l is DefaultMarkupFileLoader); - if (index < 0) - { - throw new InvalidOperationException("DotVVM Hot reload could not be initialized - the DefaultMarkupLoader was not found in the AggregateMarkupFileLoader Loaders collection."); - } - var defaultLoader = (DefaultMarkupFileLoader)Loaders[index]; - Loaders[index] = new HotReloadMarkupFileLoader(defaultLoader, notifier, configuration); - } - - } -} diff --git a/src/Tools/HotReload/Owin/Configuration/DotvvmServiceCollectionExtensions.cs b/src/Tools/HotReload/Owin/Configuration/DotvvmServiceCollectionExtensions.cs index 1fe53a139f..b6942ec7ca 100644 --- a/src/Tools/HotReload/Owin/Configuration/DotvvmServiceCollectionExtensions.cs +++ b/src/Tools/HotReload/Owin/Configuration/DotvvmServiceCollectionExtensions.cs @@ -19,7 +19,16 @@ public static class DotvvmServiceCollectionExtensions public static void AddHotReload(this IDotvvmServiceCollection services, DotvvmHotReloadOptions? options = null) { services.Services.AddSingleton(); - services.Services.AddSingleton(); + services.Services.Configure(options => { + var index = options.LoaderTypes.FindIndex(l => l == typeof(DefaultMarkupFileLoader)); + if (index < 0) + { + throw new InvalidOperationException("DotVVM Hot reload could not be initialized - the DefaultMarkupLoader was not found in the AggregateMarkupFileLoader Loaders collection."); + } + + options.LoaderTypes[index] = typeof(HotReloadMarkupFileLoader); + }); + services.Services.AddSingleton(); services.Services.Configure(config => RegisterResources(config, options ?? new DotvvmHotReloadOptions())); services.Services.AddTransient(provider => From 4da0bf287ca041ea027e87f1c82e2c78ba2b67fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Herceg?= Date: Sun, 27 Feb 2022 16:46:05 +0100 Subject: [PATCH 2/2] Fixed null-check compile error --- src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs b/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs index 9015cea941..c7b2f1be80 100644 --- a/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs +++ b/src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using DotVVM.Framework.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; namespace DotVVM.Framework.Hosting @@ -14,7 +15,7 @@ public class AggregateMarkupFileLoader : IMarkupFileLoader public AggregateMarkupFileLoader(IOptions options, IServiceProvider serviceProvider) { loaders = options.Value.LoaderTypes - .Select(p => (IMarkupFileLoader)serviceProvider.GetService(p)) + .Select(p => (IMarkupFileLoader)serviceProvider.GetRequiredService(p)) .ToList(); }