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 indentation when scaffolding with no namespace #25964

Merged
merged 1 commit into from
Sep 10, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,9 @@ protected virtual void GenerateOnConfiguring(
{
if (!suppressConnectionStringWarning)
{
_builder.DecrementIndent()
.DecrementIndent()
.DecrementIndent()
.DecrementIndent()
.AppendLine("#warning " + DesignStrings.SensitiveInformationWarning)
.IncrementIndent()
.IncrementIndent()
.IncrementIndent()
.IncrementIndent();
using var _ = _builder.SuspendIndent();

_builder.AppendLine("#warning " + DesignStrings.SensitiveInformationWarning);
}

var useProviderCall = _providerConfigurationCodeGenerator.GenerateUseProvider(
Expand Down
23 changes: 23 additions & 0 deletions src/EFCore/Infrastructure/IndentedStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ public virtual IndentedStringBuilder DecrementIndent()
public virtual IDisposable Indent()
=> new Indenter(this);

/// <summary>
/// Temporarily disables all indentation. Restores the original indentation when the returned object is disposed.
/// </summary>
/// <returns> An object that restores the original indentation when disposed. </returns>
public virtual IDisposable SuspendIndent()
=> new IndentSuspender(this);

/// <summary>
/// Returns the built string.
/// </summary>
Expand Down Expand Up @@ -246,5 +253,21 @@ public Indenter(IndentedStringBuilder stringBuilder)
public void Dispose()
=> _stringBuilder.DecrementIndent();
}

private sealed class IndentSuspender : IDisposable
{
private readonly IndentedStringBuilder _stringBuilder;
private readonly byte _indent;

public IndentSuspender(IndentedStringBuilder stringBuilder)
{
_stringBuilder = stringBuilder;
_indent = _stringBuilder._indent;
_stringBuilder._indent = 0;
}

public void Dispose()
=> _stringBuilder._indent = _indent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning "
+ DesignStrings.SensitiveInformationWarning
+ @"
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}
Expand Down Expand Up @@ -639,9 +637,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning "
+ DesignStrings.SensitiveInformationWarning
+ @"
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}
Expand Down Expand Up @@ -721,9 +717,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning "
+ DesignStrings.SensitiveInformationWarning
+ @"
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}
Expand Down Expand Up @@ -806,9 +800,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning "
+ DesignStrings.SensitiveInformationWarning
+ @"
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}
Expand Down Expand Up @@ -887,9 +879,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning "
+ DesignStrings.SensitiveInformationWarning
+ @"
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}
Expand Down Expand Up @@ -941,13 +931,52 @@ public void Global_namespace_works()
{
Test(
modelBuilder => modelBuilder.Entity("MyEntity"),
new ModelCodeGenerationOptions
{
ModelNamespace = string.Empty
},
new ModelCodeGenerationOptions { ModelNamespace = string.Empty },
code =>
{
Assert.DoesNotContain("namespace ", code.ContextFile.Code);
AssertFileContents(
@"using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

public partial class TestDbContext : DbContext
{
public TestDbContext()
{
}

public TestDbContext(DbContextOptions<TestDbContext> options)
: base(options)
{
}

public virtual DbSet<MyEntity> MyEntity { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning " + DesignStrings.SensitiveInformationWarning + @"
optionsBuilder.UseSqlServer(""Initial Catalog=TestDatabase"");
}
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyEntity>(entity =>
{
entity.HasNoKey();
});

OnModelCreatingPartial(modelBuilder);
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
",
code.ContextFile);

Assert.DoesNotContain("namespace ", Assert.Single(code.AdditionalFiles).Code);
},
model =>
Expand Down