Skip to content

Commit 355354c

Browse files
authored
Merge pull request #148 from TrackableEntities/fix-comments
Support Line Characters in Generated Comments
2 parents 4638f3a + a439c65 commit 355354c

File tree

10 files changed

+51
-12
lines changed

10 files changed

+51
-12
lines changed

sample/ScaffoldingSample/CodeTemplates/CSharpEntityType/Class.hbs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace {{namespace}}
44
{ // Comment
5-
{{#if class-annotation}}
5+
{{#if comment}}
6+
/// <summary>
7+
{{comment}}
8+
/// </summary>
9+
{{/if}}
10+
{{#each class-annotations}}
611
{{{class-annotation}}}
7-
{{/if}}
12+
{{/each}}
813
public partial class {{class}} : {{base-class}} {{my-helper}}
914
{
1015
{{{> constructor}}}

sample/ScaffoldingSample/CodeTemplates/CSharpEntityType/Partials/Properties.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
{{#each properties}}
2+
{{#if property-comment}}
3+
4+
/// <summary>
5+
{{property-comment}}
6+
/// </summary>
7+
{{/if}}
28
{{#each property-annotations}}
39
{{{property-annotation}}}
410
{{/each}}

sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4141
{
4242
entity.ToTable("Category");
4343

44-
entity.HasComment("hello table Customer");
44+
entity.HasComment("Hello table Customer\r\n(Currently not used.)");
4545

4646
entity.Property(e => e.CategoryName)
4747
.IsRequired()
4848
.HasMaxLength(15)
49-
.HasComment("hello CompanyName");
49+
.HasComment("Hello CompanyName\r\n(Currently not used.)");
5050
});
5151

5252
modelBuilder.Entity<dbo.Customer>(entity =>

sample/ScaffoldingSample/Sample.ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Demonstrates how to reverse engineer an existing database using the EF Core tool
1313
- The easiest is to use **LocalDb**, which is installed with Visual Studio.
1414
Connect to: `(localdb)\MsSqlLocalDb`.
1515
- Create a new database named **NorthwindSlim**.
16-
- Download the data file from <http://bit.ly/northwind-slim>.
16+
- Download the `NorthwindSlim.sql` file from <https://github.com/TrackableEntities/northwind-slim>.
1717
- Unzip **NorthwindSlim.sql** and run the script to create tables and populate them with data.
1818

1919
## Setup

sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
2121
public void ConfigureDesignTimeServices(IServiceCollection services)
2222
{
2323
// Uncomment to launch JIT debugger and hit breakpoints
24-
//Debugger.Launch();
24+
Debugger.Launch();
2525

2626
// Add Handlebars scaffolding templates
2727
services.AddHandlebarsScaffolding(options =>
@@ -44,6 +44,9 @@ public void ConfigureDesignTimeServices(IServiceCollection services)
4444
{ "models-namespace", "ScaffoldingSample.Models" },
4545
{ "base-class", "EntityBase" }
4646
};
47+
48+
// Disable comments generation
49+
options.GenerateComments = false;
4750
});
4851

4952
// Register Handlebars helper

src/EntityFrameworkCore.Scaffolding.Handlebars/CodeTemplates/CSharpEntityType/Class.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace {{namespace}}
44
{
55
{{#if comment}}
66
/// <summary>
7-
/// {{comment}}
7+
{{comment}}
88
/// </summary>
99
{{/if}}
1010
{{#each class-annotations}}

src/EntityFrameworkCore.Scaffolding.Handlebars/CodeTemplates/CSharpEntityType/Partials/Properties.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{{#each properties}}
22
{{#if property-comment}}
3+
34
/// <summary>
4-
/// {{property-comment}}
5+
{{property-comment}}
56
/// </summary>
67
{{/if}}
78
{{#each property-annotations}}

src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>5.0.0</Version>
6+
<Version>5.0.1</Version>
77
<Authors>Tony Sneed</Authors>
88
<Company>Tony Sneed</Company>
99
<Title>Entity Framework Core Scaffolding with Handlebars</Title>
@@ -12,7 +12,7 @@
1212
<PackageProjectUrl>https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars</PackageProjectUrl>
1313
<PackageIcon>icon.png</PackageIcon>
1414
<PackageTags>scaffolding reverse-engineer entity-framework-core handlebars</PackageTags>
15-
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v5.0.0</PackageReleaseNotes>
15+
<PackageReleaseNotes>See: https://github.com/TrackableEntities/EntityFrameworkCore.Scaffolding.Handlebars/releases/tag/v5.0.1</PackageReleaseNotes>
1616
<LangVersion>latest</LangVersion>
1717
<IncludeSource>true</IncludeSource>
1818
<SignAssembly>true</SignAssembly>

src/EntityFrameworkCore.Scaffolding.Handlebars/HandlebarsScaffoldingOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@ public class HandlebarsScaffoldingOptions
5656
/// https://docs.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types
5757
/// </summary>
5858
public bool EnableNullableReferenceTypes { get; set; }
59+
60+
/// <summary>
61+
/// Gets or sets whether table and column descriptions generate XML comments.
62+
/// </summary>
63+
public bool GenerateComments { get; set; } = true;
5964
}
6065
}

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using JetBrains.Annotations;
1313
using Microsoft.EntityFrameworkCore;
1414
using Microsoft.EntityFrameworkCore.Design;
15+
using Microsoft.EntityFrameworkCore.Infrastructure;
1516
using Microsoft.EntityFrameworkCore.Metadata;
1617
using Microsoft.EntityFrameworkCore.Metadata.Internal;
1718
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
@@ -173,7 +174,8 @@ protected override void GenerateClass(IEntityType entityType)
173174

174175
var transformedEntityName = EntityTypeTransformationService.TransformTypeEntityName(entityType.Name);
175176

176-
TemplateData.Add("comment", entityType.GetComment());
177+
if (_options?.Value?.GenerateComments == true)
178+
TemplateData.Add("comment", GenerateComment(entityType.GetComment(), 1));
177179
TemplateData.Add("class", transformedEntityName);
178180

179181
GenerateConstructor(entityType);
@@ -241,7 +243,7 @@ protected override void GenerateProperties(IEntityType entityType)
241243
{ "property-type", propertyType },
242244
{ "property-name", property.Name },
243245
{ "property-annotations", PropertyAnnotationsData },
244-
{ "property-comment", property.GetComment() },
246+
{ "property-comment", _options?.Value?.GenerateComments == true ? GenerateComment(property.GetComment(), 2) : null },
245247
{ "property-isnullable", property.IsNullable },
246248
{ "nullable-reference-types", _options?.Value?.EnableNullableReferenceTypes == true }
247249
});
@@ -567,6 +569,23 @@ private void GenerateInversePropertyAttribute(INavigation navigation)
567569
}
568570
}
569571

572+
private string GenerateComment(string comment, int indents)
573+
{
574+
var sb = new IndentedStringBuilder();
575+
if (!string.IsNullOrWhiteSpace(comment))
576+
{
577+
for (int i = 0; i < indents; i++)
578+
sb.IncrementIndent();
579+
foreach (var line in comment.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None))
580+
{
581+
sb.AppendLine($"/// {System.Security.SecurityElement.Escape(line)}");
582+
}
583+
for (int i = 0; i < indents; i++)
584+
sb.DecrementIndent();
585+
}
586+
return sb.ToString().Trim(Environment.NewLine.ToCharArray());
587+
}
588+
570589
private class AttributeWriter
571590
{
572591
private readonly string _attibuteName;

0 commit comments

Comments
 (0)