Skip to content

Commit

Permalink
Merge pull request #926 from unoplatform/dev/jela/ver-check
Browse files Browse the repository at this point in the history
feat: Add support generic wasm apps
  • Loading branch information
jeromelaban authored Nov 18, 2024
2 parents cb9da7f + a8e6dbf commit 016dd11
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 33 deletions.
1 change: 1 addition & 0 deletions src/Uno.Wasm.VersionChecker/Uno.Wasm.VersionChecker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFrameworks>net9.0</TargetFrameworks>
<Nullable>enable</Nullable>
<RollForward>Major</RollForward>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<InformationalVersion Condition="'$(Configuration)'=='Debug'">0.0.0-local-dev-build-DOT-NOT-PUBLISH</InformationalVersion>

Expand Down
82 changes: 49 additions & 33 deletions src/Uno.Wasm.VersionChecker/UnoVersionExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -28,11 +29,10 @@ internal record UnoConfig(Uri assembliesPath, string? mainAssembly, string[]? as
public record AssemblyDetail(string? name, string version, string fileVersion, string configuration, string targetFramework, string? commit);

private ImmutableArray<AssemblyDetail> _assemblies;
private UnoConfig _unoConfig;
private DotnetConfig _dotnetConfig;
private UnoConfig? _unoConfig;
private DotnetConfig? _dotnetConfig;
private AssemblyDetail? _mainAssemblyDetails;
private (string? version, string? name) _framework;
private bool _isAot;

public UnoVersionExtractor(Uri siteUri)
{
Expand Down Expand Up @@ -66,7 +66,7 @@ public async Task<int> Extract()
var web = new HtmlWeb();
var doc = await web.LoadFromWebAsync(_siteUri, default, default, default);

Console.WriteLine("Trying to find Uno bootstrapper configuration...", Color.Gray);
Console.WriteLine("Trying to find App configuration...", Color.Gray);

var files = doc?.DocumentNode
.SelectNodes("//script")
Expand Down Expand Up @@ -109,52 +109,54 @@ public async Task<int> Extract()
}
}

if (unoConfigPath is null)
{
Console.WriteLine("No Uno / Uno.UI application found.", Color.Red);
return 2;
}

Console.WriteLine("Application found.", Color.LightGreen);

Console.WriteLineFormatted("Configuration url is {0}.", Color.Gray, new Formatter(unoConfigPath, Color.Aqua));
if (unoConfigPath is not null)
{
Console.WriteLineFormatted("Uno configuration url is {0}.", Color.Gray, new Formatter(unoConfigPath, Color.Aqua));
_unoConfig = await GetUnoConfig(unoConfigPath);
}

_unoConfig = await GetUnoConfig(unoConfigPath);
_dotnetConfig = await GetDotnetConfig(dotnetConfigPath);

if (_unoConfig.server is { Length: > 0 })
if (_dotnetConfig.mainAssemblyName is not null)
{
Console.WriteLineFormatted("Dotnet configuration url is {0}.", Color.Gray, new Formatter(dotnetConfigPath, Color.Aqua));
}

if (_unoConfig?.server is { Length: > 0 })
{
Console.WriteLineFormatted(
"Server is {0}",
Color.Gray,
new Formatter(_unoConfig.server, Color.Aqua));
}

if (_unoConfig.mainAssembly is { Length: > 0 })
if (_unoConfig?.mainAssembly is { Length: > 0 })
{
Console.WriteLineFormatted("Starting assembly is {0}.", Color.Gray,
new Formatter(_unoConfig.mainAssembly, Color.Aqua));
}

if (_dotnetConfig.mainAssemblyName is { Length: > 0 })
if (_dotnetConfig?.mainAssemblyName is { Length: > 0 })
{
Console.WriteLineFormatted("Starting assembly is {0}.", Color.Gray,
new Formatter(_dotnetConfig.mainAssemblyName, Color.Aqua));
}

if (
(_unoConfig.assemblies is null || _unoConfig.assemblies is { Length: 0 })
&& _dotnetConfig.assemblies.Length == 0)
(_unoConfig?.assemblies is null || _unoConfig.assemblies is { Length: 0 })
&& _dotnetConfig?.assemblies.Length == 0)
{
Console.WriteLine("No assemblies found.", Color.Red);
return 1;
}

if(_unoConfig.assemblies is { Length: > 0 })
if(_unoConfig?.assemblies is { Length: > 0 })
{
await DumpAssemblies(_unoConfig.assembliesPath, _unoConfig.assemblies);
}
else if(_dotnetConfig.assembliesPath is not null)
else if(_dotnetConfig?.assembliesPath is not null)
{
await DumpAssemblies(_dotnetConfig.assembliesPath, _dotnetConfig.assemblies);
}
Expand Down Expand Up @@ -187,7 +189,7 @@ private async Task DumpAssemblies(Uri basePath, string[] assemblies)

foreach (var assemblyDetail in _assemblies)
{
if (assemblyDetail.name == _unoConfig.mainAssembly)
if (assemblyDetail.name == _unoConfig?.mainAssembly)
{
_mainAssemblyDetails = assemblyDetail;
}
Expand All @@ -199,24 +201,34 @@ private async Task DumpAssemblies(Uri basePath, string[] assemblies)
{
_framework = (assemblyDetail.version, assemblyDetail.targetFramework);
}
else if (assemblyDetail.name == "aot-instances")
{
_isAot = true;
}
}
}

private async Task<Uri?> GetDotnetConfigPath()
{
using var http = new HttpClient();
var dotnetConfig = new Uri(_siteUri, "_framework/blazor.boot.json");

if (await GetFile(http, new(_siteUri, "_framework/blazor.boot.json")) is { } frameworkFile)
{
return frameworkFile;
}
else if (await GetFile(http, new(_siteUri, "blazor.boot.json")) is { } baseFile)
{
return baseFile;
}

return null;
}

private static async Task<Uri?> GetFile(HttpClient http, Uri dotnetConfig)
{
var embeddedResponse = await http.GetAsync(dotnetConfig);

if (embeddedResponse.IsSuccessStatusCode)
{
var response = await embeddedResponse.Content.ReadAsStringAsync();

if(response.StartsWith("{"))
if (response.StartsWith("{"))
{
return dotnetConfig;
}
Expand Down Expand Up @@ -307,9 +319,13 @@ public void OutputResults()
List<string> assemblies = new();

var resources = json.RootElement.GetProperty("resources");

resources.TryGetProperty("coreAssembly", out var coreAssembly);
resources.TryGetProperty("assembly", out var assembly);

var assembliesList =
resources.GetProperty("coreAssembly").EnumerateObject()
.Concat(resources.GetProperty("assembly").EnumerateObject());
(coreAssembly.ValueKind == JsonValueKind.Undefined ? [] : coreAssembly.EnumerateObject())
.Concat(assembly.ValueKind == JsonValueKind.Undefined ? [] : assembly.EnumerateObject());

foreach (var resource in assembliesList)
{
Expand All @@ -319,7 +335,7 @@ public void OutputResults()
}
}

var assembliesPath = new Uri(_siteUri, "_framework/");
var assembliesPath = new Uri(_siteUri, dotnetConfigPath.OriginalString.Contains("_framework") ? "_framework/" : "");

return (mainAssemblyName, globalizationMode, assemblies.ToArray(), assembliesPath);
}
Expand Down Expand Up @@ -410,17 +426,17 @@ private async Task<UnoConfig> GetUnoConfig(Uri uri)
var details = await ReadPE(stream);

stream.Position = 0;
details ??= await ReadWebCIL(stream);
details ??= ReadWebCIL(stream);

return details;
}
catch (Exception ex)
catch (Exception)
{
return null;
}
}

private static async Task<AssemblyDetail?> ReadWebCIL(MemoryStream stream)
private static AssemblyDetail? ReadWebCIL(MemoryStream stream)
{
var asmStream = WebcilConverterUtil.ConvertFromWebcil(stream);

Expand Down Expand Up @@ -496,7 +512,7 @@ private async Task<UnoConfig> GetUnoConfig(Uri uri)
public void Dispose() => _httpClient.Dispose();
}

internal record struct DotnetConfig(string? mainAssemblyName, string? globalizationMode, string[] assemblies, Uri? assembliesPath)
internal record DotnetConfig(string? mainAssemblyName, string? globalizationMode, string[] assemblies, Uri? assembliesPath)
{
public static implicit operator (string? mainAssemblyName, string? globalizationMode, string[] assemblies, Uri? assembliesPath)(DotnetConfig value) => (value.mainAssemblyName, value.globalizationMode, value.assemblies, value.assembliesPath);
public static implicit operator DotnetConfig((string? mainAssemblyName, string? globalizationMode, string[] assemblies, Uri? assembliesPath) value) => new DotnetConfig(value.mainAssemblyName, value.globalizationMode, value.assemblies, value.assembliesPath);
Expand Down

0 comments on commit 016dd11

Please sign in to comment.