Skip to content

Commit

Permalink
checking if endpoints class is static, if not, creating one. (#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery committed Sep 2, 2022
1 parent f37cbda commit fd96fbd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
30 changes: 24 additions & 6 deletions src/Scaffolding/VS.Web.CG.Mvc/Minimal Api/MinimalApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,10 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints
//Get class syntax node to add members to the class
var docRoot = docEditor.OriginalRoot as CompilationUnitSyntax;
//create CodeFile just to add usings

var usings = new List<string>();
//add usings for DbContext related actins.
if (!string.IsNullOrEmpty(templateModel.DbContextNamespace))
{
usings.Add(Constants.MicrosoftEntityFrameworkCorePackageName);
usings.Add(templateModel.DbContextNamespace);
}

Expand All @@ -168,11 +166,31 @@ internal async Task AddEndpointsMethod(string membersBlockText, string endpoints

if (classNode != null && classNode is ClassDeclarationSyntax classDeclaration)
{
SyntaxNode classParentSyntax = null;
//if class is not static, create a new class in the same file
if (!classDeclaration.Modifiers.Any(x => x.Text.Equals(SyntaxFactory.Token(SyntaxKind.StaticKeyword).Text)))
{
classParentSyntax = classDeclaration.Parent;
classDeclaration = SyntaxFactory.ClassDeclaration($"{templateModel.ModelType.Name}Endpoints")
.WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
.NormalizeWhitespace()
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed, SyntaxFactory.CarriageReturnLineFeed);
}
var modifiedClass = classDeclaration.AddMembers(
SyntaxFactory.GlobalStatement(
SyntaxFactory.ParseStatement(membersBlockText))
.WithLeadingTrivia(SyntaxFactory.Tab));
newRoot = newRoot.ReplaceNode(classNode, modifiedClass);
SyntaxFactory.GlobalStatement(SyntaxFactory.ParseStatement(membersBlockText)).WithLeadingTrivia(SyntaxFactory.Tab));

//modify class parent by adding class, classParentSyntax should be null if given class is static.
if (classParentSyntax != null)
{
classParentSyntax = classParentSyntax.InsertNodesAfter(classNode.Parent.ChildNodes().Last(), new List<SyntaxNode>() { modifiedClass });
newRoot = newRoot.ReplaceNode(classNode.Parent, classParentSyntax);
}
//modify given class
else
{
newRoot = newRoot.ReplaceNode(classNode, modifiedClass);
}

docEditor.ReplaceNode(docRoot, newRoot);
var classFileSourceTxt = await docEditor.GetChangedDocument()?.GetTextAsync();
var classFileTxt = classFileSourceTxt?.ToString();
Expand Down
27 changes: 24 additions & 3 deletions test/Scaffolding/Shared/MSBuildProjectStrings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public static IWebHost BuildWebHost(string[] args) =>
.WithName(""DeleteCar"");
}
}";
public const string EndpointsEmptyClass = @"namespace MinimalApiTest { class Endpoints { } } ";
public const string EndpointsEmptyClass = @"namespace MinimalApiTest { static class Endpoints { } } ";
public const string MinimalProgramcsFile = @"var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
Expand Down Expand Up @@ -417,8 +417,29 @@ public class Manufacturer
}
}";

// Strings for 3 layered project
public const string WebProjectTxt = @"
public const string CarWithoutNamespaceTxt = @"
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public class Car
{
public string ID { get; set; }
public string Name { get; set; }
public int ManufacturerID { get; set; }
public Manufacturer Manufacturer { get; set; }
[DataType(DataType.MultilineText)]
public string Notes { get; set; }
}
public class Manufacturer
{
public int ID { get; set; }
public string Name { get; set; }
public virtual ICollection<Car> Cars { get; set; }
}";

// Strings for 3 layered project
public const string WebProjectTxt = @"
<Project Sdk=""Microsoft.NET.Sdk.Web"">
<Import Project=""$(MSBuildThisFileDirectory)\TestCodeGeneration.targets"" Condition=""Exists('$(MSBuildThisFileDirectory)\TestCodeGeneration.targets')"" />
Expand Down

0 comments on commit fd96fbd

Please sign in to comment.