Skip to content

Commit

Permalink
More API cleanups, make StaticAssetsDataSource internal
Browse files Browse the repository at this point in the history
  • Loading branch information
javiercn committed May 10, 2024
1 parent 19d4407 commit f91ac86
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/Components/Components.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"src\\Components\\WebAssembly\\Authentication.Msal\\src\\Microsoft.Authentication.WebAssembly.Msal.csproj",
"src\\Components\\WebAssembly\\DevServer\\src\\Microsoft.AspNetCore.Components.WebAssembly.DevServer.csproj",
"src\\Components\\WebAssembly\\JSInterop\\src\\Microsoft.JSInterop.WebAssembly.csproj",
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Client\\HostedBlazorWebassemblyApp.Client.csproj",
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Server\\HostedBlazorWebassemblyApp.Server.csproj",
"src\\Components\\WebAssembly\\Samples\\HostedBlazorWebassemblyApp\\Shared\\HostedBlazorWebassemblyApp.Shared.csproj",
"src\\Components\\WebAssembly\\Server\\src\\Microsoft.AspNetCore.Components.WebAssembly.Server.csproj",
"src\\Components\\WebAssembly\\Server\\test\\Microsoft.AspNetCore.Components.WebAssembly.Server.Tests.csproj",
"src\\Components\\WebAssembly\\WebAssembly.Authentication\\src\\Microsoft.AspNetCore.Components.WebAssembly.Authentication.csproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapRazorPages();
endpoints.MapControllers();
endpoints.MapStaticAssetEndpoints();
endpoints.MapStaticAssets();
endpoints.MapFallbackToPage("/_Host");
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Linq;
using Microsoft.AspNetCore.Components.Endpoints;
using Microsoft.AspNetCore.Components.Endpoints.Infrastructure;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Server;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.StaticAssets;
using Microsoft.AspNetCore.StaticAssets.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Linq;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Builder;

/// <summary>
/// Web assembly specific endpoint conventions for razor component applications.
/// </summary>
public static class WebAssemblyRazorComponentsEndpointConventionBuilderExtensions
public static partial class WebAssemblyRazorComponentsEndpointConventionBuilderExtensions
{
/// <summary>
/// Configures the application to support the <see cref="RenderMode.InteractiveWebAssembly"/> render mode.
Expand Down Expand Up @@ -55,16 +56,26 @@ public static class WebAssemblyRazorComponentsEndpointConventionBuilderExtension

// If the static assets data source for the given manifest name is already added, then just wire-up the Blazor WebAssembly conventions.
// MapStaticWebAssetEndpoints is idempotent and will not add the data source if it already exists.
var staticAssetsManifestPath = options.StaticAssetsManifestPath ?? Path.Combine(AppContext.BaseDirectory, $"{environment.ApplicationName}.staticwebassets.endpoints.json");
staticAssetsManifestPath = Path.IsPathRooted(staticAssetsManifestPath) ? staticAssetsManifestPath : Path.Combine(AppContext.BaseDirectory, staticAssetsManifestPath);
if (HasStaticAssetDataSource(endpointBuilder, staticAssetsManifestPath))
if (HasStaticAssetDataSource(endpointBuilder, options.StaticAssetsManifestPath))
{
options.ConventionsApplied = true;
endpointBuilder.MapStaticAssets(staticAssetsManifestPath)
endpointBuilder.MapStaticAssets(options.StaticAssetsManifestPath)
.AddBlazorWebAssemblyConventions();

return builder;
}
else if (environment.IsDevelopment())
{
var logger = endpointBuilder.ServiceProvider.GetRequiredService<ILogger<WebAssemblyComponentsEndpointOptions>>();
if (options.StaticAssetsManifestPath is null)
{
Log.StaticAssetsMappingNotFoundForDefaultManifest(logger);
}
else
{
Log.StaticAssetsMappingNotFoundWithManifest(logger, options.StaticAssetsManifestPath);
}
}

return builder;
}
Expand All @@ -73,13 +84,21 @@ private static bool HasStaticAssetDataSource(IEndpointRouteBuilder endpointRoute
{
foreach (var ds in endpointRouteBuilder.DataSources)
{
if (ds is StaticAssetsEndpointDataSource staticAssetsDataSource &&
string.Equals(Path.GetFileName(staticAssetsDataSource.ManifestName), staticAssetsManifestName, StringComparison.OrdinalIgnoreCase))
if (StaticAssetsEndpointDataSourceHelper.IsStaticAssetsDataSource(ds, staticAssetsManifestName))
{
return true;
}
}

return false;
}

internal static partial class Log
{
[LoggerMessage(1, LogLevel.Warning, $$"""Mapped static asset endpoints not found. Ensure '{{nameof(StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets)}}' is called before '{{nameof(AddInteractiveWebAssemblyRenderMode)}}'.""")]
internal static partial void StaticAssetsMappingNotFoundForDefaultManifest(ILogger logger);

[LoggerMessage(2, LogLevel.Warning, $$"""Mapped static asset endpoints not found for manifest '{ManifestPath}'. Ensure '{{nameof(StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets)}}'(staticAssetsManifestPath) is called before '{{nameof(AddInteractiveWebAssemblyRenderMode)}}' and that both manifest paths are the same.""")]
internal static partial void StaticAssetsMappingNotFoundWithManifest(ILogger logger, string manifestPath);
}
}
4 changes: 0 additions & 4 deletions src/Http/Routing/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ Microsoft.AspNetCore.Routing.ContentEncodingMetadata
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.ContentEncodingMetadata(string! value, double quality) -> void
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.Quality.get -> double
Microsoft.AspNetCore.Routing.ContentEncodingMetadata.Value.get -> string!
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.ContentEncodingMetadata(string! value, double quality) -> void
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.Quality.get -> double
Microsoft.AspNetCore.Routing.Matching.ContentEncodingMetadata.Value.get -> string!
static Microsoft.AspNetCore.Routing.RouteHandlerServices.Map(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Delegate! handler, System.Collections.Generic.IEnumerable<string!>? httpMethods, System.Func<System.Reflection.MethodInfo!, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions?, Microsoft.AspNetCore.Http.RequestDelegateMetadataResult!>! populateMetadata, System.Func<System.Delegate!, Microsoft.AspNetCore.Http.RequestDelegateFactoryOptions!, Microsoft.AspNetCore.Http.RequestDelegateMetadataResult?, Microsoft.AspNetCore.Http.RequestDelegateResult!>! createRequestDelegate, System.Reflection.MethodInfo! methodInfo) -> Microsoft.AspNetCore.Builder.RouteHandlerBuilder!
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.StaticAssets.Infrastructure;

/// <summary>
/// This type is not recommended for use outside of ASP.NET Core.
/// </summary>
public static class StaticAssetsEndpointDataSourceHelper
{
/// <summary>
/// This method is not recommended for use outside of ASP.NET Core.
/// </summary>
/// <param name="dataSource"></param>
/// <param name="staticAssetsManifestPath"></param>

public static bool IsStaticAssetsDataSource(EndpointDataSource dataSource, string? staticAssetsManifestPath = null)
{
if (dataSource is StaticAssetsEndpointDataSource staticAssetsDataSource)
{
if (staticAssetsManifestPath is null)
{
var serviceProvider = staticAssetsDataSource.ServiceProvider;
var environment = serviceProvider.GetRequiredService<IWebHostEnvironment>();
staticAssetsManifestPath = Path.Combine(AppContext.BaseDirectory, $"{environment.ApplicationName}.staticwebassets.endpoints.json");
}

staticAssetsManifestPath = Path.IsPathRooted(staticAssetsManifestPath) ? staticAssetsManifestPath : Path.Combine(AppContext.BaseDirectory, staticAssetsManifestPath);

return string.Equals(staticAssetsDataSource.ManifestPath, staticAssetsManifestPath, StringComparison.Ordinal);
}

return false;
}
}
11 changes: 4 additions & 7 deletions src/StaticAssets/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions
Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions
Microsoft.AspNetCore.StaticAssets.Infrastructure.StaticAssetsEndpointDataSourceHelper
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder.Add(System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder!>! convention) -> void
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder.Finally(System.Action<Microsoft.AspNetCore.Builder.EndpointBuilder!>! convention) -> void
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.DefaultBuilder.get -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.ManifestName.get -> string!
override Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.Endpoints.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Http.Endpoint!>!
override Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointDataSource.GetChangeToken() -> Microsoft.Extensions.Primitives.IChangeToken!
static Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string? staticAssetsManifestPath = null) -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
static Microsoft.AspNetCore.Builder.StaticAssetsEndpointRouteBuilderExtensions.MapStaticAssets(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string? staticAssetsManifestPath = null) -> Microsoft.AspNetCore.StaticAssets.StaticAssetsEndpointConventionBuilder!
static Microsoft.AspNetCore.StaticAssets.Infrastructure.StaticAssetsEndpointDataSourceHelper.IsStaticAssetsDataSource(Microsoft.AspNetCore.Routing.EndpointDataSource! dataSource, string? staticAssetsManifestPath = null) -> bool
17 changes: 11 additions & 6 deletions src/StaticAssets/src/StaticAssetEndpointDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ namespace Microsoft.AspNetCore.StaticAssets;
/// An <see cref="EndpointDataSource"/> for static assets.
/// </summary>
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
public class StaticAssetsEndpointDataSource : EndpointDataSource
internal class StaticAssetsEndpointDataSource : EndpointDataSource
{
private readonly object _lock = new();
private readonly StaticAssetsManifest _manifest;
private readonly string _manifestName;
private readonly StaticAssetEndpointFactory _endpointFactory;
private readonly List<Action<EndpointBuilder>> _conventions = [];
private readonly List<Action<EndpointBuilder>> _finallyConventions = [];
private List<Endpoint> _endpoints = null!;
private CancellationTokenSource _cancellationTokenSource;
private CancellationChangeToken _changeToken;

internal StaticAssetsEndpointDataSource(StaticAssetsManifest manifest, StaticAssetEndpointFactory endpointFactory, string manifestName, List<StaticAssetDescriptor> descriptors)
internal StaticAssetsEndpointDataSource(IServiceProvider serviceProvider, StaticAssetsManifest manifest, StaticAssetEndpointFactory endpointFactory, string manifestName, List<StaticAssetDescriptor> descriptors)
{
ServiceProvider = serviceProvider;
_manifest = manifest;
_manifestName = manifestName;
ManifestPath = manifestName;
_endpointFactory = endpointFactory;
_cancellationTokenSource = new CancellationTokenSource();
_changeToken = new CancellationChangeToken(_cancellationTokenSource.Token);
Expand All @@ -43,7 +43,7 @@ internal StaticAssetsEndpointDataSource(StaticAssetsManifest manifest, StaticAss
/// <summary>
/// Gets the manifest name associated with this static asset endpoint data source.
/// </summary>
public string ManifestName => _manifestName;
public string ManifestPath { get; }

/// <inheritdoc />
internal StaticAssetsEndpointConventionBuilder DefaultBuilder { get; set; }
Expand All @@ -67,6 +67,8 @@ public override IReadOnlyList<Endpoint> Endpoints
}
}

internal IServiceProvider ServiceProvider { get; }

private void Initialize()
{
if (_endpoints == null)
Expand Down Expand Up @@ -110,5 +112,8 @@ public override IChangeToken GetChangeToken()
return _changeToken;
}

private string GetDebuggerDisplay() => _manifestName;
private string GetDebuggerDisplay()
{
return ManifestPath;
}
}
1 change: 0 additions & 1 deletion src/StaticAssets/src/StaticAssetEndpointFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.AspNetCore.Routing.Patterns;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static StaticAssetsEndpointConventionBuilder MapStaticAssets(this IEndpoi
{
foreach (var ds in endpoints.DataSources)
{
if (ds is StaticAssetsEndpointDataSource sads && sads.ManifestName.Equals(manifestPath, StringComparison.Ordinal))
if (ds is StaticAssetsEndpointDataSource sads && sads.ManifestPath.Equals(manifestPath, StringComparison.Ordinal))
{
return sads.DefaultBuilder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/StaticAssets/src/StaticAssetsManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal static StaticAssetsManifest Parse(string manifestPath)

internal StaticAssetsEndpointDataSource CreateDataSource(IEndpointRouteBuilder endpoints, string manifestName, List<StaticAssetDescriptor> descriptors)
{
var dataSource = new StaticAssetsEndpointDataSource(this, new StaticAssetEndpointFactory(endpoints.ServiceProvider), manifestName, descriptors);
var dataSource = new StaticAssetsEndpointDataSource(endpoints.ServiceProvider, this, new StaticAssetEndpointFactory(endpoints.ServiceProvider), manifestName, descriptors);
endpoints.DataSources.Add(dataSource);
return dataSource;
}
Expand Down

0 comments on commit f91ac86

Please sign in to comment.