Do not generate MVC attributes unless necessary

Fixes https://github.com/aspnet/AspNetCore/issues/8161
\n\nCommit migrated from a976b80c7c
This commit is contained in:
Pranav K 2019-04-03 20:59:43 -07:00
parent 01e9d70207
commit 37275e5fd5
9 changed files with 90 additions and 50 deletions

View File

@ -28,30 +28,44 @@ Copyright (c) .NET Foundation. All rights reserved.
</PropertyGroup>
<!--
Configure MVC specific configuration if the application uses a configuration that supports MVC.
Resolve MVC specific configuration depending on the presence of MVC specific Razor files.
-->
<PropertyGroup Condition="'$(AddRazorSupportForMvc)' == 'true'">
<!--
MVC uses a ProvideApplicationPartFactoryAttribute on the generated assembly to load compiled views from assembly. Set this to false, to prevent generating this attribute.
-->
<GenerateProvideApplicationPartFactoryAttribute Condition="'$(GenerateProvideApplicationPartFactoryAttribute)'==''">true</GenerateProvideApplicationPartFactoryAttribute>
<Target Name="_ResolveMvcSpecificRazorConfiguration"
DependsOnTargets="ResolveRazorGenerateInputs"
Condition="'$(Language)' == 'C#' AND '$(AddRazorSupportForMvc)' == 'true'">
<!--
Determines if the Sdk is allowed to add additional attributes to the project assembly.
For instance, MVC will generally want to add attributes to support view discovery and runtime compilation.
-->
<GenerateRazorAssemblyInfo Condition="'$(GenerateRazorAssemblyInfo)'==''">true</GenerateRazorAssemblyInfo>
<Message Text="@(RazorGenerateWithTargetPath)" Importance="High" />
<!--
MVC will generally want to add support for runtime compilation, but only for applications.
-->
<GenerateRazorHostingAssemblyInfo Condition="'$(GenerateRazorHostingAssemblyInfo)'=='' AND '$(OutputType)'=='exe'">$(GenerateRazorAssemblyInfo)</GenerateRazorHostingAssemblyInfo>
<PropertyGroup Condition="'@(RazorGenerate->Count())' != '0'">
<!--
MVC uses a ProvideApplicationPartFactoryAttribute on the generated assembly to load compiled views from assembly. Set this to false, to prevent generating this attribute.
-->
<GenerateProvideApplicationPartFactoryAttribute Condition="'$(GenerateProvideApplicationPartFactoryAttribute)'==''">true</GenerateProvideApplicationPartFactoryAttribute>
<!--
The type name of the ApplicationPartFactory applied to the generated Razor file.
-->
<ProvideApplicationPartFactoryAttributeTypeName Condition="'$(ProvideApplicationPartFactoryAttributeTypeName)' == ''">Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFactory, Microsoft.AspNetCore.Mvc.Razor</ProvideApplicationPartFactoryAttributeTypeName>
</PropertyGroup>
<!--
Determines if the Sdk is allowed to add additional attributes to the project assembly.
For instance, MVC will generally want to add attributes to support view discovery and runtime compilation.
-->
<GenerateRazorAssemblyInfo Condition="'$(GenerateRazorAssemblyInfo)'==''">true</GenerateRazorAssemblyInfo>
<!--
The type name of the ApplicationPartFactory applied to the generated Razor file.
-->
<ProvideApplicationPartFactoryAttributeTypeName Condition="'$(ProvideApplicationPartFactoryAttributeTypeName)' == ''">Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFactory, Microsoft.AspNetCore.Mvc.Razor</ProvideApplicationPartFactoryAttributeTypeName>
</PropertyGroup>
<ItemGroup Condition="'$(GenerateProvideApplicationPartFactoryAttribute)' == 'true' AND '$(ProvideApplicationPartFactoryAttributeTypeName)'!=''">
<RazorTargetAssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute">
<_Parameter1>$(ProvideApplicationPartFactoryAttributeTypeName)</_Parameter1>
</RazorTargetAssemblyAttribute>
</ItemGroup>
<ItemGroup Condition="'$(GenerateRazorAssemblyInfo)'=='true' AND '$(ResolvedRazorCompileToolset)'=='RazorSdk' AND ('$(RazorCompileOnBuild)' == 'true' OR '$(RazorCompileOnPublish)' == 'true')">
<_RazorAssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.RelatedAssemblyAttribute">
<_Parameter1>$(RazorTargetName)</_Parameter1>
</_RazorAssemblyAttribute>
</ItemGroup>
</Target>
<!--
Determine the default Razor configuration
@ -80,16 +94,4 @@ Copyright (c) .NET Foundation. All rights reserved.
</RazorExtension>
</ItemGroup>
<ItemGroup Condition="'$(GenerateRazorAssemblyInfo)'=='true' AND '$(ResolvedRazorCompileToolset)'=='RazorSdk' AND ('$(RazorCompileOnBuild)' == 'true' OR '$(RazorCompileOnPublish)' == 'true')">
<_RazorAssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.RelatedAssemblyAttribute">
<_Parameter1>$(RazorTargetName)</_Parameter1>
</_RazorAssemblyAttribute>
</ItemGroup>
<ItemGroup Condition="'$(GenerateProvideApplicationPartFactoryAttribute)' == 'true' AND '$(ProvideApplicationPartFactoryAttributeTypeName)'!=''">
<RazorTargetAssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute">
<_Parameter1>$(ProvideApplicationPartFactoryAttributeTypeName)</_Parameter1>
</RazorTargetAssemblyAttribute>
</ItemGroup>
</Project>

View File

@ -32,6 +32,11 @@ Copyright (c) .NET Foundation. All rights reserved.
_CreateRazorTargetAssemblyInfoInputsCacheFile;
CoreGenerateRazorTargetAssemblyInfo
</GenerateRazorTargetAssemblyInfoDependsOn>
<GenerateRazorTargetAssemblyInfoDependsOn Condition="'$(_Targeting30OrNewerRazorLangVersion)' == 'true'">
_ResolveMvcSpecificRazorConfiguration;
$(GenerateRazorTargetAssemblyInfoDependsOn)
</GenerateRazorTargetAssemblyInfoDependsOn>
</PropertyGroup>
<Target
@ -156,9 +161,9 @@ Copyright (c) .NET Foundation. All rights reserved.
DependsOnTargets="_CreateRazorAssemblyInfoInputsCacheFile"
Inputs="$(_RazorAssemblyInfoInputsCacheFile)"
Outputs="$(_RazorAssemblyInfo)"
Condition="'@(_RazorAssemblyAttribute)' != ''">
Condition="'$(GenerateRazorAssemblyInfo)' == 'true' AND '@(_RazorAssemblyAttribute)' != ''">
<ItemGroup Condition="'$(GenerateRazorAssemblyInfo)'=='true'">
<ItemGroup>
<Compile Remove="$(_RazorAssemblyInfo)" />
<Compile Include="$(_RazorAssemblyInfo)" />
</ItemGroup>
@ -173,11 +178,18 @@ Copyright (c) .NET Foundation. All rights reserved.
<PropertyGroup>
<!-- Generate attributes in the main assembly if we're targeting a C# project and using the Razor Sdk. -->
<CoreCompileDependsOn Condition="'$(ResolvedRazorCompileToolset)'=='RazorSdk' and '$(Language)' == 'C#' and '$(GenerateRazorAssemblyInfo)' == 'true'">
$(CoreCompileDependsOn);_GenerateRazorAssemblyInfo
<CoreCompileDependsOn Condition="'$(ResolvedRazorCompileToolset)'=='RazorSdk' and '$(Language)' == 'C#'">
$(CoreCompileDependsOn);
_GenerateRazorAssemblyInfo
</CoreCompileDependsOn>
<_GenerateRazorAssemblyInfoDependsOn>PrepareForBuild;_CoreGenerateRazorAssemblyInfo</_GenerateRazorAssemblyInfoDependsOn>
<_GenerateRazorAssemblyInfoDependsOn>RazorGetAssemblyAttributes;PrepareForBuild;_CoreGenerateRazorAssemblyInfo</_GenerateRazorAssemblyInfoDependsOn>
<!-- In 3.0 or later, we need to invoke a target to determine MVC specific configuration. -->
<_GenerateRazorAssemblyInfoDependsOn Condition="'$(_Targeting30OrNewerRazorLangVersion)' == 'true'">
_ResolveMvcSpecificRazorConfiguration;
$(_GenerateRazorAssemblyInfoDependsOn);
</_GenerateRazorAssemblyInfoDependsOn>
</PropertyGroup>
<Target Name="_GenerateRazorAssemblyInfo" DependsOnTargets="$(_GenerateRazorAssemblyInfoDependsOn)" />

View File

@ -407,9 +407,6 @@ Copyright (c) .NET Foundation. All rights reserved.
The SDK expects configurations that use runtime compilation to set $(GenerateRazorHostingAssemblyInfo) to true,
it will be unset by default.
-->
<PropertyGroup>
<_GenerateRazorAssemblyInfoDependsOn>RazorGetAssemblyAttributes;$(_GenerateRazorAssemblyInfoDependsOn)</_GenerateRazorAssemblyInfoDependsOn>
</PropertyGroup>
<Target
Name="RazorGetAssemblyAttributes"

View File

@ -379,9 +379,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(result);
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileDoesNotContain(result, razorAssemblyInfo, "RelatedAssemblyAttribute");
Assert.FileDoesNotExist(result, razorAssemblyInfo);
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.RazorTargetAssemblyInfo.cs");
}
@ -454,8 +452,8 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.BuildPassed(result);
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileDoesNotContain(result, razorAssemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"2.1\")");
Assert.FileDoesNotContain(result, razorAssemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-2-1\")");
Assert.FileDoesNotContain(result, razorAssemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute");
Assert.FileDoesNotContain(result, razorAssemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute");
}
[Fact]

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
[Fact]
[InitializeTestProject("ComponentApp")]
public async Task Build_DoesNotProduceMvcArtifacts_IfAddRazorSupportForMvcIsFalse()
public async Task Build_DoesNotProduceMvcArtifacts_IfProjectDoesNotContainRazorGenerateItems()
{
var result = await DotnetMSBuild("Build");

View File

@ -14,9 +14,9 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
{
}
[Fact]
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_WithMvc_AddsConfigurationMetadata()
public async Task Build_DoesNotAddHostingMetadata_ByDefault()
{
var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.RazorAssemblyInfo.cs");
var result = await DotnetMSBuild("Build");
@ -26,6 +26,37 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.Views.pdb");
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileContainsLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.RelatedAssemblyAttribute(\"SimpleMvc.Views\")]");
Assert.FileDoesNotContainLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"3.0\")]");
Assert.FileDoesNotContainLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-3.0\")]");
Assert.FileDoesNotContainLine(
result,
razorAssemblyInfo,
"[assembly: Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute(\"MVC-3.0\", \"Microsoft.AspNetCore.Mvc.Razor.Extensions\")]");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_WithGenerateRazorHostingAssemblyInfo_AddsConfigurationMetadata()
{
var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.RazorAssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=true");
Assert.BuildPassed(result);
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.Views.dll");
Assert.FileExists(result, IntermediateOutputPath, "SimpleMvc.Views.pdb");
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileContainsLine(
result,

View File

@ -29,7 +29,7 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileDoesNotExist(result, OutputPath, "SimpleMvc.Views.pdb");
// This target should be part of the design time build.
Assert.Contains("RazorGetAssemblyAttributes", result.Output);
Assert.Contains("_GenerateRazorAssemblyInfo", result.Output);
// We don't want to see the expensive Razor targets in the performance summary, since they shouldn't run
// during a design time build.

View File

@ -2,7 +2,7 @@
<PropertyGroup>
<RazorSdkDirectoryRoot>$(RazorSdkArtifactsDirectory)$(Configuration)\sdk-output\</RazorSdkDirectoryRoot>
<_RazorComponentInclude>**\*.cshtml</_RazorComponentInclude>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
<PropertyGroup>