Skip to content

Commit

Permalink
Improved local config
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed May 1, 2024
1 parent a834b39 commit 7ab676e
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Microsoft.AspNetCore.Http;
using System.IO.Pipelines;
using System.Runtime.Loader;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using NuGet.Versioning;

namespace Piral.Blazor.Orchestrator.Connector;

Expand Down
7 changes: 1 addition & 6 deletions src/Piral.Blazor.Orchestrator/FsNugetSnapshotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@ public FsNugetSnapshotService(INugetService nuget, IConfiguration configuration)
_initialized = false;
}

public Task UpdateMicrofrontends(IEnumerable<string> ids)
public Task UpdateMicrofrontends(IEnumerable<NugetEntryWithConfig> entries)
{
return EnqueueJob(() =>
{
var entries = ids.Select(m =>
{
var (name, version) = m.GetIdentity();
return new NugetEntryWithConfig { Name = name, Version = version };
});
_mfs.Clear();
_mfs.AddRange(entries);
return StoreMicrofrontendsSnapshot(entries);
Expand Down
2 changes: 1 addition & 1 deletion src/Piral.Blazor.Orchestrator/ISnapshotService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ISnapshotService
{
Task<IEnumerable<string>> AvailableMicrofrontends();

Task UpdateMicrofrontends(IEnumerable<string> ids);
Task UpdateMicrofrontends(IEnumerable<NugetEntryWithConfig> entries);

Task<JsonObject?> GetConfig(string id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ public MfDiscoveryLoaderService(IHttpClientFactory client, IMfRepository reposit

private void OnPackagesChanged(object? sender, EventArgs e)
{
var ids = _repository.Packages
.Select(m => new NugetEntry { Name = m.Name, Version = m.Version })
.Select(m => m.MakePackageId());
var entries = _repository.Packages
.Select(m => new NugetEntryWithConfig { Name = m.Name, Version = m.Version, Config = m.Config });

_snapshot.UpdateMicrofrontends(ids);
_snapshot.UpdateMicrofrontends(entries);
}

public async Task LoadMicrofrontends(CancellationToken cancellationToken)
Expand Down
30 changes: 18 additions & 12 deletions src/Piral.Blazor.Orchestrator/Loader/MfLocalLoaderService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace Piral.Blazor.Orchestrator.Loader;
Expand All @@ -19,21 +20,26 @@ public void ConnectMicrofrontends(CancellationToken cancellationToken)

public async Task LoadMicrofrontends(CancellationToken cancellationToken)
{
var ass = Assembly.GetEntryAssembly()!;
var all = (Environment.GetEnvironmentVariable("PIRAL_BLAZOR_ALL_DEBUG_ASSEMBLIES") ?? "").Split(',');
var cfg = new JsonObject();

// set primary
await _repository.SetPackage(new LocalMicrofrontendPackage(ass, cfg, _container, _events, _data));

foreach (var path in all)
{
if (path != ass.Location)
{
// set other
var other = Assembly.LoadFrom(path);
await _repository.SetPackage(new LocalMicrofrontendPackage(other, cfg, _container, _events, _data));
}
var cfg = GetMicrofrontendConfig(path);
await _repository.SetPackage(new LocalMicrofrontendPackage(path, cfg, _container, _events, _data));
}
}

private static JsonObject? GetMicrofrontendConfig(string path)
{
var dir = Path.GetDirectoryName(path)!;
var cfgPath = Path.Combine(dir, "config.json");

if (File.Exists(cfgPath))
{
var text = File.ReadAllText(cfgPath, Encoding.UTF8);
return JsonSerializer.Deserialize<JsonObject?>(text);
}

return null;
}
}
17 changes: 6 additions & 11 deletions src/Piral.Blazor.Orchestrator/LocalMicrofrontendPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ namespace Piral.Blazor.Orchestrator;

internal class LocalMicrofrontendPackage : MicrofrontendPackage
{
private readonly Assembly _assembly;
private readonly string _path;
private readonly List<string> _contentRoots = [];

public LocalMicrofrontendPackage(Assembly assembly, JsonObject? config, IModuleContainerService container, IEvents events, IData data)
: this(assembly, assembly.GetName(), config, container, events, data)
public LocalMicrofrontendPackage(string path, JsonObject? config, IModuleContainerService container, IEvents events, IData data)
: base(Path.GetFileNameWithoutExtension(path), "0.0.0", config, container, events, data)
{
}

private LocalMicrofrontendPackage(Assembly assembly, AssemblyName assemblyName, JsonObject? config, IModuleContainerService container, IEvents events, IData data)
: base(assemblyName.Name!, assemblyName.Version!.ToString(), config, container, events, data)
{
_assembly = assembly;
_path = path;
}

protected override Assembly? LoadMissingAssembly(AssemblyLoadContext _, AssemblyName assemblyName)
Expand All @@ -29,13 +24,13 @@ private LocalMicrofrontendPackage(Assembly assembly, AssemblyName assemblyName,

protected override async Task OnInitialized()
{
var infos = Path.ChangeExtension(_assembly.Location, ".staticwebassets.runtime.json");
var infos = Path.ChangeExtension(_path, ".staticwebassets.runtime.json");
using var fs = File.OpenRead(infos);
var assets = await JsonSerializer.DeserializeAsync<StaticWebAssets>(fs);
_contentRoots.AddRange(assets?.ContentRoots ?? Enumerable.Empty<string>());
}

protected override Assembly? GetAssembly() => Context.LoadFromAssemblyPath(_assembly.Location);
protected override Assembly? GetAssembly() => Context.LoadFromAssemblyPath(_path);

public override Task<Stream?> GetFile(string path)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Piral.Blazor.Orchestrator/MicrofrontendLoadContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Reflection;
using System.Runtime.Loader;

namespace Piral.Blazor.Orchestrator;

internal class MicrofrontendLoadContext(string name) : AssemblyLoadContext(name, true)
{
protected override Assembly? Load(AssemblyName assemblyName)
{
//string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
//if (assemblyPath != null)
//{
// return LoadFromAssemblyPath(assemblyPath);
//}
//return null;
return base.Load(assemblyName);
}
}
2 changes: 1 addition & 1 deletion src/Piral.Blazor.Orchestrator/MicrofrontendPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public abstract class MicrofrontendPackage(string name, string version, JsonObje
{
private readonly RelatedMfAppService _app = new(name, version, config, events, data);
private readonly IModuleContainerService _container = container;
private readonly AssemblyLoadContext _context = new ($"{name}@{version}", true);
private readonly AssemblyLoadContext _context = new MicrofrontendLoadContext($"{name}@{version}");
public event EventHandler? PackageChanged;

private IMfModule? _module;
Expand Down

0 comments on commit 7ab676e

Please sign in to comment.