Skip to content

Commit 22ee63b

Browse files
author
Anthony Sneed
committed
Refactor HbsCSharpScaffoldingGeneratorTests.
1 parent f82fa22 commit 22ee63b

22 files changed

+209
-1695
lines changed

src/EntityFrameworkCore.Scaffolding.Handlebars/FileSystemTemplateFileService.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
/* using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
43
using System.Reflection;
5-
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
4+
using EntityFrameworkCore.Scaffolding.Handlebars.Internal;
65

76
namespace EntityFrameworkCore.Scaffolding.Handlebars
87
{
@@ -58,4 +57,4 @@ public virtual string RetrieveTemplateFileContents(string relativeDirectory, str
5857
return contents;
5958
}
6059
}
61-
} */
60+
}

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,13 @@ public class HbsCSharpDbContextGenerator : ICSharpDbContextGenerator
5151
/// <param name="dbContextTemplateService">Template service for DbContext generator.</param>
5252
/// <param name="cSharpHelper">CSharp helper.</param>
5353
public HbsCSharpDbContextGenerator(
54-
//IScaffoldingProviderCodeGenerator providerCodeGenerator,
55-
//IAnnotationCodeGenerator annotationCodeGenerator,
5654
#pragma warning disable CS0618 // Type or member is obsolete
5755
IEnumerable<IScaffoldingProviderCodeGenerator> legacyProviderCodeGenerators,
5856
#pragma warning restore CS0618 // Type or member is obsolete
5957
IEnumerable<IProviderConfigurationCodeGenerator> providerCodeGenerators,
6058
IAnnotationCodeGenerator annotationCodeGenerator,
6159
IDbContextTemplateService dbContextTemplateService,
62-
ICSharpHelper cSharpHelper
63-
)
60+
ICSharpHelper cSharpHelper)
6461
{
6562
if (!legacyProviderCodeGenerators.Any() && !providerCodeGenerators.Any())
6663
{

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpEntityTypeGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ public class HbsCSharpEntityTypeGenerator : ICSharpEntityTypeGenerator
3838
/// <summary>
3939
/// Constructor for the Handlebars entity types generator.
4040
/// </summary>
41-
/// <param name="cSharpHelper">CSharp helper.</param>
4241
/// <param name="entityTypeTemplateService">Template service for the entity types generator.</param>
42+
/// <param name="cSharpHelper">CSharp helper.</param>
4343
public HbsCSharpEntityTypeGenerator(
44-
ICSharpHelper cSharpHelper,
45-
IEntityTypeTemplateService entityTypeTemplateService)
44+
IEntityTypeTemplateService entityTypeTemplateService,
45+
ICSharpHelper cSharpHelper)
4646
{
4747
_code = cSharpHelper ?? throw new ArgumentNullException(nameof(cSharpHelper));
4848
EntityTypeTemplateService = entityTypeTemplateService ?? throw new ArgumentNullException(nameof(entityTypeTemplateService));

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpModelGenerator.cs

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
// Modifications copyright(C) 2018 Tony Sneed.
55

6+
using System;
67
using System.IO;
8+
using EntityFrameworkCore.Scaffolding.Handlebars.Helpers;
79
using Microsoft.EntityFrameworkCore.Metadata;
810
using Microsoft.EntityFrameworkCore.Metadata.Internal;
911
using Microsoft.EntityFrameworkCore.Scaffolding;
@@ -18,6 +20,16 @@ public class HbsCSharpModelGenerator : ModelCodeGenerator
1820
{
1921
private const string FileExtension = ".cs";
2022

23+
/// <summary>
24+
/// DbContext template service.
25+
/// </summary>
26+
public virtual IDbContextTemplateService DbContextTemplateService { get; }
27+
28+
/// <summary>
29+
/// Entity type template service.
30+
/// </summary>
31+
public virtual IEntityTypeTemplateService EntityTypeTemplateService { get; }
32+
2133
/// <summary>
2234
/// DbContext generator.
2335
/// </summary>
@@ -32,14 +44,20 @@ public class HbsCSharpModelGenerator : ModelCodeGenerator
3244
/// Constructor for the HbsCSharpModelGenerator.
3345
/// </summary>
3446
/// <param name="dependencies">Service dependencies parameter class for HbsCSharpModelGenerator.</param>
47+
/// <param name="dbContextTemplateService">Template service for DbContext generator.</param>
48+
/// <param name="entityTypeTemplateService">Template service for the entity types generator.</param>
3549
/// <param name="cSharpDbContextGenerator">DbContext generator.</param>
3650
/// <param name="cSharpEntityTypeGenerator">Entity type generator.</param>
3751
public HbsCSharpModelGenerator(ModelCodeGeneratorDependencies dependencies,
52+
IDbContextTemplateService dbContextTemplateService,
53+
IEntityTypeTemplateService entityTypeTemplateService,
3854
ICSharpDbContextGenerator cSharpDbContextGenerator,
3955
ICSharpEntityTypeGenerator cSharpEntityTypeGenerator) : base(dependencies)
4056
{
41-
CSharpDbContextGenerator = cSharpDbContextGenerator;
42-
CSharpEntityTypeGenerator = cSharpEntityTypeGenerator;
57+
DbContextTemplateService = dbContextTemplateService ?? throw new ArgumentNullException(nameof(dbContextTemplateService));
58+
EntityTypeTemplateService = entityTypeTemplateService ?? throw new ArgumentNullException(nameof(entityTypeTemplateService));
59+
CSharpDbContextGenerator = cSharpDbContextGenerator ?? throw new ArgumentNullException(nameof(cSharpDbContextGenerator));
60+
CSharpEntityTypeGenerator = cSharpEntityTypeGenerator ?? throw new ArgumentNullException(nameof(cSharpEntityTypeGenerator));
4361
}
4462

4563
/// <summary>Generates code for a model.</summary>
@@ -57,19 +75,40 @@ public override ScaffoldedModel GenerateModel(IModel model,
5775
string connectionString,
5876
ModelCodeGenerationOptions options)
5977
{
78+
if (model == null) throw new ArgumentNullException(nameof(model));
79+
if (string.IsNullOrWhiteSpace(@namespace)) throw new ArgumentNullException(nameof(@namespace));
80+
if (contextDir == null) throw new ArgumentNullException(nameof(contextDir));
81+
if (string.IsNullOrWhiteSpace(contextName)) throw new ArgumentNullException(nameof(contextName));
82+
if (string.IsNullOrWhiteSpace(connectionString)) throw new ArgumentNullException(nameof(connectionString));
83+
if (contextDir == null) throw new ArgumentNullException(nameof(contextDir));
84+
if (options == null) throw new ArgumentNullException(nameof(options));
85+
86+
// Register Hbs helpers and partial templates
87+
DbContextTemplateService.RegisterHelper(Constants.SpacesHelper, HandlebarsHelpers.GetSpacesHelper());
88+
DbContextTemplateService.RegisterPartialTemplates();
89+
EntityTypeTemplateService.RegisterPartialTemplates();
90+
6091
var resultingFiles = new ScaffoldedModel();
6192

62-
var generatedCode = CSharpDbContextGenerator.WriteCode(model, @namespace, contextName, connectionString, options.UseDataAnnotations, options.SuppressConnectionStringWarning);
93+
string generatedCode;
6394

64-
var dbContextFileName = contextName + FileExtension;
65-
resultingFiles.ContextFile = new ScaffoldedFile { Path = Path.Combine(contextDir, dbContextFileName), Code = generatedCode };
95+
if (!(CSharpDbContextGenerator is NullCSharpDbContextGenerator))
96+
{
97+
generatedCode = CSharpDbContextGenerator.WriteCode(model, @namespace, contextName, connectionString, options.UseDataAnnotations, options.SuppressConnectionStringWarning);
98+
99+
var dbContextFileName = contextName + FileExtension;
100+
resultingFiles.ContextFile = new ScaffoldedFile { Path = Path.Combine(contextDir, dbContextFileName), Code = generatedCode };
101+
}
66102

67-
foreach (var entityType in model.GetEntityTypes())
103+
if (!(CSharpEntityTypeGenerator is NullCSharpEntityTypeGenerator))
68104
{
69-
generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, @namespace, options.UseDataAnnotations);
105+
foreach (var entityType in model.GetEntityTypes())
106+
{
107+
generatedCode = CSharpEntityTypeGenerator.WriteCode(entityType, @namespace, options.UseDataAnnotations);
70108

71-
var entityTypeFileName = entityType.DisplayName() + FileExtension;
72-
resultingFiles.AdditionalFiles.Add(new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode });
109+
var entityTypeFileName = entityType.DisplayName() + FileExtension;
110+
resultingFiles.AdditionalFiles.Add(new ScaffoldedFile { Path = entityTypeFileName, Code = generatedCode });
111+
}
73112
}
74113

75114
return resultingFiles;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
// Modifications copyright(C) 2018 Tony Sneed.
5+
6+
using System;
7+
using System.IO;
8+
using System.Text;
9+
10+
namespace EntityFrameworkCore.Scaffolding.Handlebars.Internal
11+
{
12+
/// <summary>
13+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
14+
/// directly from your code. This API may change or be removed in future releases.
15+
/// </summary>
16+
public class FileSystemFileService : IFileService
17+
{
18+
/// <summary>
19+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
20+
/// directly from your code. This API may change or be removed in future releases.
21+
/// </summary>
22+
public virtual bool DirectoryExists(string directoryName)
23+
{
24+
return Directory.Exists(directoryName);
25+
}
26+
27+
/// <summary>
28+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
29+
/// directly from your code. This API may change or be removed in future releases.
30+
/// </summary>
31+
public virtual bool FileExists(string directoryName, string fileName)
32+
{
33+
return File.Exists(Path.Combine(directoryName, fileName));
34+
}
35+
36+
/// <summary>
37+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
38+
/// directly from your code. This API may change or be removed in future releases.
39+
/// </summary>
40+
public virtual bool IsFileReadOnly(string directoryName, string fileName)
41+
{
42+
string path = Path.Combine(directoryName, fileName);
43+
if (File.Exists(path))
44+
return File.GetAttributes(path).HasFlag((Enum)FileAttributes.ReadOnly);
45+
return false;
46+
}
47+
48+
/// <summary>
49+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
50+
/// directly from your code. This API may change or be removed in future releases.
51+
/// </summary>
52+
public virtual string RetrieveFileContents(string directoryName, string fileName)
53+
{
54+
return File.ReadAllText(Path.Combine(directoryName, fileName));
55+
}
56+
57+
/// <summary>
58+
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
59+
/// directly from your code. This API may change or be removed in future releases.
60+
/// </summary>
61+
public virtual string OutputFile(string directoryName, string fileName, string contents)
62+
{
63+
Directory.CreateDirectory(directoryName);
64+
string path = Path.Combine(directoryName, fileName);
65+
File.WriteAllText(path, contents, Encoding.UTF8);
66+
return path;
67+
}
68+
}
69+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{> dbimports}}
2+
3+
namespace {{namespace}}
4+
{
5+
public partial class {{class}} : DbContext
6+
{
7+
{{> dbsets}}
8+
{{#if entity-type-errors}}
9+
{{#each entity-type-errors}}
10+
{{spaces 8}}{{{entity-type-error}}}
11+
{{/each}}
12+
13+
{{/if}}
14+
15+
{{{on-configuring}}}
16+
{{{on-model-creating}}}
17+
}
18+
}

test/Scaffolding.Handlebars.Tests/ExpectedContexts.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2121
if (!optionsBuilder.IsConfigured)
2222
{
2323
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
24-
optionsBuilder.UseSqlServer(@""Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindTestDb; Integrated Security=True"");
24+
optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=NorthwindTestDb; Integrated Security=True"");
2525
}
2626
}
2727
@@ -75,7 +75,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
7575
if (!optionsBuilder.IsConfigured)
7676
{
7777
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
78-
optionsBuilder.UseSqlServer(@""Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindTestDb; Integrated Security=True"");
78+
optionsBuilder.UseSqlServer(""Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=NorthwindTestDb; Integrated Security=True"");
7979
}
8080
}
8181

test/Scaffolding.Handlebars.Tests/ExpectedEntities.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ public Category()
3131
3232
namespace FakeNamespace
3333
{
34-
public partial class Category
34+
public partial class Product
3535
{
36-
public Category()
37-
{
38-
Product = new HashSet<Product>();
39-
}
40-
41-
public int CategoryId { get; set; }
42-
public string CategoryName { get; set; }
36+
public int ProductId { get; set; }
37+
public string ProductName { get; set; }
38+
public decimal? UnitPrice { get; set; }
39+
public bool Discontinued { get; set; }
40+
public byte[] RowVersion { get; set; }
41+
public int? CategoryId { get; set; }
4342
44-
public ICollection<Product> Product { get; set; }
45-
}
43+
public Category Category { get; set; }
4644
}
45+
}
4746
";
4847
}
4948

@@ -86,14 +85,14 @@ namespace FakeNamespace
8685
public partial class Product
8786
{
8887
public int ProductId { get; set; }
89-
public int? CategoryId { get; set; }
90-
public bool Discontinued { get; set; }
9188
[Required]
9289
[StringLength(40)]
9390
public string ProductName { get; set; }
94-
public byte[] RowVersion { get; set; }
9591
[Column(TypeName = ""money"")]
9692
public decimal? UnitPrice { get; set; }
93+
public bool Discontinued { get; set; }
94+
public byte[] RowVersion { get; set; }
95+
public int? CategoryId { get; set; }
9796
9897
[ForeignKey(""CategoryId"")]
9998
[InverseProperty(""Product"")]

test/Scaffolding.Handlebars.Tests/Fakes/FakeAnnotationCodeGenerator.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)