2021-12-24 20:06:06 +03:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-24 19:56:44 +03:00
|
|
|
|
using System.Diagnostics;
|
2021-12-24 20:06:06 +03:00
|
|
|
|
using System.Linq;
|
2022-08-24 19:56:44 +03:00
|
|
|
|
using System.Threading;
|
2021-12-24 20:06:06 +03:00
|
|
|
|
using MapTo.Extensions;
|
|
|
|
|
using MapTo.Sources;
|
|
|
|
|
using Microsoft.CodeAnalysis;
|
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
|
|
|
|
|
|
namespace MapTo
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// MapTo source generator.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Generator]
|
|
|
|
|
public class MapToGenerator : ISourceGenerator
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Initialize(GeneratorInitializationContext context)
|
|
|
|
|
{
|
2022-08-24 19:56:44 +03:00
|
|
|
|
|
2021-12-24 20:06:06 +03:00
|
|
|
|
context.RegisterForSyntaxNotifications(() => new MapToSyntaxReceiver());
|
2022-08-24 19:56:44 +03:00
|
|
|
|
|
2021-12-24 20:06:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Execute(GeneratorExecutionContext context)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var options = SourceGenerationOptions.From(context);
|
|
|
|
|
|
|
|
|
|
var compilation = context.Compilation
|
|
|
|
|
.AddSource(ref context, UseUpdateAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, JsonExtensionAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, MapFromAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, IgnoreMemberAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, ITypeConverterSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, MapTypeConverterAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, MapPropertyAttributeSource.Generate(options))
|
|
|
|
|
.AddSource(ref context, MappingContextSource.Generate(options));
|
|
|
|
|
|
|
|
|
|
if (context.SyntaxReceiver is MapToSyntaxReceiver receiver && receiver.CandidateTypes.Any())
|
|
|
|
|
{
|
|
|
|
|
AddGeneratedMappingsClasses(context, compilation, receiver.CandidateTypes, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(ex);
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddGeneratedMappingsClasses(GeneratorExecutionContext context, Compilation compilation, IEnumerable<TypeDeclarationSyntax> candidateTypes, SourceGenerationOptions options)
|
|
|
|
|
{
|
|
|
|
|
foreach (var typeDeclarationSyntax in candidateTypes)
|
|
|
|
|
{
|
|
|
|
|
var mappingContext = MappingContext.Create(compilation, options, typeDeclarationSyntax);
|
|
|
|
|
mappingContext.Diagnostics.ForEach(context.ReportDiagnostic);
|
|
|
|
|
|
|
|
|
|
if (mappingContext.Model is null)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-08-24 19:56:44 +03:00
|
|
|
|
|
2021-12-24 20:06:06 +03:00
|
|
|
|
|
|
|
|
|
var (source, hintName) = typeDeclarationSyntax switch
|
|
|
|
|
{
|
|
|
|
|
StructDeclarationSyntax => MapStructSource.Generate(mappingContext.Model),
|
|
|
|
|
ClassDeclarationSyntax => MapClassSource.Generate(mappingContext.Model),
|
|
|
|
|
RecordDeclarationSyntax => MapRecordSource.Generate(mappingContext.Model),
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.AddSource(hintName, source);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-19 10:55:47 +03:00
|
|
|
|
}
|