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

not adding extra whitespace and checking for Main method correctly #2013

Merged
merged 1 commit into from
Sep 8, 2022
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
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