Skip to content

Commit

Permalink
Uses Polysharp for AOT related attributes
Browse files Browse the repository at this point in the history
Adds polysharp for AOT attributes to reduce #IF statements. Also removes Nullable polyfill as polysharp provides that functionality too.
  • Loading branch information
phil-scott-78 committed Apr 7, 2024
1 parent ffb782b commit 6dc5a59
Show file tree
Hide file tree
Showing 37 changed files with 110 additions and 235 deletions.
5 changes: 4 additions & 1 deletion examples/Cli/Injection/Infrastructure/TypeRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public ITypeResolver Build()
return new TypeResolver(_builder.BuildServiceProvider());
}

public void Register(Type service, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type implementation)
public void Register(
Type service,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type implementation
)
{
_builder.AddSingleton(service, implementation);
}
Expand Down
18 changes: 2 additions & 16 deletions src/Spectre.Console.Cli/CommandApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public void Configure(Action<IConfigurator> configuration)
/// </summary>
/// <typeparam name="TCommand">The command type.</typeparam>
/// <returns>A <see cref="DefaultCommandConfigurator"/> that can be used to configure the default command.</returns>
public DefaultCommandConfigurator SetDefaultCommand<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TCommand
>()
public DefaultCommandConfigurator SetDefaultCommand<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TCommand>()
where TCommand : class, ICommand
{
return new DefaultCommandConfigurator(GetConfigurator().SetDefaultCommand<TCommand>());
Expand All @@ -68,12 +63,11 @@ public int Run(IEnumerable<string> args)
/// </summary>
/// <param name="args">The arguments.</param>
/// <returns>The exit code from the executed command.</returns>
#if NET6_0_OR_GREATER
// we have a handful of helper classes that we create dynamically, make sure we mark them
// as dynamic dependencies to force their inclusion.
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(FlagValue<>))]
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(EmptyCommandSettings))]
#endif
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(MultiMap<,>))]
public async Task<int> RunAsync(IEnumerable<string> args)
{
try
Expand All @@ -84,17 +78,9 @@ public async Task<int> RunAsync(IEnumerable<string> args)
_configurator.AddBranch(CliConstants.Commands.Branch, cli =>
{
cli.HideBranch();

// need to explicitly add the settings for NET6 or greater to support trimming
#if NET6_0_OR_GREATER
cli.AddCommand<VersionCommand, VersionCommand.Settings>(CliConstants.Commands.Version);
cli.AddCommand<XmlDocCommand, XmlDocCommand.Settings>(CliConstants.Commands.XmlDoc);
cli.AddCommand<ExplainCommand, ExplainCommand.Settings>(CliConstants.Commands.Explain);
#else
cli.AddCommand<VersionCommand>(CliConstants.Commands.Version);
cli.AddCommand<XmlDocCommand>(CliConstants.Commands.XmlDoc);
cli.AddCommand<ExplainCommand>(CliConstants.Commands.Explain);
#endif
});

_executed = true;
Expand Down
7 changes: 1 addition & 6 deletions src/Spectre.Console.Cli/CommandAppOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ namespace Spectre.Console.Cli;
/// The entry point for a command line application with a default command.
/// </summary>
/// <typeparam name="TDefaultCommand">The type of the default command.</typeparam>
public sealed class CommandApp<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TDefaultCommand
> : ICommandApp
public sealed class CommandApp<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TDefaultCommand> : ICommandApp
where TDefaultCommand : class, ICommand
{
private readonly CommandApp _app;
Expand Down
24 changes: 5 additions & 19 deletions src/Spectre.Console.Cli/ConfiguratorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public static IConfigurator SetHelpProvider(this IConfigurator configurator, IHe
/// <typeparam name="T">The type of the help provider to instantiate at runtime and use.</typeparam>
/// <returns>A configurator that can be used to configure the application further.</returns>
public static IConfigurator SetHelpProvider<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>(this IConfigurator configurator)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T
>(this IConfigurator configurator)
where T : IHelpProvider
{
if (configurator == null)
Expand Down Expand Up @@ -265,11 +263,7 @@ public static IBranchConfigurator AddBranch(
/// <param name="name">The name of the command branch.</param>
/// <param name="action">The command branch configuration.</param>
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
public static IBranchConfigurator AddBranch<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings>(
public static IBranchConfigurator AddBranch<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings>(
this IConfigurator<TSettings> configurator,
string name,
Action<IConfigurator<TSettings>> action)
Expand Down Expand Up @@ -331,11 +325,7 @@ public static ICommandConfigurator AddAsyncDelegate(
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings>(
public static ICommandConfigurator AddDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings>(
this IConfigurator<TSettings> configurator,
string name,
Func<CommandContext, int> func)
Expand All @@ -357,11 +347,7 @@ public static ICommandConfigurator AddDelegate<
/// <param name="name">The name of the command.</param>
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
public static ICommandConfigurator AddAsyncDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings>(
public static ICommandConfigurator AddAsyncDelegate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings>(
this IConfigurator<TSettings> configurator,
string name,
Func<CommandContext, Task<int>> func)
Expand Down
25 changes: 4 additions & 21 deletions src/Spectre.Console.Cli/IConfigurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ public interface IConfigurator
/// Sets the help provider for the application.
/// </summary>
/// <typeparam name="T">The type of the help provider to instantiate at runtime and use.</typeparam>
public void SetHelpProvider<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
T>()
public void SetHelpProvider<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>()
where T : IHelpProvider;

/// <summary>
Expand All @@ -39,13 +35,10 @@ public void SetHelpProvider<
/// <typeparam name="TCommand">The command type.</typeparam>
/// <param name="name">The name of the command.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
#if NET7_0_OR_GREATER
[RequiresDynamicCode(TrimWarnings.AddCommandShouldBeExplicitAboutSettings)]
#endif
ICommandConfigurator AddCommand<TCommand>(string name)
where TCommand : class, ICommand;

#if NET6_0_OR_GREATER
/// <summary>
/// Adds a command.
/// </summary>
Expand All @@ -59,7 +52,6 @@ ICommandConfigurator AddCommand<
where TCommand : class, ICommand
where TSettings : CommandSettings
;
#endif

/// <summary>
/// Adds a command that executes a delegate.
Expand All @@ -69,10 +61,7 @@ ICommandConfigurator AddCommand<
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings
>(string name, Func<CommandContext, TSettings, int> func)
where TSettings : CommandSettings;

Expand All @@ -84,10 +73,7 @@ ICommandConfigurator AddDelegate<
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddAsyncDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings
>(string name, Func<CommandContext, TSettings, Task<int>> func)
where TSettings : CommandSettings;

Expand All @@ -99,10 +85,7 @@ ICommandConfigurator AddAsyncDelegate<
/// <param name="action">The command branch configurator.</param>
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
IBranchConfigurator AddBranch<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TSettings
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TSettings
>(string name, Action<IConfigurator<TSettings>> action)
where TSettings : CommandSettings;
}
29 changes: 8 additions & 21 deletions src/Spectre.Console.Cli/IConfiguratorOfT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@ public interface IConfigurator<in TSettings>
/// arguments, flags or option values.
/// </remarks>
/// <typeparam name="TDefaultCommand">The default command type.</typeparam>
#if NET7_0_OR_GREATER
[RequiresDynamicCode(TrimWarnings.AddCommandShouldBeExplicitAboutSettings)]
#endif
void SetDefaultCommand<TDefaultCommand>()
where TDefaultCommand : class, ICommandLimiter<TSettings>;

#if NET6_0_OR_GREATER
/// <summary>
/// Adds a default command.
/// </summary>
Expand All @@ -49,7 +46,6 @@ void SetDefaultCommand<TDefaultCommand, TDefaultCommandSettings>()
where TDefaultCommand : class, ICommandLimiter<TSettings>
where TDefaultCommandSettings : CommandSettings
;
#endif

/// <summary>
/// Marks the branch as hidden.
Expand All @@ -64,13 +60,10 @@ void SetDefaultCommand<TDefaultCommand, TDefaultCommandSettings>()
/// <typeparam name="TCommand">The command type.</typeparam>
/// <param name="name">The name of the command.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
#if NET7_0_OR_GREATER
[RequiresDynamicCode(TrimWarnings.AddCommandShouldBeExplicitAboutSettings)]
#endif
ICommandConfigurator AddCommand<TCommand>(string name)
where TCommand : class, ICommandLimiter<TSettings>;

#if NET6_0_OR_GREATER
/// <summary>
/// Adds a command.
/// </summary>
Expand All @@ -80,11 +73,11 @@ ICommandConfigurator AddCommand<TCommand>(string name)
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddCommand<
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TCommand,
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TCommandSettings>(string name)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TCommandSettings
>(string name)
where TCommand : class, ICommandLimiter<TSettings>
where TCommandSettings : CommandSettings
;
#endif

/// <summary>
/// Adds a command that executes a delegate.
Expand All @@ -94,10 +87,8 @@ ICommandConfigurator AddCommand<
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, int> func)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TDerivedSettings
>(string name, Func<CommandContext, TDerivedSettings, int> func)
where TDerivedSettings : TSettings;

/// <summary>
Expand All @@ -108,10 +99,8 @@ ICommandConfigurator AddDelegate<
/// <param name="func">The delegate to execute as part of command execution.</param>
/// <returns>A command configurator that can be used to configure the command further.</returns>
ICommandConfigurator AddAsyncDelegate<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TDerivedSettings>(string name, Func<CommandContext, TDerivedSettings, Task<int>> func)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TDerivedSettings
>(string name, Func<CommandContext, TDerivedSettings, Task<int>> func)
where TDerivedSettings : TSettings;

/// <summary>
Expand All @@ -122,9 +111,7 @@ ICommandConfigurator AddAsyncDelegate<
/// <param name="action">The command branch configuration.</param>
/// <returns>A branch configurator that can be used to configure the branch further.</returns>
IBranchConfigurator AddBranch<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
#endif
TDerivedSettings>(string name, Action<IConfigurator<TDerivedSettings>> action)
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] TDerivedSettings
>(string name, Action<IConfigurator<TDerivedSettings>> action)
where TDerivedSettings : TSettings;
}
2 changes: 0 additions & 2 deletions src/Spectre.Console.Cli/ITypeRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ public interface ITypeRegistrar
/// <param name="implementation">The implementation.</param>
void Register(
Type service,
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
Type implementation);

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions src/Spectre.Console.Cli/ITypeRegistrarFrontend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public interface ITypeRegistrarFrontend
/// <typeparam name="TImplementation">The implementing type.</typeparam>
void Register<
TService,
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
#endif
TImplementation>()
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TImplementation>()
where TImplementation : TService;

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ namespace Spectre.Console.Cli;

internal static class CommandConstructorBinder
{
#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2072",
Justification = TrimWarnings.SuppressMessage)]
#endif
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2072", Justification = TrimWarnings.SuppressMessage)]
public static CommandSettings CreateSettings(CommandValueLookup lookup, ConstructorInfo constructor, ITypeResolver resolver)
{
if (constructor.DeclaringType == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ public static CommandSettings CreateSettings(CommandValueLookup lookup, Type set
return settings;
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2067",
Justification = TrimWarnings.SuppressMessage)]
#endif
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2067", Justification = TrimWarnings.SuppressMessage)]
private static CommandSettings CreateSettings(ITypeResolver resolver, Type settingsType)
{
if (resolver.Resolve(settingsType) is CommandSettings settings)
Expand Down
12 changes: 2 additions & 10 deletions src/Spectre.Console.Cli/Internal/Binding/CommandValueBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ public void Bind(CommandParameter parameter, ITypeResolver resolver, object? val
_lookup.SetValue(parameter, value);
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2072", Justification = TrimWarnings.SuppressMessage)]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050", Justification = TrimWarnings.SuppressMessage)]
#endif
private object GetLookup(CommandParameter parameter, ITypeResolver resolver, object? value)
{
var genericTypes = parameter.Property.PropertyType.GetGenericArguments();
Expand Down Expand Up @@ -67,9 +65,7 @@ private object GetLookup(CommandParameter parameter, ITypeResolver resolver, obj
return multimap;
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL3050", Justification = TrimWarnings.SuppressMessage)]
#endif
private object GetArray(CommandParameter parameter, object? value)
{
if (value is Array)
Expand Down Expand Up @@ -101,12 +97,8 @@ private object GetArray(CommandParameter parameter, object? value)
return newArray;
}

#if NET6_0_OR_GREATER
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2067",
Justification = TrimWarnings.SuppressMessage)]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2072",
Justification = TrimWarnings.SuppressMessage)]
#endif
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2067", Justification = TrimWarnings.SuppressMessage)]
[UnconditionalSuppressMessage("AssemblyLoadTrimming", "IL2072", Justification = TrimWarnings.SuppressMessage)]
private object GetFlag(CommandParameter parameter, object? value)
{
var flagValue = (IFlagValue?)_lookup.GetValue(parameter);
Expand Down
Loading

0 comments on commit 6dc5a59

Please sign in to comment.