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

Refactor compiled models MSBuild integration. #33700

Merged
merged 1 commit into from
May 13, 2024
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
9 changes: 6 additions & 3 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ public virtual IReadOnlyList<string> Optimize(string? outputDir, string? modelNa
/// </summary>
public virtual DbContext CreateContext(string? contextType)
{
var factory = FindContextType(contextType).Value;
var contextPair = FindContextType(contextType);
var factory = contextPair.Value;
try
{
var context = factory();
_reporter.WriteVerbose(DesignStrings.UseContext(context.GetType().ShortDisplayName()));
contextType = context.GetType().ShortDisplayName();
_reporter.WriteVerbose(DesignStrings.UseContext(contextType));

var loggerFactory = context.GetService<ILoggerFactory>();
loggerFactory.AddProvider(new OperationLoggerProvider(_reporter));
Expand All @@ -226,7 +228,8 @@ public virtual DbContext CreateContext(string? contextType)
ex = ex.InnerException!;
}

throw new OperationException(DesignStrings.CannotCreateContextInstance(contextType, ex.Message), ex);
throw new OperationException(DesignStrings.CannotCreateContextInstance(
contextType ?? contextPair.Key.GetType().ShortDisplayName(), ex.Message), ex);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/EFCore.Tasks/EFCore.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<GenerateDependencyFile>true</GenerateDependencyFile>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<DevelopmentDependency>true</DevelopmentDependency>
<NoWarn>NU5100;NU5128</NoWarn>
<ImplicitUsings>true</ImplicitUsings>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)..\..\rulesets\EFCore.noxmldocs.ruleset</CodeAnalysisRuleSet>
Expand Down
2 changes: 2 additions & 0 deletions src/EFCore.Tasks/PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Install the package into your project, set `<EFOptimizeContext Condition="'$(Con

If the startup project is different from the current project it needs to be specified: `<EFStartupProject>..\Startup\Startup.csproj</EFStartupProject>`

The startup project must also reference the `Microsoft.EntityFrameworkCore.Tasks` package.

## Getting started with EF Core

See [Getting started with EF Core](https://learn.microsoft.com/ef/core/get-started/overview/install) for more information about EF NuGet packages, including which to install when getting started.
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Tasks/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@
<value>Startup project '{startupProject}' targets framework '.NETCoreApp' version '{targetFrameworkVersion}'. This version of the Entity Framework Core Tools only supports version 2.0 or higher. For information on using older versions of the tools, see https://go.microsoft.com/fwlink/?linkid=871254</value>
</data>
<data name="NETStandardStartupProject" xml:space="preserve">
<value>Startup project '{startupProject}' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Tools with this project, add an executable project targeting .NET Core that references this project, and set it as the startup project using --startup-project; or, update this project to cross-target .NET Core. For more information on using the Entity Framework Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034781</value>
<value>Startup project '{startupProject}' targets framework '.NETStandard'. There is no runtime associated with this framework, and projects targeting it cannot be executed directly. To use the Entity Framework Core Tools with this project, add an executable project targeting .NET Core that references this project, and set it as the startup project using &lt;EFStartupProject&gt; property; or, update this project to cross-target .NET Core. For more information on using the Entity Framework Tools with .NET Standard projects, see https://go.microsoft.com/fwlink/?linkid=2034781</value>
</data>
<data name="NotExecutableStartupProject" xml:space="preserve">
<value>Startup project '{startupProject}' cannot be executed directly. To use the Entity Framework Core Tools with this project, add an executable project targeting .NET Core that references this project, and set it as the startup project using --startup-project.</value>
<value>Startup project '{startupProject}' cannot be executed directly. To use the Entity Framework Core Tools with this project, add an executable project targeting .NET Core that references this project, and set it as the startup project using &lt;EFStartupProject&gt; property.</value>
</data>
<data name="UnsupportedFramework" xml:space="preserve">
<value>Startup project '{startupProject}' targets framework '{targetFramework}'. The Entity Framework Core Tools don't support this framework. See https://aka.ms/efcore-docs-cli-tfms for more information.</value>
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Tasks/Tasks/Internal/OperationTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override bool ValidateParameters()
var targetFramework = new FrameworkName(TargetFrameworkMoniker);
if (targetFramework.Identifier == ".NETStandard")
{
Log.LogError(Resources.NETStandardStartupProject(startupAssemblyName));
Log.LogError(Resources.NETStandardStartupProject(startupAssemblyName));
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Tasks;
/// <summary>
/// Generates files that contain tailored code for some runtime services.
/// </summary>
public class OptimizeContext : OperationTaskBase
public class OptimizeDbContext : OperationTaskBase
{
/// <summary>
/// The name of the target DbContext.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,9 @@
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' == 'core'">net8.0</_TaskTargetFramework>
<_TaskTargetFramework Condition="'$(MSBuildRuntimeType)' != 'core'">net472</_TaskTargetFramework>
<_EFCustomTasksAssembly>$([MSBuild]::NormalizePath($(MSBuildThisFileDirectory), '..\tasks\$(_TaskTargetFramework)\$(MSBuildThisFileName).dll'))</_EFCustomTasksAssembly>
<EFOptimizeContext Condition="'$(EFOptimizeContext)'==''">false</EFOptimizeContext>
<EFStartupProject Condition="'$(EFStartupProject)'==''">$(MSBuildProjectFullPath)</EFStartupProject>
</PropertyGroup>

<UsingTask TaskName="$(MSBuildThisFileName).OptimizeContext" AssemblyFile="$(_EFCustomTasksAssembly)"/>

<PropertyGroup>
<EFOptimizeContext Condition="'$(EFOptimizeContext)' == ''">false</EFOptimizeContext>
</PropertyGroup>

<PropertyGroup Condition="'$(EFTargetLanguage)' == ''">
<EFTargetLanguage Condition="'$(MSBuildProjectExtension)' == '.csproj'">C#</EFTargetLanguage>
<EFTargetLanguage Condition="'$(MSBuildProjectExtension)' == '.vbproj'">VB</EFTargetLanguage>
<EFTargetLanguage Condition="'$(MSBuildProjectExtension)' == '.fsproj'">F#</EFTargetLanguage>
</PropertyGroup>
<UsingTask TaskName="$(MSBuildThisFileName).OptimizeDbContext" AssemblyFile="$(_EFCustomTasksAssembly)"/>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,165 @@
<Project ToolsVersion="17.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<EFGeneratedFilesList Condition="'$(EFGeneratedFilesList)' == ''">$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(IntermediateOutputPath)$(AssemblyName).EFGeneratedFiles.txt'))</EFGeneratedFilesList>
<_FullOutputPath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(OutputPath)'))</_FullOutputPath>
<_FullIntermediateOutputPath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(IntermediateOutputPath)'))</_FullIntermediateOutputPath>
<EFGeneratedSourcesFile Condition="'$(EFGeneratedSourcesFile)' == ''">$(_FullIntermediateOutputPath)$(AssemblyName).EFGeneratedSources.txt</EFGeneratedSourcesFile>
<EFProjectsToOptimizePath Condition="'$(EFProjectsToOptimizePath)' == ''">$(_FullIntermediateOutputPath)EFProjectsToOptimize\</EFProjectsToOptimizePath>
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).dll</_AssemblyFullName>
<CoreCompileDependsOn>$(CoreCompileDependsOn);_EFPrepareForCompile</CoreCompileDependsOn>
</PropertyGroup>

<!-- Invokes OptimizeContext on the startup project to generate files if needed -->
<Target Name="_GenerateFiles"
BeforeTargets="_ReadGeneratedFilesList"
Condition="'$(EFOptimizeContext)'=='true'">
<PropertyGroup>
<EFStartupProject Condition="'$(EFStartupProject)'==''">$(MSBuildProjectFullPath)</EFStartupProject>
<EFRootNamespace Condition="'$(EFRootNamespace)'==''">$(RootNamespace)</EFRootNamespace>
<EFRootNamespace Condition="'$(EFRootNamespace)'==''">$(AssemblyName)</EFRootNamespace>
<EFTargetNamespace Condition="'$(EFTargetNamespace)'==''">$(EFRootNamespace)</EFTargetNamespace>
<_FullOutputPath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(OutputPath)'))</_FullOutputPath>
<_FullIntermediateOutputPath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(IntermediateOutputPath)'))</_FullIntermediateOutputPath>
<EFNullable>false</EFNullable>
</PropertyGroup>
<PropertyGroup Condition="'$(OutputType)'=='Exe' Or '$(OutputType)'=='WinExe'">
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).exe</_AssemblyFullName>
</PropertyGroup>

<PropertyGroup Condition="'$(Nullable)'=='enable' Or '$(Nullable)'=='annotations'">
<EFNullable>true</EFNullable>
</PropertyGroup>
<!-- Invokes OptimizeDbContext on projects that had changes since the last time they were optimized -->
<Target Name="_EFGenerateFiles"
AfterTargets="Build"
Condition="Exists($(EFProjectsToOptimizePath))">
<ItemGroup>
<_EFProjectsToOptimizeFiles Include="$(EFProjectsToOptimizePath)*.*" />
</ItemGroup>

<PropertyGroup Condition="'$(OutputType)'=='Library'">
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).dll</_AssemblyFullName>
</PropertyGroup>
<PropertyGroup Condition="'$(OutputType)'=='Exe'">
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).exe</_AssemblyFullName>
</PropertyGroup>
<PropertyGroup Condition="'$(OutputType)'=='WinExe'">
<_AssemblyFullName>$(_FullOutputPath)$(AssemblyName).exe</_AssemblyFullName>
</PropertyGroup>
<ReadLinesFromFile File="%(_EFProjectsToOptimizeFiles.Identity)">
<Output TaskParameter="Lines" ItemName="_EFProjectsToOptimize"/>
</ReadLinesFromFile>

<RemoveDir Directories="$(EFProjectsToOptimizePath)" />

<!-- The startup assembly used for file generation should be compiled without using AOT mode -->
<MSBuild Projects="$(MSBuildProjectFullPath)"
Targets="Build"
BuildInParallel="true"
Condition="'$(EFStartupProject)'!='$(MSBuildProjectFullPath)'"
Properties="Configuration=$(Configuration);Platform=$(Platform);PublishAot=false;EFOptimizeContext=false" />
Condition="'$(PublishAot)'=='true'"
Properties="Configuration=$(Configuration);Platform=$(Platform);EFOptimizeContext=false;PublishAot=false" />

<MSBuild Projects="$(EFStartupProject)"
Targets="OptimizeContext"
<MSBuild Projects="@(_EFProjectsToOptimize)"
Targets="OptimizeDbContext"
BuildInParallel="true"
Properties="Configuration=$(Configuration);Platform=$(Platform);PublishAot=false;EFOptimizeContext=false;EFGeneratedFilesListToWrite=$(EFGeneratedFilesList);DbContextAssembly=$(_AssemblyFullName);DbContextName=$(DbContextName);EFRootNamespace=$(EFRootNamespace);EFTargetNamespace=$(EFTargetNamespace);EFTargetLanguage=$(EFTargetLanguage);EFNullable=$(EFNullable);EFOutputDir=$(_FullIntermediateOutputPath);EFProjectDir=$(MSBuildProjectDirectory)">
<Output TaskParameter="TargetOutputs" ItemName="_GeneratedFiles" />
</MSBuild>
Properties="Configuration=$(Configuration);Platform=$(Platform);EFOptimizeContext=false;EFStartupAssembly=$(_AssemblyFullName)" />

<Message Text="Generated files: @(_GeneratedFiles)" Importance="low" />
<ItemGroup>
<_EFProjectsToOptimize Remove="$(MSBuildProjectFullPath)" />
</ItemGroup>

<!-- This assumes that the optimized projects are dependencies, so the current project needs to be recompiled too -->
<MSBuild Projects="$(MSBuildProjectFullPath)"
Targets="Build"
BuildInParallel="true"
Condition="@(_EFProjectsToOptimize->Count()) &gt; 0"
Properties="Configuration=$(Configuration);Platform=$(Platform);EFOptimizeContext=false" />
</Target>

<Target Name="OptimizeContext"
DependsOnTargets="Build"
Inputs="$(DbContextAssembly)"
Outputs="$(EFGeneratedFilesListToWrite)"
Returns="$(_GeneratedFiles)">
<Target Name="OptimizeDbContext"
Inputs="$(_AssemblyFullName)"
Outputs="$(EFGeneratedSourcesFile)"
Returns="$(_EFGeneratedFiles)">
<PropertyGroup>
<_FullOutputPath>$([MSBuild]::NormalizePath($(MSBuildProjectDirectory), '$(OutputPath)'))</_FullOutputPath>
<EFRootNamespace Condition="'$(EFRootNamespace)'==''">$(RootNamespace)</EFRootNamespace>
<EFRootNamespace Condition="'$(EFRootNamespace)'==''">$(AssemblyName)</EFRootNamespace>
<EFTargetNamespace Condition="'$(EFTargetNamespace)'==''">$(EFRootNamespace)</EFTargetNamespace>
<EFOutputDir Condition="'$(EFOutputDir)'==''">$(_FullIntermediateOutputPath)</EFOutputDir>
<EFNullable>false</EFNullable>
</PropertyGroup>

<PropertyGroup Condition="'$(OutputType)'=='Library'">
<_StartupAssemblyFullName>$(_FullOutputPath)$(AssemblyName).dll</_StartupAssemblyFullName>
</PropertyGroup>
<PropertyGroup Condition="'$(OutputType)'=='Exe'">
<_StartupAssemblyFullName>$(_FullOutputPath)$(AssemblyName).exe</_StartupAssemblyFullName>
</PropertyGroup>
<PropertyGroup Condition="'$(OutputType)'=='WinExe'">
<_StartupAssemblyFullName>$(_FullOutputPath)$(AssemblyName).exe</_StartupAssemblyFullName>
<PropertyGroup Condition="'$(Nullable)'=='enable' Or '$(Nullable)'=='annotations'">
<EFNullable>true</EFNullable>
</PropertyGroup>

<ItemGroup>
<MyItemGroup Remove="@(_GeneratedFiles)"/>
</ItemGroup>

<OptimizeContext Assembly="$(DbContextAssembly)"
StartupAssembly="$(_StartupAssemblyFullName)"
ProjectAssetsFile="$(ProjectAssetsFile)"
RuntimeFrameworkVersion="$(RuntimeFrameworkVersion)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
DbContextName="$(DbContextName)"
RootNamespace="$(EFRootNamespace)"
TargetNamespace="$(EFTargetNamespace)"
Language="$(EFTargetLanguage)"
Nullable="$(EFNullable)"
OutputDir="$(EFOutputDir)"
ProjectDir="$(EFProjectDir)">
<Output TaskParameter="GeneratedFiles" PropertyName="_GeneratedFiles" />
</OptimizeContext>
<OptimizeDbContext Assembly="$(_AssemblyFullName)"
StartupAssembly="$(EFStartupAssembly)"
ProjectAssetsFile="$(ProjectAssetsFile)"
RuntimeFrameworkVersion="$(RuntimeFrameworkVersion)"
TargetFrameworkMoniker="$(TargetFrameworkMoniker)"
DbContextName="$(DbContextName)"
RootNamespace="$(EFRootNamespace)"
TargetNamespace="$(EFTargetNamespace)"
Language="$(Language)"
Nullable="$(EFNullable)"
OutputDir="$(EFOutputDir)"
ProjectDir="$(MSBuildProjectDirectory)">
<Output TaskParameter="GeneratedFiles" PropertyName="_EFGeneratedFiles" />
</OptimizeDbContext>

<Delete Files="$(EFGeneratedSourcesFile)" />

<CallTarget Targets="Build"/>

<WriteLinesToFile File="$(EFGeneratedSourcesFile)" Lines="$(_EFGeneratedFiles)"/>
</Target>

<!-- Read the previously generated files if the files weren't regenerated -->
<Target Name="_ReadGeneratedFilesList"
BeforeTargets="_CompileGeneratedFiles"
Condition="Exists($(EFGeneratedFilesList))">
<ReadLinesFromFile File="$(EFGeneratedFilesList)" Condition="@(_GeneratedFiles->Count()) == 0">
<Target Name="_EFReadGeneratedFilesList"
BeforeTargets="_EFProcessGeneratedFiles;_EFCleanupGeneratedFiles"
Condition="'$(EFOptimizeContext)'=='true' And Exists($(EFGeneratedSourcesFile))">
<ReadLinesFromFile File="$(EFGeneratedSourcesFile)">
<Output TaskParameter="Lines" ItemName="_ReadGeneratedFiles"/>
</ReadLinesFromFile>
<Message Text="Found previously generated files: @(_ReadGeneratedFiles)" Importance="low" Condition="@(_GeneratedFiles->Count()) == 0" />

<ItemGroup>
<_GeneratedFiles Include="@(_ReadGeneratedFiles)" />
<_EFGeneratedFiles Include="@(_ReadGeneratedFiles)" />
</ItemGroup>
</Target>

<!-- Adds the generated files to compilation -->
<Target Name="_CompileGeneratedFiles"
<Target Name="_EFProcessGeneratedFiles"
BeforeTargets="CoreCompile">
<ItemGroup>
<Compile Include="@(_GeneratedFiles)" />
<Compile Include="@(_EFGeneratedFiles)" />
</ItemGroup>
</Target>

<!-- Writes the list of generated files with a newer timestamp than the assembly compiled with them -->
<Target Name="_WriteGeneratedFilesList"
AfterTargets="Build"
Condition="'$(EFOptimizeContext)'=='true' And @(_GeneratedFiles->Count()) &gt; 0">
<Message Text="Writing generated files list to: $(EFGeneratedFilesList)" Importance="low" />
<!-- Removes the outdated generated files from compilation and registers this project for optimization
This target has the same Inputs and Outputs as CoreCompile to run only if CoreCompile isn't going to be skipped -->
<Target Name="_EFPrepareForCompile"
DependsOnTargets="_EFProcessGeneratedFiles"
Condition="'$(EFOptimizeContext)'=='true'"
Inputs="$(MSBuildAllProjects);
@(Compile);
@(_CoreCompileResourceInputs);
$(ApplicationIcon);
$(KeyOriginatorFile);
@(ReferencePathWithRefAssemblies);
@(CompiledLicenseFile);
@(LinkResource);
@(EmbeddedDocumentation);
$(Win32Resource);
$(Win32Manifest);
@(CustomAdditionalCompileInputs);
$(ResolvedCodeAnalysisRuleSet);
@(AdditionalFiles);
@(EmbeddedFiles);
@(Analyzer);
@(EditorConfigFiles);
$(SourceLink)"
Outputs="@(DocFileItem);
@(IntermediateAssembly);
@(IntermediateRefAssembly);
@(_DebugSymbolsIntermediatePath);
$(NonExistentFile);
@(CustomAdditionalCompileOutputs)">
<ItemGroup>
<Compile Remove="@(_EFGeneratedFiles)" />
</ItemGroup>

<MSBuild Projects="$(EFStartupProject)"
Targets="_EFRegisterProjectToOptimize"
Properties="Configuration=$(Configuration);Platform=$(Platform);_EFProjectToOptimize=$(MSBuildProjectFullPath)" />
</Target>

<Target Name="_EFRegisterProjectToOptimize">
<PropertyGroup>
<_ProjectName>$([System.IO.Path]::GetFileName('$(_EFProjectToOptimize)'))</_ProjectName>
</PropertyGroup>
<MakeDir Directories="$(EFProjectsToOptimizePath)" />
<WriteLinesToFile File="$(EFProjectsToOptimizePath)$(_ProjectName).txt" Lines="$(_EFProjectToOptimize)" Overwrite="true"/>
</Target>

<WriteLinesToFile File="$(EFGeneratedFilesList)" Lines="@(_GeneratedFiles)" Overwrite="true"/>
<Target Name="_EFCleanGeneratedFiles" AfterTargets="Clean">
<Delete Files="@(_EFGeneratedFiles)" />
<Delete Files="$(EFGeneratedSourcesFile)" />
<RemoveDir Directories="$(EFProjectsToOptimizePath)" />
</Target>

</Project>
Loading
Loading