Skip to content

Commit

Permalink
Add AOT and trim analyzers (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyrrrz authored May 30, 2024
1 parent 1f45336 commit 11c3602
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/Passwordless.AspNetCore/IdentityBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public static class IdentityBuilderExtensions
/// <param name="services">The <see cref="IServiceCollection" />.</param>
/// <param name="configure">Configures the <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUser>(this IServiceCollection services, Action<PasswordlessAspNetCoreOptions> configure)
where TUser : class, new()
{
Expand All @@ -36,12 +38,16 @@ public static IServiceCollection AddPasswordless<TUser>(this IServiceCollection
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configure">Configures the <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IdentityBuilder AddPasswordless(this IdentityBuilder builder, Action<PasswordlessAspNetCoreOptions> configure)
{
builder.Services.AddPasswordlessCore(builder.UserType, configure, IdentityConstants.ApplicationScheme);
return builder;
}

[RequiresDynamicCode("Calls Microsoft.Extensions.DependencyInjection.IdentityBuilderExtensions.AddShared(Type, OptionsBuilder<PasswordlessAspNetCoreOptions>, String)")]
[RequiresUnreferencedCode("Calls Microsoft.Extensions.DependencyInjection.IdentityBuilderExtensions.AddShared(Type, OptionsBuilder<PasswordlessAspNetCoreOptions>, String)")]
private static IServiceCollection AddPasswordlessCore(this IServiceCollection services,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type userType,
Action<PasswordlessAspNetCoreOptions> configure,
Expand Down Expand Up @@ -71,6 +77,8 @@ private static IServiceCollection AddPasswordlessCore(this IServiceCollection se
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configuration">The <see cref="IConfiguration" /> to use to bind to <see cref="PasswordlessAspNetCoreOptions" />. Generally it's own section.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless(this IdentityBuilder builder, IConfiguration configuration)
{
return builder.AddPasswordless(configuration, builder.UserType, IdentityConstants.ApplicationScheme);
Expand All @@ -82,11 +90,15 @@ public static IServiceCollection AddPasswordless(this IdentityBuilder builder, I
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="configuration">The <see cref="IConfiguration" /> to use to bind to <see cref="PasswordlessAspNetCoreOptions" />. Generally it's own section.</param>
/// <returns>The <see cref="IdentityBuilder" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUserType>(this IdentityBuilder builder, IConfiguration configuration)
{
return builder.AddPasswordless(configuration, typeof(TUserType), null);
}

[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
private static IServiceCollection AddPasswordless(this IdentityBuilder builder, IConfiguration configuration, Type? userType, string? defaultScheme)
{
var optionsBuilder = builder.Services
Expand All @@ -104,6 +116,8 @@ private static IServiceCollection AddPasswordless(this IdentityBuilder builder,
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="path">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless(this IdentityBuilder builder, string path)
{
return builder.AddPasswordless(path, builder.UserType, IdentityConstants.ApplicationScheme);
Expand All @@ -115,11 +129,15 @@ public static IServiceCollection AddPasswordless(this IdentityBuilder builder, s
/// <param name="builder">The current <see cref="IdentityBuilder" /> instance.</param>
/// <param name="path">The configuration path to use to bind to <see cref="PasswordlessAspNetCoreOptions" />.</param>
/// <returns>The <see cref="IServiceCollection" />.</returns>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static IServiceCollection AddPasswordless<TUserType>(this IdentityBuilder builder, string path)
{
return builder.AddPasswordless(path, typeof(TUserType), null);
}

[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
private static IServiceCollection AddPasswordless(this IdentityBuilder builder, string path, Type userType, string? defaultScheme)
{
var optionsBuilder = builder.Services
Expand All @@ -131,6 +149,8 @@ private static IServiceCollection AddPasswordless(this IdentityBuilder builder,
return builder.Services.AddShared(userType ?? builder.UserType, optionsBuilder, IdentityConstants.ApplicationScheme);
}

[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
private static IServiceCollection AddShared(this IServiceCollection services,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Type userType,
Expand Down
3 changes: 2 additions & 1 deletion src/Passwordless.AspNetCore/Passwordless.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>true</IsPackable>
<PublishAot Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '8.0'))">true</PublishAot>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<!-- Packaging-related properties -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,8 +16,10 @@ namespace Passwordless.AspNetCore;
public static class PasswordlessApiEndpointRouteBuilderExtensions
{
/// <summary>
///
///
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static PasswordlessEndpointConventionBuilder MapPasswordless(this IEndpointRouteBuilder endpoints, bool enableRegisterEndpoint = false)
{
// TODO: When a custom register body isn't passed in, we can make a reasonable assumption
Expand All @@ -26,24 +29,30 @@ public static PasswordlessEndpointConventionBuilder MapPasswordless(this IEndpoi
}

/// <summary>
///
///
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static PasswordlessEndpointConventionBuilder MapPasswordless(this IEndpointRouteBuilder endpoints, PasswordlessEndpointOptions endpointOptions)
{
return endpoints.MapPasswordless<PasswordlessRegisterRequest>(endpointOptions);
}

/// <summary>
///
///
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static PasswordlessEndpointConventionBuilder MapPasswordless<TRegisterBody>(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapPasswordless<PasswordlessRegisterRequest>(new PasswordlessEndpointOptions(enableRegisterEndpoint: true));
}

/// <summary>
///
///
/// </summary>
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
public static PasswordlessEndpointConventionBuilder MapPasswordless<TRegisterBody>(this IEndpointRouteBuilder endpoints, PasswordlessEndpointOptions endpointOptions)
{
var routeGroup = endpoints
Expand Down
2 changes: 2 additions & 0 deletions src/Passwordless/Passwordless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<TargetFrameworks>net462;net6.0;net7.0;netstandard2.0</TargetFrameworks>
<IsPackable>true</IsPackable>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<!-- Packaging-related properties -->
Expand Down
14 changes: 13 additions & 1 deletion src/Passwordless/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Passwordless;
Expand Down Expand Up @@ -31,6 +32,12 @@ public static IServiceCollection AddPasswordlessSdk(
/// <summary>
/// Adds and configures Passwordless-related services.
/// </summary>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
#endif
#if NET7_0_OR_GREATER
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
#endif
public static IServiceCollection AddPasswordlessSdk(
this IServiceCollection services,
IConfiguration configuration)
Expand All @@ -44,13 +51,18 @@ public static IServiceCollection AddPasswordlessSdk(
return services;
}


/// <summary>
/// Adds and configures Passwordless-related services.
/// </summary>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns></returns>
#if NET6_0_OR_GREATER
[RequiresUnreferencedCode("This method is incompatible with assembly trimming.")]
#endif
#if NET7_0_OR_GREATER
[RequiresDynamicCode("This method is incompatible with native AOT compilation.")]
#endif
public static IServiceCollection AddPasswordlessSdk(
this IServiceCollection services,
string section)
Expand Down

0 comments on commit 11c3602

Please sign in to comment.