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

Scaffolding: Enable text templates #27565

Merged
1 commit merged into from
Mar 9, 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
1 change: 1 addition & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ updates:
- dependency-name: Microsoft.Data.SqlClient
- dependency-name: Microsoft.DotNet.PlatformAbstractions
- dependency-name: mod_spatialite
- dependency-name: Mono.TextTemplating
- dependency-name: NetTopologySuite*
- dependency-name: Newtonsoft.Json
- dependency-name: SQLitePCLRaw*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
using Microsoft.EntityFrameworkCore.Migrations.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.TextTemplating;
using Microsoft.EntityFrameworkCore.TextTemplating.Internal;

namespace Microsoft.EntityFrameworkCore.Design;

Expand Down Expand Up @@ -52,7 +54,9 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
.TryAddSingleton(reporter)
.TryAddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
.TryAddSingleton<IMigrationsCodeGeneratorSelector, MigrationsCodeGeneratorSelector>()
.TryAddSingleton<IModelCodeGenerator, CSharpModelGenerator>()
.TryAddSingleton<ITextTemplating, TextTemplatingService>()
.TryAddSingletonEnumerable<IModelCodeGenerator, TextTemplatingModelGenerator>()
.TryAddSingletonEnumerable<IModelCodeGenerator, CSharpModelGenerator>()
.TryAddSingleton<IModelCodeGeneratorSelector, ModelCodeGeneratorSelector>()
.TryAddSingleton<ICompiledModelCodeGenerator, CSharpRuntimeModelCodeGenerator>()
.TryAddSingleton<ICompiledModelCodeGeneratorSelector, CompiledModelCodeGeneratorSelector>()
Expand Down
3 changes: 2 additions & 1 deletion src/EFCore.Design/Design/Internal/DatabaseOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public virtual SavedModelFiles ScaffoldContext(
UseNullableReferenceTypes = _nullable,
ContextDir = MakeDirRelative(outputDir, outputContextDir),
ContextName = dbContextClassName,
SuppressOnConfiguring = suppressOnConfiguring
SuppressOnConfiguring = suppressOnConfiguring,
ProjectDir = _projectDir
});

return scaffolder.Save(
Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Design/EFCore.Design.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<ItemGroup>
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
<PackageReference Include="Microsoft.Extensions.HostFactoryResolver.Sources" PrivateAssets="All" Version="$(MicrosoftExtensionsHostFactoryResolverSourcesVersion)" />
<PackageReference Include="Mono.TextTemplating" Version="2.2.1" />
</ItemGroup>
bricelam marked this conversation as resolved.
Show resolved Hide resolved

<ItemGroup>
Expand Down
23 changes: 21 additions & 2 deletions src/EFCore.Design/Properties/DesignStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 34 additions & 28 deletions src/EFCore.Design/Properties/DesignStrings.resx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -189,6 +189,9 @@
<data name="DuplicateMigrationName" xml:space="preserve">
<value>The name '{migrationName}' is used by an existing migration.</value>
</data>
<data name="EncodingIgnored" xml:space="preserve">
<value>The encoding '{encoding}' specified in the output directive will be ignored. EF Core always scaffolds files using the encoding 'utf-8'.</value>
</data>
<data name="ErrorConnecting" xml:space="preserve">
<value>An error occurred while accessing the database. Continuing without the information provided by the database. Error: {message}</value>
</data>
Expand Down Expand Up @@ -378,6 +381,9 @@ Change your target project to the migrations project by using the Package Manage
<data name="UnhandledEnumValue" xml:space="preserve">
<value>Unhandled enum value '{enumValue}'.</value>
</data>
<data name="UnknownDirectiveProcessor" xml:space="preserve">
<value>Failed to resolve type for directive processor {name}.</value>
</data>
<data name="UnknownLiteral" xml:space="preserve">
<value>Cannot scaffold C# literals of type '{literalType}'. The provider should implement CoreTypeMapping.GenerateCodeLiteral to support using it at design time.</value>
</data>
Expand Down Expand Up @@ -420,4 +426,4 @@ Change your target project to the migrations project by using the Package Manage
<data name="WritingSnapshot" xml:space="preserve">
<value>Writing model snapshot to '{file}'.</value>
</data>
</root>
</root>
11 changes: 11 additions & 0 deletions src/EFCore.Design/Scaffolding/IModelCodeGeneratorSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,16 @@ public interface IModelCodeGeneratorSelector
/// </summary>
/// <param name="language">The programming language.</param>
/// <returns>The <see cref="IModelCodeGenerator" />.</returns>
[Obsolete("Use the overload that takes ModelCodeGenerationOptions instead.")]
IModelCodeGenerator Select(string? language);

/// <summary>
/// Selects an <see cref="IModelCodeGenerator" /> service for a given set of options.
/// </summary>
/// <param name="options">The options.</param>
/// <returns>The <see cref="IModelCodeGenerator" />.</returns>
IModelCodeGenerator Select(ModelCodeGenerationOptions options)
#pragma warning disable CS0618 // Type or member is obsolete
=> Select(options.Language);
#pragma warning restore CS0618
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,22 @@ namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal;
/// </summary>
public class ModelCodeGeneratorSelector : LanguageBasedSelector<IModelCodeGenerator>, IModelCodeGeneratorSelector
{
private readonly IEnumerable<TemplatedModelGenerator> _templatedModelGenerators;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ModelCodeGeneratorSelector(IEnumerable<IModelCodeGenerator> services)
: base(services)
{
}
: base(services.Except(services.OfType<TemplatedModelGenerator>()).ToList())
=> _templatedModelGenerators = services.OfType<TemplatedModelGenerator>().ToList();

/// <inheritdoc />
public virtual IModelCodeGenerator Select(ModelCodeGenerationOptions options)
=> _templatedModelGenerators
.Where(g => options.ProjectDir != null && g.HasTemplates(options.ProjectDir))
.LastOrDefault()
?? Select(options.Language);
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public virtual ScaffoldedModel ScaffoldModel(
: DefaultDbContextName;
}

var codeGenerator = ModelCodeGeneratorSelector.Select(codeOptions.Language);
var codeGenerator = ModelCodeGeneratorSelector.Select(codeOptions);

return codeGenerator.GenerateModel(model, codeOptions);
}
Expand Down
Loading