Skip to content

Commit

Permalink
not adding extra whitespace and checking for Main method correctly (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
deepchoudhery authored Sep 8, 2022
1 parent 5de3812 commit 4bb5a46
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
11 changes: 1 addition & 10 deletions src/Scaffolding/VS.Web.CG.Mvc/Identity/IdentityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,7 @@ private CodeSnippet[] ApplyIdentityChanges(CodeSnippet[] filteredChanges, string
{
foreach (var codeChange in filteredChanges)
{
if (!useTopLevelsStatements)
{
codeChange.LeadingTrivia = codeChange.LeadingTrivia ?? new Formatting();
codeChange.LeadingTrivia.NumberOfSpaces += 12;
}
codeChange.LeadingTrivia = codeChange.LeadingTrivia ?? new Formatting();
codeChange.Block = EditIdentityStrings(codeChange.Block, dbContextClassName, identityUserClassName, useSqlite, codeChange?.LeadingTrivia?.NumberOfSpaces);
}

Expand Down Expand Up @@ -364,11 +360,6 @@ internal static string EditIdentityStrings(string stringToModify, string dbConte
modifiedString = modifiedString.Replace("Connection string '{0}'", $"Connection string '{dbContextClassName}Connection'");
}

if (stringToModify.Contains("{1}"))
{
modifiedString = modifiedString.Replace("{1}", new string(' ', spaces.GetValueOrDefault() + 4));
}

return modifiedString;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
{
"InsertAfter": "builder.Configuration.GetConnectionString",
"CheckBlock": "builder.Services.AddDbContext",
"Block": "builder.Services.AddDbContext<{0}>(options =>\r\n{1}options.{0}(connectionString))\"",
"Block": "builder.Services.AddDbContext<{0}>(options => options.{0}(connectionString))\"",
"LeadingTrivia": {
"Newline": true
}
},
{
"InsertAfter": "builder.Services.AddDbContext",
"CheckBlock": "builder.Services.AddDefaultIdentity",
"Block": "builder.Services.AddDefaultIdentity<{0}>(options => options.SignIn.RequireConfirmedAccount = true)\r\n{1}.AddEntityFrameworkStores<{0}>()\"",
"Block": "builder.Services.AddDefaultIdentity<{0}>(options => options.SignIn.RequireConfirmedAccount = true).AddEntityFrameworkStores<{0}>()\"",
"LeadingTrivia": {
"Newline": true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,17 @@ internal static SyntaxNode ApplyChangesToMethod(SyntaxNode root, CodeSnippet[] f

internal static MethodDeclarationSyntax GetMethodFromSyntaxRoot(CompilationUnitSyntax root, string methodIdentifier)
{
var namespaceNode = root.Members.OfType<NamespaceDeclarationSyntax>()?.FirstOrDefault();
BaseNamespaceDeclarationSyntax namespaceNode = root.Members.OfType<NamespaceDeclarationSyntax>()?.FirstOrDefault();
if (namespaceNode == null)
{
namespaceNode = root.Members.OfType<FileScopedNamespaceDeclarationSyntax>()?.FirstOrDefault();
}

var classNode = namespaceNode?.Members.OfType<ClassDeclarationSyntax>()?.FirstOrDefault() ??
root?.Members.OfType<ClassDeclarationSyntax>()?.FirstOrDefault();
if (classNode?.ChildNodes().FirstOrDefault(
n => n is MethodDeclarationSyntax syntax &&
syntax.Identifier.ToString().Equals(methodIdentifier, StringComparison.OrdinalIgnoreCase)) is MethodDeclarationSyntax method)
n => n is MethodDeclarationSyntax syntax &&
syntax.Identifier.ToString().Equals(methodIdentifier, StringComparison.OrdinalIgnoreCase)) is MethodDeclarationSyntax method)
{
return method;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,23 @@ internal static async Task<bool> IsUsingTopLevelStatements(IModelTypesLocator mo
var programDocument = modelTypesLocator.GetAllDocuments().FirstOrDefault(d => d.Name.EndsWith("Program.cs"));
if (programDocument != null && await programDocument.GetSyntaxRootAsync() is CompilationUnitSyntax root)
{
var fileScopedNamespaceNode = root.Members.OfType<FileScopedNamespaceDeclarationSyntax>()?.FirstOrDefault();
if (fileScopedNamespaceNode == null)
BaseNamespaceDeclarationSyntax namespaceNode = root.Members.OfType<FileScopedNamespaceDeclarationSyntax>()?.FirstOrDefault();
if (namespaceNode == null)
{
var namespaceNode = root.Members.OfType<NamespaceDeclarationSyntax>()?.FirstOrDefault();
var classNode = namespaceNode?.Members.OfType<ClassDeclarationSyntax>()?.FirstOrDefault();
var mainMethod = classNode?.ChildNodes().FirstOrDefault(n => n is MethodDeclarationSyntax
&& ((MethodDeclarationSyntax)n).Identifier.ToString().Equals(Main, StringComparison.OrdinalIgnoreCase));
namespaceNode = root.Members.OfType<NamespaceDeclarationSyntax>()?.FirstOrDefault();
}
var classNode = namespaceNode?.Members.OfType<ClassDeclarationSyntax>()?.FirstOrDefault();
var mainMethod = classNode?.ChildNodes().FirstOrDefault(n => n is MethodDeclarationSyntax syntax
&& syntax.Identifier.ToString().Equals(Main, StringComparison.OrdinalIgnoreCase));

return mainMethod == null;
if (mainMethod == null)
{
mainMethod = namespaceNode?.ChildNodes().FirstOrDefault(
n => n is MethodDeclarationSyntax syntax &&
syntax.Identifier.ToString().Equals(Main, StringComparison.OrdinalIgnoreCase));
}

return mainMethod == null;
}

return true;
Expand Down

0 comments on commit 4bb5a46

Please sign in to comment.