@@ -15,17 +15,17 @@ namespace SourceGenerators
15
15
public class PageDetailsGenerator : ISourceGenerator
16
16
{
17
17
private const string RouteAttributeName = "Microsoft.AspNetCore.Components.RouteAttribute" ;
18
- private const string DescriptionAttributeName = "System.ComponentModel.Description " ;
18
+ private const string MenuItemAttributeName = "MenuItem " ;
19
19
20
20
public void Execute ( GeneratorExecutionContext context )
21
21
{
22
22
try
23
23
{
24
- // Debugger.Launch();
25
24
26
25
IEnumerable < RouteableComponent > menuComponents = GetMenuComponents ( context . Compilation ) ;
27
26
28
27
context . AddSource ( "PageDetail" , SourceText . From ( Templates . PageDetail ( ) , Encoding . UTF8 ) ) ;
28
+ context . AddSource ( "MenuItemAttribute" , SourceText . From ( Templates . MenuItemAttribute ( ) , Encoding . UTF8 ) ) ;
29
29
var pageDetailsSource = SourceText . From ( Templates . MenuPages ( menuComponents ) , Encoding . UTF8 ) ;
30
30
context . AddSource ( "PageDetails" , pageDetailsSource ) ;
31
31
}
@@ -47,11 +47,13 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
47
47
IEnumerable < ClassDeclarationSyntax > allClasses = allNodes
48
48
. Where ( d => d . IsKind ( SyntaxKind . ClassDeclaration ) )
49
49
. OfType < ClassDeclarationSyntax > ( ) ;
50
-
50
+
51
51
return allClasses
52
52
. Select ( component => TryGetMenuComponent ( compilation , component ) )
53
53
. 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 )
55
57
. ToImmutableArray ( ) ;
56
58
}
57
59
@@ -61,20 +63,20 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
61
63
. SelectMany ( x => x . Attributes )
62
64
. Where ( attr =>
63
65
attr . Name . ToString ( ) == RouteAttributeName
64
- || attr . Name . ToString ( ) == DescriptionAttributeName )
66
+ || attr . Name . ToString ( ) == MenuItemAttributeName )
65
67
. ToList ( ) ;
66
68
67
69
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 ) ;
69
71
70
- if ( routeAttribute is null || descriptionAttribute is null )
72
+ if ( routeAttribute is null || menuItemAttribute is null )
71
73
{
72
74
return null ;
73
75
}
74
76
75
77
if (
76
78
routeAttribute . ArgumentList ? . Arguments . Count != 1 ||
77
- descriptionAttribute . ArgumentList ? . Arguments . Count != 1 )
79
+ menuItemAttribute . ArgumentList ? . Arguments . Count < 2 )
78
80
{
79
81
// no route path or description value
80
82
return null ;
@@ -86,11 +88,27 @@ private static ImmutableArray<RouteableComponent> GetMenuComponents(Compilation
86
88
var routeExpr = routeArg . Expression ;
87
89
var routeTemplate = semanticModel . GetConstantValue ( routeExpr ) . ToString ( ) ;
88
90
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 ] ;
90
96
var descriptionExpr = descriptionArg . Expression ;
91
97
var title = semanticModel . GetConstantValue ( descriptionExpr ) . ToString ( ) ;
92
98
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 ) ;
94
112
}
95
113
}
96
114
}
0 commit comments