Skip to content

Commit d24b4f2

Browse files
fix minimal endpoints in minimal classes (w/ top level statements) (#2292)
* init * file with top level statements can be used for endpoints now! * minor fixes
1 parent ce2e530 commit d24b4f2

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

scripts/install-aspnet-codegenerator.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set VERSION=7.0.4
1+
set VERSION=8.0.0-dev
22
set DEFAULT_NUPKG_PATH=%userprofile%\.nuget\packages
33
set SRC_DIR=%cd%
44
set NUPKG=artifacts/packages/Debug/Shipping/

scripts/install-aspnet-codegenerator.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
VERSION=7.0.4
3+
VERSION=8.0.0-dev
44
DEFAULT_NUPKG_PATH=~/.nuget/packages
55
SRC_DIR=$(pwd)
66
echo $SRC_DIR

src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
154154
//TODO throw exception
155155
return;
156156
}
157+
157158
//Get class syntax node to add members to the class
158159
var docRoot = docEditor.OriginalRoot as CompilationUnitSyntax;
159160
//create CodeFile just to add usings
@@ -178,14 +179,16 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
178179
{
179180
usings.Add("Microsoft.AspNetCore.Http.HttpResults");
180181
}
182+
181183
var endpointsCodeFile = new CodeFile { Usings = usings.ToArray() };
182184
var docBuilder = new DocumentBuilder(docEditor, endpointsCodeFile, ConsoleLogger);
183185
var newRoot = docBuilder.AddUsings(new CodeChangeOptions());
184186
var classNode = newRoot.DescendantNodes().FirstOrDefault(node => node is ClassDeclarationSyntax classDeclarationSyntax && classDeclarationSyntax.Identifier.ValueText.Contains(className));
185187
//get namespace node just for the namespace name.
186-
var namespaceSyntax = classNode.Parent.DescendantNodes().FirstOrDefault(node => node is NamespaceDeclarationSyntax nsDeclarationSyntax || node is FileScopedNamespaceDeclarationSyntax fsDeclarationSyntax);
188+
var namespaceSyntax = classNode?.Parent?.DescendantNodes().FirstOrDefault(node => node is NamespaceDeclarationSyntax nsDeclarationSyntax || node is FileScopedNamespaceDeclarationSyntax fsDeclarationSyntax);
187189
templateModel.EndpointsNamespace = string.IsNullOrEmpty(namespaceSyntax?.ToString()) ? templateModel.EndpointsNamespace : namespaceSyntax?.ToString();
188190

191+
//if a normal ClassDeclarationSyntax, add static method to this class
189192
if (classNode != null && classNode is ClassDeclarationSyntax classDeclaration)
190193
{
191194
SyntaxNode classParentSyntax = null;
@@ -198,6 +201,7 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
198201
.NormalizeWhitespace()
199202
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
200203
}
204+
201205
var modifiedClass = classDeclaration.AddMembers(
202206
SyntaxFactory.GlobalStatement(SyntaxFactory.ParseStatement(membersBlockText)).WithLeadingTrivia(SyntaxFactory.Tab));
203207

@@ -212,17 +216,31 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
212216
{
213217
newRoot = newRoot.ReplaceNode(classNode, modifiedClass);
214218
}
219+
}
220+
//check if its a minimal class with no class declarations (using top level statements)
221+
else
222+
{
223+
//create a ClassDeclarationSyntax, add the static endpoints method to the class
224+
var newClassDeclaration = SyntaxFactory.ClassDeclaration($"{templateModel.ModelType.Name}Endpoints")
225+
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
226+
.NormalizeWhitespace()
227+
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
228+
newClassDeclaration = newClassDeclaration.AddMembers(
229+
SyntaxFactory.GlobalStatement(SyntaxFactory.ParseStatement(membersBlockText)).WithLeadingTrivia(SyntaxFactory.Tab));
230+
//add members at the end of the namespace node.
231+
//replace namespace node in newRoot
232+
newRoot = newRoot.InsertNodesAfter(newRoot.ChildNodes().Last(), new List<SyntaxNode> { newClassDeclaration });
233+
}
215234

216-
docEditor.ReplaceNode(docRoot, newRoot);
217-
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
218-
var classFileTxt = classFileSourceTxt?.ToString();
219-
if (!string.IsNullOrEmpty(classFileTxt))
220-
{
221-
//write to endpoints class path.
222-
FileSystem.WriteAllText(endPointsDocument.FilePath, classFileTxt);
223-
//add app.Map statement to Program.cs
224-
await ModifyProgramCs(templateModel);
225-
}
235+
docEditor.ReplaceNode(docRoot, newRoot);
236+
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
237+
var classFileTxt = classFileSourceTxt?.ToString();
238+
if (!string.IsNullOrEmpty(classFileTxt))
239+
{
240+
//write to endpoints class path.
241+
FileSystem.WriteAllText(endPointsDocument.FilePath, classFileTxt);
242+
//add app.Map statement to Program.cs
243+
await ModifyProgramCs(templateModel);
226244
}
227245
}
228246
}

0 commit comments

Comments
 (0)