mirror of
https://github.com/chylex/.NET-Community-Toolkit.git
synced 2024-11-24 07:42:45 +01:00
74 lines
3.7 KiB
C#
74 lines
3.7 KiB
C#
// Licensed to the .NET Foundation under one or more agreements.
|
|
// The .NET Foundation licenses this file to you under the MIT license.
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
using System.Linq;
|
|
using System.Text;
|
|
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
|
|
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
namespace CommunityToolkit.Mvvm.SourceGenerators;
|
|
|
|
/// <summary>
|
|
/// A source generator for message registration without relying on compiled LINQ expressions.
|
|
/// </summary>
|
|
[Generator(LanguageNames.CSharp)]
|
|
public sealed partial class ObservableValidatorValidateAllPropertiesGenerator : IIncrementalGenerator
|
|
{
|
|
/// <inheritdoc/>
|
|
public void Initialize(IncrementalGeneratorInitializationContext context)
|
|
{
|
|
// Get all class declarations. We intentionally skip generating code for abstract types, as that would never be used.
|
|
// The methods that are generated by this generator are retrieved through reflection using the type of the invoking
|
|
// instance as discriminator, which means a type that is abstract could never be used (since it couldn't be instantiated).
|
|
IncrementalValuesProvider<INamedTypeSymbol> typeSymbols =
|
|
context.SyntaxProvider
|
|
.CreateSyntaxProvider(
|
|
static (node, _) => node is ClassDeclarationSyntax,
|
|
static (context, _) => (context.Node, Symbol: (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
|
|
.Where(static item => item.Symbol is { IsAbstract: false, IsGenericType: false } && item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
|
|
.Select(static (item, _) => item.Symbol);
|
|
|
|
// Get the types that inherit from ObservableValidator and gather their info
|
|
IncrementalValuesProvider<ValidationInfo> validationInfo =
|
|
typeSymbols
|
|
.Where(Execute.IsObservableValidator)
|
|
.Select(static (item, _) => Execute.GetInfo(item))
|
|
.WithComparer(ValidationInfo.Comparer.Default);
|
|
|
|
// Check whether the header file is needed
|
|
IncrementalValueProvider<bool> isHeaderFileNeeded =
|
|
validationInfo
|
|
.Collect()
|
|
.Select(static (item, _) => item.Length > 0);
|
|
|
|
// Check whether [DynamicallyAccessedMembers] is available
|
|
IncrementalValueProvider<bool> isDynamicallyAccessedMembersAttributeAvailable =
|
|
context.CompilationProvider
|
|
.Select(static (item, _) => item.HasAccessibleTypeWithMetadataName("System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute"));
|
|
|
|
// Gather the conditional flag and attribute availability
|
|
IncrementalValueProvider<(bool IsHeaderFileNeeded, bool IsDynamicallyAccessedMembersAttributeAvailable)> headerFileInfo =
|
|
isHeaderFileNeeded.Combine(isDynamicallyAccessedMembersAttributeAvailable);
|
|
|
|
// Generate the header file with the attributes
|
|
context.RegisterConditionalImplementationSourceOutput(headerFileInfo, static (context, item) =>
|
|
{
|
|
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item);
|
|
|
|
context.AddSource("__ObservableValidatorExtensions.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
|
});
|
|
|
|
// Generate the class with all validation methods
|
|
context.RegisterImplementationSourceOutput(validationInfo, static (context, item) =>
|
|
{
|
|
CompilationUnitSyntax compilationUnit = Execute.GetSyntax(item);
|
|
|
|
context.AddSource($"{item.FilenameHint}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
|
});
|
|
}
|
|
}
|