|
| 1 | +using Microsoft.CodeAnalysis; |
| 2 | +using Microsoft.CodeAnalysis.CSharp; |
| 3 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 4 | +using Microsoft.CodeAnalysis.Text; |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Collections.Immutable; |
| 8 | +using System.Diagnostics; |
| 9 | +using System.Linq; |
| 10 | +using System.Text; |
| 11 | + |
| 12 | +namespace SourceGenerators |
| 13 | +{ |
| 14 | + // see https://github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/SourceGenerators/SourceGeneratorSamples/AutoNotifyGenerator.cs |
| 15 | + // https://github.com/dotnet/roslyn-sdk/blob/master/samples/CSharp/SourceGenerators/SourceGeneratorSamples/MustacheGenerator.cs |
| 16 | + [Generator] |
| 17 | + public class AppRoutesGenerator : ISourceGenerator |
| 18 | + { |
| 19 | + private const string RouteAttributeName = "Microsoft.AspNetCore.Components.RouteAttribute"; |
| 20 | + |
| 21 | + public void Execute(GeneratorExecutionContext context) |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + Debugger.Launch(); |
| 26 | + var allRoutePaths = GetRouteTemplates(context.Compilation); |
| 27 | + |
| 28 | + var dictSource = SourceText.From(Templates.AppRoutes(allRoutePaths), Encoding.UTF8); |
| 29 | + context.AddSource("AppRoutes", dictSource); |
| 30 | + } |
| 31 | + catch (Exception) |
| 32 | + { |
| 33 | + Debugger.Launch(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public void Initialize(GeneratorInitializationContext context) |
| 38 | + { |
| 39 | + //Debugger.Launch(); |
| 40 | + } |
| 41 | + |
| 42 | + private static ImmutableArray<string> GetRouteTemplates(Compilation compilation) |
| 43 | + { |
| 44 | + // Get all classes |
| 45 | + IEnumerable<SyntaxNode> allNodes = compilation.SyntaxTrees.SelectMany(s => s.GetRoot().DescendantNodes()); |
| 46 | + IEnumerable<ClassDeclarationSyntax> allClasses = allNodes |
| 47 | + .Where(d => d.IsKind(SyntaxKind.ClassDeclaration)) |
| 48 | + .OfType<ClassDeclarationSyntax>(); |
| 49 | + |
| 50 | + return allClasses |
| 51 | + .Select(component => GetRoutePath(compilation, component)) |
| 52 | + .Where(route => route is not null) |
| 53 | + .Cast<string>()// stops the nullable lies |
| 54 | + .ToImmutableArray(); |
| 55 | + } |
| 56 | + |
| 57 | + private static string? GetRoutePath(Compilation compilation, ClassDeclarationSyntax component) |
| 58 | + { |
| 59 | + var routeAttribute = component.AttributeLists |
| 60 | + .SelectMany(x => x.Attributes) |
| 61 | + .FirstOrDefault(attr => attr.Name.ToString() == RouteAttributeName); |
| 62 | + |
| 63 | + if (routeAttribute?.ArgumentList?.Arguments.Count != 1) |
| 64 | + { |
| 65 | + // no route path |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + var semanticModel = compilation.GetSemanticModel(component.SyntaxTree); |
| 70 | + |
| 71 | + var routeArg = routeAttribute.ArgumentList.Arguments[0]; |
| 72 | + var routeExpr = routeArg.Expression; |
| 73 | + return semanticModel.GetConstantValue(routeExpr).ToString(); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments