Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions doc/GenMapper版本日志.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

## v0.1.0
- ⚡️升级`.NET10`
- 🛠优化生成器代码
- ⚡️支持生成扩展方法

```csharp
internal static partial class MapperExtensions
{
[GenMapper]
[MapBetween([nameof(Product.Name), nameof(Product.Category)], nameof(ProductDto.Name), By = nameof(MapToDtoName))]
[MapBetween(nameof(Product.SplitValue), [nameof(ProductDto.S1), nameof(ProductDto.S2)], By = nameof(MapOneToMultiTest))]
public static partial ProductDto ToDto(this Product product, Action<Product, ProductDto>? action = null);

public static string MapToDtoName(string name, string category)
{
return $"{name}-{category}";
}

public static (string, string) MapOneToMultiTest(string value)
{
var val = value.Split(',');
return (val[0], val[1]);
}
}
```

## v0.0.9

Expand Down
72 changes: 72 additions & 0 deletions src/AutoGenMapper.Roslyn/AutoMapperExtensionGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Generators.Shared;
using Generators.Shared.Builder;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Linq;
using static AutoGenMapperGenerator.Helper;
namespace AutoGenMapperGenerator;

[Generator(LanguageNames.CSharp)]
public class AutoMapperExtensionGenerator : IIncrementalGenerator
{
public void Initialize(IncrementalGeneratorInitializationContext context)
{
var map = context.SyntaxProvider.ForAttributeWithMetadataName(GenMapperAttributeFullName
, static (node, _) => node is MethodDeclarationSyntax
{
ParameterList.Parameters:
{
Count: var psCount
} ps
} && psCount > 0 && ps[0].Type is not null
, static (source, _) => CollectMapperContext(source));

context.RegisterSourceOutput(map, static (context, source) =>
{
if (source.Item2 is not null)
{
context.ReportDiagnostic(source.Item2);
return;
}
if (source.Item1 is null) return;
var file = CreateCodeFile(source.Item1);
#if DEBUG
var ss = file?.ToString();
#endif
context.AddSource(file);
});
}
static (MapperContext?, Diagnostic?) CollectMapperContext(GeneratorAttributeSyntaxContext context)
{
var location = context.TargetNode.GetLocation();
var mapBetweens = CollectSpecificBetweenInfo(context.TargetSymbol).ToArray();
var mapperTargets = CollectMapTargets(context.TargetSymbol);
var mapContext = new MapperContext(context.TargetSymbol)
{
Targets = mapperTargets
};
var error = Helper.CollectMapperContext(mapContext, mapBetweens, location);
return (mapContext, error);
}
static CodeFile? CreateCodeFile(MapperContext context)
{
INamedTypeSymbol classSymbol = context.ContainingType!;
var cb = ClassBuilder.Default.Modifiers("static partial").ClassName(classSymbol.Name)
.AddGeneratedCodeAttribute(typeof(AutoMapperGenerator));
List<MethodBuilder> methods = [];
foreach (var ctx in context.Targets)
{
var m = BuildAutoMapClass.GenerateExtensionMethod(context, ctx);
methods.Add(m);
}

cb.AddMembers([.. methods]);
var ns = NamespaceBuilder.Default.Namespace(classSymbol.ContainingNamespace.ToDisplayString());
return CodeFile.New($"{classSymbol.FormatFileName()}.AutoMap.Ex.g.cs")
//.AddUsings("using System.Linq;")
//.AddUsings("using AutoGenMapperGenerator;")
.AddUsings(classSymbol.GetTargetUsings())
.AddMembers(ns.AddMembers(cb));
}
}
Loading
Loading