22using Microsoft . CodeAnalysis . CSharp ;
33using Microsoft . CodeAnalysis . CSharp . Syntax ;
44using Sybil ;
5- using System ;
65using System . Collections . Generic ;
76using System . Collections . Immutable ;
8- using System . Diagnostics ;
97using System . Linq ;
10- using System . Runtime . CompilerServices ;
118using System . Text ;
129
1310namespace Net . Sdk . Web . Extensions . SourceGenerators ;
@@ -68,16 +65,17 @@ private static void Execute(Compilation compilation, ImmutableArray<(ClassDeclar
6865 }
6966
7067 var languageVersion = maybeLanguageVersion . Value ;
71- var builder = SyntaxBuilder . CreateCompilationUnit ( )
72- . WithUsing ( Constants . UsingSystemThreading )
73- . WithUsing ( Constants . UsingMicrosoftAspNetCoreRouting )
74- . WithUsing ( Constants . UsingMicrosoftAspNetCoreHttp )
75- . WithUsing ( Constants . UsingMicrosoftAspNetCoreBuilder )
76- . WithUsing ( Constants . UsingMicrosoftAspNetCoreMvc )
77- . WithUsing ( Constants . UsingSystemRuntimeCompilerServices ) ;
78-
79- var routeUsings = new HashSet < string > ( ) ;
80-
68+ var routeUsings = new HashSet < string >
69+ {
70+ Constants . UsingSystemThreading ,
71+ Constants . UsingMicrosoftAspNetCoreRouting ,
72+ Constants . UsingMicrosoftAspNetCoreHttp ,
73+ Constants . UsingMicrosoftAspNetCoreBuilder ,
74+ Constants . UsingMicrosoftAspNetCoreMvc ,
75+ Constants . UsingSystemRuntimeCompilerServices
76+ } ;
77+
78+ var builder = SyntaxBuilder . CreateCompilationUnit ( ) ;
8179 var namespaceBuilder = languageVersion >= LanguageVersion . CSharp10 ? SyntaxBuilder . CreateFileScopedNamespace ( Constants . Namespace ) : SyntaxBuilder . CreateNamespace ( Constants . Namespace ) ;
8280 builder . WithNamespace ( namespaceBuilder ) ;
8381 var webAppBuilder = SyntaxBuilder . CreateClass ( Constants . WebApplicationExtensionsName )
@@ -88,13 +86,7 @@ private static void Execute(Compilation compilation, ImmutableArray<(ClassDeclar
8886 . WithModifiers ( $ "{ Constants . Public } { Constants . Static } ")
8987 . WithAttribute ( SyntaxBuilder . CreateAttribute ( Constants . MethodImplAttribute )
9088 . WithRawArgument ( Constants . MethodImplArgument ) ) ;
91- var registerWebAppMethodBuilder = SyntaxBuilder . CreateMethod ( Constants . WebApplicationTypeName , Constants . RegisterRoutesMethodName )
92- . WithParameter ( Constants . WebApplicationTypeName , Constants . BuilderParameterName )
93- . WithModifiers ( $ "{ Constants . Public } { Constants . Static } ")
94- . WithAttribute ( SyntaxBuilder . CreateAttribute ( Constants . MethodImplAttribute )
95- . WithRawArgument ( Constants . MethodImplArgument ) ) ;
9689 webAppBuilder . WithMethod ( useRoutesWebAppMethodBuilder ) ;
97- webAppBuilder . WithMethod ( registerWebAppMethodBuilder ) ;
9890
9991 var useRoutesWebAppBody = new StringBuilder ( ) ;
10092 foreach ( var classToMethodMap in classToMethodMapping )
@@ -164,22 +156,22 @@ or Constants.DeleteAttributeName
164156 var innerPattern = $ "/{ pattern ? . Trim ( '/' ) } ";
165157 var finalPattern = $ "{ outerPattern } { innerPattern } ". Replace ( "//" , "/" ) ;
166158
167- if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . GetAttributeName or Constants . GetAttributeShortName ) is AttributeSyntax &&
159+ if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . GetAttributeName or Constants . GetAttributeShortName ) is not null &&
168160 GetMethodBodyByType ( "Get" , finalPattern , classDeclarationSyntax , methodDeclarationSyntax , compilation , routeUsings ) is string getMethodBody )
169161 {
170162 useRoutesWebAppBody . AppendLine ( getMethodBody ) ;
171163 }
172- else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . PostAttributeName or Constants . PostAttributeShortName ) is AttributeSyntax &&
164+ else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . PostAttributeName or Constants . PostAttributeShortName ) is not null &&
173165 GetMethodBodyByType ( "Post" , finalPattern , classDeclarationSyntax , methodDeclarationSyntax , compilation , routeUsings ) is string postMethodBody )
174166 {
175167 useRoutesWebAppBody . AppendLine ( postMethodBody ) ;
176168 }
177- else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . PutAttributeName or Constants . PutAttributeShortName ) is AttributeSyntax &&
169+ else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . PutAttributeName or Constants . PutAttributeShortName ) is not null &&
178170 GetMethodBodyByType ( "Put" , finalPattern , classDeclarationSyntax , methodDeclarationSyntax , compilation , routeUsings ) is string putMethodBoty )
179171 {
180172 useRoutesWebAppBody . AppendLine ( putMethodBoty ) ;
181173 }
182- else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . DeleteAttributeName or Constants . DeleteAttributeShortName ) is AttributeSyntax &&
174+ else if ( methodAttributes . FirstOrDefault ( a => a . Name . ToString ( ) is Constants . DeleteAttributeName or Constants . DeleteAttributeShortName ) is not null &&
183175 GetMethodBodyByType ( "Delete" , finalPattern , classDeclarationSyntax , methodDeclarationSyntax , compilation , routeUsings ) is string deleteMethodBody )
184176 {
185177 useRoutesWebAppBody . AppendLine ( deleteMethodBody ) ;
@@ -189,7 +181,6 @@ or Constants.DeleteAttributeName
189181
190182 useRoutesWebAppBody . AppendLine ( "return builder;" ) ;
191183 useRoutesWebAppMethodBuilder . WithBody ( useRoutesWebAppBody . ToString ( ) ) ;
192- registerWebAppMethodBuilder . WithBody ( useRoutesWebAppBody . ToString ( ) ) ;
193184 foreach ( var classUsing in routeUsings )
194185 {
195186 builder . WithUsing ( classUsing ) ;
@@ -231,15 +222,13 @@ or Constants.DeleteAttributeName
231222 return true ;
232223 } ) )
233224 {
234-
235225 if ( attribute . Name is not GenericNameSyntax genericNameSyntax || genericNameSyntax . TypeArgumentList . Arguments . Count != 1 )
236226 {
237227 continue ;
238228 }
239229
240230 var routeFilterTypeSyntax = genericNameSyntax . TypeArgumentList . Arguments [ 0 ] ;
241- var routeFilterTypeSymbol = semanticModel . GetSymbolInfo ( routeFilterTypeSyntax ) . Symbol as ITypeSymbol ;
242- if ( routeFilterTypeSymbol is null )
231+ if ( semanticModel . GetSymbolInfo ( routeFilterTypeSyntax ) . Symbol is not ITypeSymbol routeFilterTypeSymbol )
243232 {
244233 continue ;
245234 }
@@ -273,10 +262,10 @@ or Constants.DeleteAttributeName
273262 return returnTypeSymbol ? . ToDisplayString ( ) switch
274263 {
275264 "System.Threading.Tasks.Task<Microsoft.AspNetCore.Http.IResult>" => @$ "
276- builder.Map{ type } (""{ pattern } "", (HttpContext httpContext, { classDeclarationSyntax . Identifier } route{ ( parameters . Length > 0 ? $ ", { parameters } " : "" ) } ) =>
265+ builder.Map{ type } (""{ pattern } "", async (HttpContext httpContext, { classDeclarationSyntax . Identifier } route{ ( parameters . Length > 0 ? $ ", { parameters } " : "" ) } ) =>
277266 {{
278267 var cancellationToken = httpContext.RequestAborted;
279- return route.{ methodDeclarationSyntax . Identifier } ({ variables } );
268+ return await route.{ methodDeclarationSyntax . Identifier } ({ variables } );
280269 }}){ routeFilterSb } ;" ,
281270 "Microsoft.AspNetCore.Http.IResult" => @$ "
282271 builder.Map{ type } (""{ pattern } "", (HttpContext httpContext, { classDeclarationSyntax . Identifier } route{ ( parameters . Length > 0 ? $ ", { parameters } " : "" ) } ) =>
0 commit comments