Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored markup file loader to be extensible by third-party libraries #1312

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public static IServiceCollection RegisterDotVVMServices(IServiceCollection servi
services.TryAddSingleton<IOutputRenderer, DefaultOutputRenderer>();
services.TryAddSingleton<IDotvvmPresenter, DotvvmPresenter>();
services.TryAddSingleton<IMarkupFileLoader, AggregateMarkupFileLoader>();
services.TryAddSingleton<DefaultMarkupFileLoader>();
services.TryAddSingleton<EmbeddedMarkupFileLoader>();
services.TryAddSingleton<IControlBuilderFactory, DefaultControlBuilderFactory>();
services.TryAddSingleton<IControlResolver, DefaultControlResolver>();
services.TryAddSingleton<IControlTreeResolver, DefaultControlTreeResolver>();
Expand Down
16 changes: 9 additions & 7 deletions src/Framework/Framework/Hosting/AggregateMarkupFileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@
using System.Linq;
using System.Text;
using DotVVM.Framework.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace DotVVM.Framework.Hosting
{
public class AggregateMarkupFileLoader : IMarkupFileLoader
{
public List<IMarkupFileLoader> Loaders { get; private set; } = new List<IMarkupFileLoader>();
private readonly List<IMarkupFileLoader> loaders;

public AggregateMarkupFileLoader()
public AggregateMarkupFileLoader(IOptions<AggregateMarkupFileLoaderOptions> 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.GetRequiredService(p))
.ToList();
}

/// <summary>
/// Gets the markup file for the specified virtual path.
/// </summary>
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;

namespace DotVVM.Framework.Hosting;

public class AggregateMarkupFileLoaderOptions
{
public List<Type> LoaderTypes { get; }

public AggregateMarkupFileLoaderOptions()
{
LoaderTypes = new()
{
// the EmbeddedMarkupFileLoader must be registered before DefaultMarkupFileLoader (which gets wrapped by HotReloadMarkupFileLoader)
typeof(EmbeddedMarkupFileLoader),
typeof(DefaultMarkupFileLoader)
};
}
}
2 changes: 1 addition & 1 deletion src/Framework/Framework/Hosting/MarkupFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ public static void AddHotReload(this IDotvvmServiceCollection services)
services.Services.AddSignalR();

services.Services.AddSingleton<IMarkupFileChangeNotifier, AspNetCoreMarkupFileChangeNotifier>();
services.Services.AddSingleton<IMarkupFileLoader, HotReloadAggregateMarkupFileLoader>();
services.Services.Configure<AggregateMarkupFileLoaderOptions>(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<HotReloadMarkupFileLoader>();

services.Services.Configure<DotvvmConfiguration>(RegisterResources);
services.Services.AddTransient<ResourceManager>(provider =>
Expand Down
22 changes: 0 additions & 22 deletions src/Tools/HotReload/Common/HotReloadAggregateMarkupFileLoader.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ public static class DotvvmServiceCollectionExtensions
public static void AddHotReload(this IDotvvmServiceCollection services, DotvvmHotReloadOptions? options = null)
{
services.Services.AddSingleton<IMarkupFileChangeNotifier, OwinMarkupFileChangeNotifier>();
services.Services.AddSingleton<IMarkupFileLoader, HotReloadAggregateMarkupFileLoader>();
services.Services.Configure<AggregateMarkupFileLoaderOptions>(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<HotReloadMarkupFileLoader>();

services.Services.Configure<DotvvmConfiguration>(config => RegisterResources(config, options ?? new DotvvmHotReloadOptions()));
services.Services.AddTransient<ResourceManager>(provider =>
Expand Down