-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Add Microsoft.EntityFrameworkCore.Templates #28678
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageType>Template</PackageType> | ||
<PackageId>Microsoft.EntityFrameworkCore.Templates</PackageId> | ||
<Description> | ||
Entity Framework Core templates for dotnet-new. | ||
|
||
Enables these commonly used templates: | ||
ef-templates | ||
</Description> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<IncludeContentInPack>true</IncludeContentInPack> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<ContentTargetFolders>content</ContentTargetFolders> | ||
<NoWarn>$(NoWarn);NU5128</NoWarn> | ||
<NoDefaultExcludes>true</NoDefaultExcludes> | ||
<IncludeSymbols>false</IncludeSymbols> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Content Include="templates\**\*" Exclude="templates\**\bin\**;templates\**\obj\**" /> | ||
<Compile Remove="**\*" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<GeneratedContent Include="..\EFCore.Design\Scaffolding\Internal\CSharpDbContextGenerator.tt"> | ||
<PackagePath>content\templates\ef-templates\CodeTemplates\EFCore\DbContext.t4</PackagePath> | ||
<IsDbContext>true</IsDbContext> | ||
</GeneratedContent> | ||
<GeneratedContent Include="..\EFCore.Design\Scaffolding\Internal\CSharpEntityTypeGenerator.tt"> | ||
<PackagePath>content\templates\ef-templates\CodeTemplates\EFCore\EntityType.t4</PackagePath> | ||
</GeneratedContent> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" /> | ||
</ItemGroup> | ||
|
||
<UsingTask TaskName="PatchTemplate" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"> | ||
<ParameterGroup> | ||
<TemplateFile ParameterType="System.String" Required="true" /> | ||
<OutputPath ParameterType="System.String" Required="true" /> | ||
<IsDbContext ParameterType="System.Boolean" /> | ||
<VersionPrefix ParameterType="System.String" /> | ||
</ParameterGroup> | ||
<Task> | ||
<Using Namespace="System.IO" /> | ||
<Code Type="Fragment" Language="cs"> | ||
<![CDATA[ | ||
const string importLine = @"<#@ import namespace=""Microsoft.EntityFrameworkCore.Infrastructure"" #>"; | ||
|
||
var linePragmasRemoved = false; | ||
var importAdded = !IsDbContext; | ||
var versionCheckAdded = !IsDbContext; | ||
|
||
Directory.CreateDirectory(Path.GetDirectoryName(OutputPath)); | ||
|
||
using (var reader = new StreamReader(TemplateFile)) | ||
using (var writer = new StreamWriter(OutputPath)) | ||
{ | ||
string line; | ||
while ((line = reader.ReadLine()) != null) | ||
{ | ||
if (!linePragmasRemoved | ||
&& line.StartsWith("<#@ template ")) | ||
{ | ||
writer.WriteLine(line.Replace(@" linePragmas=""false""", "")); | ||
|
||
linePragmasRemoved = true; | ||
} | ||
else if (!importAdded | ||
&& line.StartsWith("<#@ import ") | ||
&& !line.Contains(@" namespace=""System.") | ||
&& string.Compare(line, importLine) > 0 | ||
&& !line.Contains(@" namespace=""Microsoft.EntityFrameworkCore""")) | ||
{ | ||
writer.WriteLine(importLine); | ||
writer.WriteLine(line); | ||
|
||
importAdded = true; | ||
} | ||
else if (!versionCheckAdded | ||
&& line == "<#") | ||
{ | ||
var version = new Version(VersionPrefix); | ||
|
||
writer.WriteLine(line); | ||
writer.WriteLine(@" if (!ProductInfo.GetVersion().StartsWith(""" + version.Major + "." + version.Minor + @"""))"); | ||
writer.WriteLine(@" {"); | ||
writer.WriteLine(@" Warning(""Your templates were created using an older version of Entity Framework. Additional features and bug fixes may be available. See https://aka.ms/efcore-docs-updating-templates for more information."");"); | ||
writer.WriteLine(@" }"); | ||
writer.WriteLine(); | ||
|
||
versionCheckAdded = true; | ||
} | ||
else | ||
{ | ||
writer.WriteLine(line); | ||
} | ||
} | ||
} | ||
|
||
if (!linePragmasRemoved | ||
|| !importAdded | ||
|| !versionCheckAdded) | ||
{ | ||
Log.LogError("Failed to patch the template files. Update the code in EFCore.Templates.csproj"); | ||
|
||
return false; | ||
} | ||
]]> | ||
</Code> | ||
</Task> | ||
</UsingTask> | ||
|
||
<PropertyGroup> | ||
<BeforePack>$(BeforePack);GenerateContent</BeforePack> | ||
</PropertyGroup> | ||
|
||
<Target Name="GenerateContent"> | ||
<PatchTemplate TemplateFile="%(GeneratedContent.Identity)" | ||
OutputPath="$(IntermediateOutputPath)%(GeneratedContent.PackagePath)" | ||
IsDbContext="%(GeneratedContent.IsDbContext)" | ||
VersionPrefix="$(VersionPrefix)"/> | ||
<ItemGroup> | ||
<Content Include="$(IntermediateOutputPath)%(GeneratedContent.PackagePath)" PackagePath="$([System.IO.Path]::GetDirectoryName(%(GeneratedContent.PackagePath)))" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
12 changes: 12 additions & 0 deletions
12
src/EFCore.Templates/templates/ef-templates/.template.config/template.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/template", | ||
"author": "Microsoft", | ||
"classifications": [ "EFCore", "Scaffolding" ], | ||
"identity": "Microsoft.EntityFrameworkCore.Templates.ef-templates", | ||
"name": "Entity Framework Core Scaffolding Templates", | ||
"shortName": "ef-templates", | ||
"tags": { | ||
"language": "C#", | ||
"type": "item" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about inlining the task source here, we should discuss
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I considered a lot of alternatives. This one seemed the most maintainable.