@@ -15,17 +15,17 @@ namespace SourceGenerators
1515 public class PageDetailsGenerator : ISourceGenerator
1616 {
1717 private const string RouteAttributeName = "Microsoft.AspNetCore.Components.RouteAttribute" ;
18- private const string DescriptionAttributeName = "System.ComponentModel.Description " ;
18+ private const string MenuItemAttributeName = "MenuItem " ;
1919
2020 public void Execute ( GeneratorExecutionContext context )
2121 {
2222 try
2323 {
24- // Debugger.Launch();
2524
2625 IEnumerable < RouteableComponent > menuComponents = GetMenuComponents ( context . Compilation ) ;
2726
2827 context . AddSource ( "PageDetail" , SourceText . From ( Templates . PageDetail ( ) , Encoding . UTF8 ) ) ;
28+ context . AddSource ( "MenuItemAttribute" , SourceText . From ( Templates . MenuItemAttribute ( ) , Encoding . UTF8 ) ) ;
2929 var pageDetailsSource = SourceText . From ( Templates . MenuPages ( menuComponents ) , Encoding . UTF8 ) ;
3030 context . AddSource ( "PageDetails" , pageDetailsSource ) ;
3131 }
@@ -47,11 +47,13 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
4747 IEnumerable < ClassDeclarationSyntax > allClasses = allNodes
4848 . Where ( d => d . IsKind ( SyntaxKind . ClassDeclaration ) )
4949 . OfType < ClassDeclarationSyntax > ( ) ;
50-
50+
5151 return allClasses
5252 . Select ( component => TryGetMenuComponent ( compilation , component ) )
5353 . Where ( page => page is not null )
54- . Cast < RouteableComponent > ( ) // stops the nullable lies
54+ . Cast < RouteableComponent > ( ) // stops the nullable lies
55+ . OrderBy ( x => x . Order )
56+ . ThenBy ( x => x . Title )
5557 . ToImmutableArray ( ) ;
5658 }
5759
@@ -61,20 +63,20 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
6163 . SelectMany ( x => x . Attributes )
6264 . Where ( attr =>
6365 attr . Name . ToString ( ) == RouteAttributeName
64- || attr . Name . ToString ( ) == DescriptionAttributeName )
66+ || attr . Name . ToString ( ) == MenuItemAttributeName )
6567 . ToList ( ) ;
6668
6769 var routeAttribute = attributes . FirstOrDefault ( attr => attr . Name . ToString ( ) == RouteAttributeName ) ;
68- var descriptionAttribute = attributes . FirstOrDefault ( attr => attr . Name . ToString ( ) == DescriptionAttributeName ) ;
70+ var menuItemAttribute = attributes . FirstOrDefault ( attr => attr . Name . ToString ( ) == MenuItemAttributeName ) ;
6971
70- if ( routeAttribute is null || descriptionAttribute is null )
72+ if ( routeAttribute is null || menuItemAttribute is null )
7173 {
7274 return null ;
7375 }
7476
7577 if (
7678 routeAttribute . ArgumentList ? . Arguments . Count != 1 ||
77- descriptionAttribute . ArgumentList ? . Arguments . Count != 1 )
79+ menuItemAttribute . ArgumentList ? . Arguments . Count < 2 )
7880 {
7981 // no route path or description value
8082 return null ;
@@ -86,11 +88,27 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
8688 var routeExpr = routeArg . Expression ;
8789 var routeTemplate = semanticModel . GetConstantValue ( routeExpr ) . ToString ( ) ;
8890
89- var descriptionArg = descriptionAttribute . ArgumentList . Arguments [ 0 ] ;
91+ var iconArg = menuItemAttribute . ArgumentList . Arguments [ 0 ] ;
92+ var iconExpr = iconArg . Expression ;
93+ var icon = semanticModel . GetConstantValue ( iconExpr ) . ToString ( ) ;
94+
95+ var descriptionArg = menuItemAttribute . ArgumentList . Arguments [ 1 ] ;
9096 var descriptionExpr = descriptionArg . Expression ;
9197 var title = semanticModel . GetConstantValue ( descriptionExpr ) . ToString ( ) ;
9298
93- return new RouteableComponent ( routeTemplate , title ) ;
99+ var order = 0 ;
100+ if ( menuItemAttribute . ArgumentList ? . Arguments . Count == 3 )
101+ {
102+ var orderArg = menuItemAttribute . ArgumentList . Arguments [ 2 ] ;
103+ var orderExpr = orderArg . Expression ;
104+ var maybeOrder = semanticModel . GetConstantValue ( orderExpr ) ;
105+ if ( maybeOrder . HasValue )
106+ {
107+ order = ( int ) maybeOrder . Value ;
108+ }
109+ }
110+
111+ return new RouteableComponent ( routeTemplate , title , icon , order ) ;
94112 }
95113 }
96114}
0 commit comments