Skip to content

Commit

Permalink
Improved local emulator
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed May 2, 2024
1 parent fe69ece commit f2074ef
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;

namespace Piral.Blazor.Orchestrator;

Expand Down
19 changes: 7 additions & 12 deletions src/Piral.Blazor.Orchestrator/LocalMicrofrontendPackage.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
using System.Reflection;
using System.Runtime.Loader;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Piral.Blazor.Orchestrator;

internal class LocalMicrofrontendPackage : MicrofrontendPackage
internal class LocalMicrofrontendPackage(string path, JsonObject? config, IModuleContainerService container, IEvents events, IData data) :
MicrofrontendPackage(Path.GetFileNameWithoutExtension(path), "0.0.0", config, container, events, data)
{
private readonly string _path;
private readonly string _path = path;
private readonly List<string> _contentRoots = [];

public LocalMicrofrontendPackage(string path, JsonObject? config, IModuleContainerService container, IEvents events, IData data)
: base(Path.GetFileNameWithoutExtension(path), "0.0.0", config, container, events, data)
protected override Assembly? ResolveAssembly(AssemblyName assemblyName)
{
_path = path;
}

protected override Assembly? LoadMissingAssembly(AssemblyLoadContext _, AssemblyName assemblyName)
{
//TODO
return null;
var dllName = assemblyName.Name;
var basePath = Path.GetDirectoryName(_path)!;
return Context.LoadFromAssemblyPath(Path.Combine(basePath, $"{dllName}.dll"));
}

protected override async Task OnInitialized()
Expand Down
7 changes: 5 additions & 2 deletions src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

namespace Piral.Blazor.Orchestrator;

internal class MicrofrontendLoadContext(string name) : AssemblyLoadContext(name, true)
internal class MicrofrontendLoadContext(string name, Func<AssemblyName, Assembly?> resolve) : AssemblyLoadContext(name, true)
{
private readonly AssemblyLoadContext _root = All.FirstOrDefault(m => m.Name == "root") ?? Default;

public readonly Func<AssemblyName, Assembly?> _resolve = resolve;

protected override Assembly? Load(AssemblyName assemblyName)
{
var existing = GetExisting(_root.Assemblies, assemblyName);
Expand All @@ -29,7 +31,8 @@ internal class MicrofrontendLoadContext(string name) : AssemblyLoadContext(name,
}
}

return base.Load(assemblyName);
// Let's find it via the custom resolve
return _resolve(assemblyName);
}

private static Assembly? GetExisting(IEnumerable<Assembly> assemblies, AssemblyName name)
Expand Down
19 changes: 12 additions & 7 deletions src/Piral.Blazor.Orchestrator/MicrofrontendPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

namespace Piral.Blazor.Orchestrator;

public abstract class MicrofrontendPackage(string name, string version, JsonObject? config, IModuleContainerService container, IEvents events, IData data) : IDisposable
public abstract class MicrofrontendPackage : IDisposable
{
private readonly RelatedMfAppService _app = new(name, version, config, events, data);
private readonly IModuleContainerService _container = container;
private readonly AssemblyLoadContext _context = new MicrofrontendLoadContext($"{name}@{version}");
private readonly RelatedMfAppService _app;
private readonly IModuleContainerService _container;
private readonly MicrofrontendLoadContext _context;
public event EventHandler? PackageChanged;

public MicrofrontendPackage(string name, string version, JsonObject? config, IModuleContainerService container, IEvents events, IData data)
{
_app = new (name, version, config, events, data);
_container = container;
_context = new MicrofrontendLoadContext($"{name}@{version}", ResolveAssembly);
}

private IMfModule? _module;
private bool _disabled = false;
private Assembly? _assembly;
Expand Down Expand Up @@ -69,8 +76,6 @@ public IEnumerable<Type> GetComponents(string name)

public async Task Init()
{
_context.Resolving += LoadMissingAssembly;

await OnInitializing();

var assembly = GetAssembly();
Expand All @@ -85,7 +90,7 @@ public async Task Init()
await OnInitialized();
}

protected abstract Assembly? LoadMissingAssembly(AssemblyLoadContext _, AssemblyName assemblyName);
protected abstract Assembly? ResolveAssembly(AssemblyName assemblyName);

protected virtual Task OnInitializing() => Task.CompletedTask;

Expand Down
6 changes: 3 additions & 3 deletions src/Piral.Blazor.Orchestrator/NugetMicrofrontendPackage.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using NuGet.Frameworks;
using NuGet.Packaging;
using System.Reflection;
using System.Runtime.Loader;
using System.Text.Json.Nodes;

namespace Piral.Blazor.Orchestrator;

internal class NugetMicrofrontendPackage(string name, string version, JsonObject? mfConfig, List<PackageArchiveReader> packages, IPiralConfig config, IModuleContainerService container, IEvents events, IData data) : MicrofrontendPackage(name, version, mfConfig, container, events, data)
internal class NugetMicrofrontendPackage(string name, string version, JsonObject? mfConfig, List<PackageArchiveReader> packages, IPiralConfig config, IModuleContainerService container, IEvents events, IData data) :
MicrofrontendPackage(name, version, mfConfig, container, events, data)
{
private const string target = "net8.0";
private readonly IPiralConfig _config = config;
Expand Down Expand Up @@ -51,7 +51,7 @@ internal class NugetMicrofrontendPackage(string name, string version, JsonObject
return null;
}

protected override Assembly? LoadMissingAssembly(AssemblyLoadContext _, AssemblyName assemblyName)
protected override Assembly? ResolveAssembly(AssemblyName assemblyName)
{
var dll = $"{assemblyName.Name}.dll";
return AddAssemblyToContext(dll);
Expand Down
13 changes: 13 additions & 0 deletions src/Piral.Blazor.Sdk/Emulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public static void Main(string[] args)
};

var ass = root.LoadFromAssemblyPath(path);
var assName = ass.GetName().Name;

AssemblyLoadContext.Default.Resolving += (_, assemblyName) =>
{
// In general we just keep default; but the app shell we also resolve here
if (assemblyName.Name == assName)
{
return ass;
}
return null;
};

var prog = ass.DefinedTypes.First(m => m.Name == "Program");
var method = prog.GetMethods(BindingFlags.Static | BindingFlags.NonPublic).First();

Expand Down

0 comments on commit f2074ef

Please sign in to comment.