Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix minimal endpoints in minimal classes (w/ top level statements) #2292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/install-aspnet-codegenerator.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set VERSION=7.0.4
set VERSION=8.0.0-dev
set DEFAULT_NUPKG_PATH=%userprofile%\.nuget\packages
set SRC_DIR=%cd%
set NUPKG=artifacts/packages/Debug/Shipping/
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-aspnet-codegenerator.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

VERSION=7.0.4
VERSION=8.0.0-dev
DEFAULT_NUPKG_PATH=~/.nuget/packages
SRC_DIR=$(pwd)
echo $SRC_DIR
Expand Down
40 changes: 29 additions & 11 deletions src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
//TODO throw exception
return;
}

//Get class syntax node to add members to the class
var docRoot = docEditor.OriginalRoot as CompilationUnitSyntax;
//create CodeFile just to add usings
Expand All @@ -178,14 +179,16 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
{
usings.Add("Microsoft.AspNetCore.Http.HttpResults");
}

var endpointsCodeFile = new CodeFile { Usings = usings.ToArray() };
var docBuilder = new DocumentBuilder(docEditor, endpointsCodeFile, ConsoleLogger);
var newRoot = docBuilder.AddUsings(new CodeChangeOptions());
var classNode = newRoot.DescendantNodes().FirstOrDefault(node => node is ClassDeclarationSyntax classDeclarationSyntax && classDeclarationSyntax.Identifier.ValueText.Contains(className));
//get namespace node just for the namespace name.
var namespaceSyntax = classNode.Parent.DescendantNodes().FirstOrDefault(node => node is NamespaceDeclarationSyntax nsDeclarationSyntax || node is FileScopedNamespaceDeclarationSyntax fsDeclarationSyntax);
var namespaceSyntax = classNode?.Parent?.DescendantNodes().FirstOrDefault(node => node is NamespaceDeclarationSyntax nsDeclarationSyntax || node is FileScopedNamespaceDeclarationSyntax fsDeclarationSyntax);
templateModel.EndpointsNamespace = string.IsNullOrEmpty(namespaceSyntax?.ToString()) ? templateModel.EndpointsNamespace : namespaceSyntax?.ToString();

//if a normal ClassDeclarationSyntax, add static method to this class
if (classNode != null && classNode is ClassDeclarationSyntax classDeclaration)
{
SyntaxNode classParentSyntax = null;
Expand All @@ -198,6 +201,7 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
.NormalizeWhitespace()
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
}

var modifiedClass = classDeclaration.AddMembers(
SyntaxFactory.GlobalStatement(SyntaxFactory.ParseStatement(membersBlockText)).WithLeadingTrivia(SyntaxFactory.Tab));

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

docEditor.ReplaceNode(docRoot, newRoot);
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
var classFileTxt = classFileSourceTxt?.ToString();
if (!string.IsNullOrEmpty(classFileTxt))
{
//write to endpoints class path.
FileSystem.WriteAllText(endPointsDocument.FilePath, classFileTxt);
//add app.Map statement to Program.cs
await ModifyProgramCs(templateModel);
}
docEditor.ReplaceNode(docRoot, newRoot);
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
var classFileTxt = classFileSourceTxt?.ToString();
if (!string.IsNullOrEmpty(classFileTxt))
{
//write to endpoints class path.
FileSystem.WriteAllText(endPointsDocument.FilePath, classFileTxt);
//add app.Map statement to Program.cs
await ModifyProgramCs(templateModel);
}
}
}
Expand Down