RazorSdk - Attribute code generation fixes

* Allow the type name for ProvideApplicationPartFactoryAttribute to be specified.

* Generate ReleatedAssemblyAttribute in class library projects
This commit is contained in:
Pranav K 2018-03-14 23:09:28 -07:00 committed by GitHub
parent f9ad346be8
commit 5f69a01cc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 6 deletions

View File

@ -11,6 +11,12 @@
-->
<RazorDefaultConfiguration Condition="'$(RazorDefaultConfiguration)'==''">MVC-2.1</RazorDefaultConfiguration>
<!--
Determines the load behavior for Razor compiled views in an Mvc application. By default, the compiled assembly is initialized as an Mvc application part.
This results in compiled views being discovered and routes for compiled Razor Pages being set up. Setting this value to false, disables this behavior.
-->
<EnableDefaultCompiledViewAssemblyLoadBehavior Condition="'$(EnableDefaultCompiledViewAssemblyLoadBehavior)' == ''">true</EnableDefaultCompiledViewAssemblyLoadBehavior>
<!-- Override for testing. This path is only correct inside a nuget package. -->
<_MvcExtensionAssemblyPath Condition="'$(_MvcExtensionAssemblyPath)'==''">$(MSBuildThisFileDirectory)..\..\lib\netstandard2.0\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll</_MvcExtensionAssemblyPath>
</PropertyGroup>
@ -32,4 +38,4 @@
<AssemblyFilePath>$(_MvcExtensionAssemblyPath)</AssemblyFilePath>
</RazorExtension>
</ItemGroup>
</Project>
</Project>

View File

@ -4,7 +4,9 @@
<!--
MVC will generally want to add support for runtime compilation, but only for applications.
-->
<GenerateRazorAssemblyInfo Condition="'$(GenerateRazorAssemblyInfo)'=='' and '$(OutputType)'=='Exe'">true</GenerateRazorAssemblyInfo>
<GenerateRazorAssemblyInfo Condition="'$(GenerateRazorAssemblyInfo)'==''">true</GenerateRazorAssemblyInfo>
<GenerateRazorHostingAssemblyInfo Condition="'$(GenerateRazorHostingAssemblyInfo)'=='' AND '$(OutputType)'=='exe'">$(GenerateRazorAssemblyInfo)</GenerateRazorHostingAssemblyInfo>
<!--
Use the suffix .Views when producing compiled view assemblies. This matches the requirements for Mvc's ViewsFeatureProvider.
@ -12,15 +14,21 @@
<RazorTargetNameSuffix Condition="'$(RazorTargetNameSuffix)'==''">.Views</RazorTargetNameSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(ProvideApplicationPartFactoryAttributeTypeName)' == ''">
<ProvideApplicationPartFactoryAttributeTypeName Condition="'$(EnableDefaultCompiledViewAssemblyLoadBehavior)'=='false'">Microsoft.AspNetCore.Mvc.ApplicationParts.NullApplicationPartFactory, Microsoft.AspNetCore.Mvc.Core</ProvideApplicationPartFactoryAttributeTypeName>
<ProvideApplicationPartFactoryAttributeTypeName Condition="'$(EnableDefaultCompiledViewAssemblyLoadBehavior)'=='true'">Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFactory, Microsoft.AspNetCore.Mvc.Razor</ProvideApplicationPartFactoryAttributeTypeName>
</PropertyGroup>
<ItemGroup Condition="'$(GenerateRazorAssemblyInfo)'=='true' AND '$(ResolvedRazorCompileToolset)'=='RazorSdk' AND ('$(RazorCompileOnBuild)' == 'true' OR '$(RazorCompileOnPublish)' == 'true')">
<AssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.RelatedAssemblyAttribute">
<_Parameter1>$(RazorTargetName)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ItemGroup Condition="'$(ProvideApplicationPartFactoryAttributeTypeName)'!=''">
<RazorAssemblyAttribute Include="Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute">
<_Parameter1>Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFactory, Microsoft.AspNetCore.Mvc.Razor</_Parameter1>
<_Parameter1>$(ProvideApplicationPartFactoryAttributeTypeName)</_Parameter1>
</RazorAssemblyAttribute>
</ItemGroup>
</Project>

View File

@ -76,6 +76,9 @@ Copyright (c) .NET Foundation. All rights reserved.
<RazorAssemblyAttribute Include="System.Reflection.AssemblyProductAttribute" Condition="'$(Product)' != '' and '$(GenerateAssemblyProductAttribute)' == 'true'">
<_Parameter1>$(Product)</_Parameter1>
</RazorAssemblyAttribute>
<RazorAssemblyAttribute Include="System.Resources.NeutralResourcesLanguageAttribute" Condition="'$(NeutralLanguage)' != '' and '$(GenerateNeutralResourcesLanguageAttribute)' == 'true'">
<_Parameter1>$(NeutralLanguage)</_Parameter1>
</RazorAssemblyAttribute>
<RazorAssemblyAttribute Include="System.Reflection.AssemblyFileVersionAttribute" Condition="'$(RazorAssemblyFileVersion)' != '' and '$(GenerateAssemblyFileVersionAttribute)' == 'true'">
<_Parameter1>$(RazorAssemblyFileVersion)</_Parameter1>

View File

@ -241,13 +241,13 @@ Copyright (c) .NET Foundation. All rights reserved.
This allows the project file to act as the source of truth for the applicable Razor configuration regardless
of how Razor is used.
The SDK expects configurations that use runtime compilation to set $(GenerateRazorAssemblyInfo) to true,
The SDK expects configurations that use runtime compilation to set $(GenerateRazorHostingAssemblyInfo) to true,
it will be unset by default.
-->
<Target
Name="RazorGetAssemblyAttributes"
AfterTargets="GetAssemblyAttributes"
Condition="'$(GenerateRazorAssemblyInfo)'=='true' and '$(RazorDefaultConfiguration)'!=''"
Condition="'$(GenerateRazorHostingAssemblyInfo)'=='true' and '$(RazorDefaultConfiguration)'!=''"
DependsOnTargets="ResolveRazorConfiguration">
<ItemGroup>
<AssemblyAttribute Include="Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute">

View File

@ -367,6 +367,32 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileContains(result, razorAssemblyInfoPath, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"Microsoft.AspNetCore.Mvc.ApplicationParts.CompiledRazorAssemblyApplicationPartFac\"");
}
[Fact]
[InitializeTestProject("ClassLibrary")]
public async Task Build_UsesSpecifiedApplicationPartFactoryTypeName()
{
var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.RazorAssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:ProvideApplicationPartFactoryAttributeTypeName=CustomFactory");
Assert.BuildPassed(result);
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileContains(result, razorAssemblyInfo, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"CustomFactory\"");
}
[Fact]
[InitializeTestProject("ClassLibrary")]
public async Task Build_UsesNullFactory_IfEnableDefaultCompiledViewAssemblyLoadBehaviorIsSetToFalse()
{
var razorAssemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.RazorAssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:EnableDefaultCompiledViewAssemblyLoadBehavior=false");
Assert.BuildPassed(result);
Assert.FileExists(result, razorAssemblyInfo);
Assert.FileContains(result, razorAssemblyInfo, "[assembly: Microsoft.AspNetCore.Mvc.ApplicationParts.ProvideApplicationPartFactoryAttribute(\"Microsoft.AspNetCore.Mvc.ApplicationParts.NullApplicationPartFactory");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_DoesNotAddRelatedAssemblyPart_IfToolSetIsNotRazorSdk()
@ -412,6 +438,50 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.FileDoesNotExist(result, IntermediateOutputPath, "SimpleMvc.RazorAssemblyInfo.cs");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_WithGenerateRazorHostingAssemblyInfoFalse_DoesNotGenerateHostingAttributes()
{
var assemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.AssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=false");
Assert.BuildPassed(result);
Assert.FileExists(result, assemblyInfo);
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute");
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute");
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute");
}
[Fact]
[InitializeTestProject("ClassLibrary")]
public async Task Build_DoesNotGenerateHostingAttributes_InClassLibraryProjects()
{
var assemblyInfo = Path.Combine(IntermediateOutputPath, "ClassLibrary.AssemblyInfo.cs");
var result = await DotnetMSBuild("Build");
Assert.BuildPassed(result);
Assert.FileExists(result, assemblyInfo);
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute");
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute");
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorExtensionAssemblyNameAttribute");
}
[Fact]
[InitializeTestProject("SimpleMvc")]
public async Task Build_GeneratesHostingAttributesByDefault()
{
var assemblyInfo = Path.Combine(IntermediateOutputPath, "SimpleMvc.AssemblyInfo.cs");
var result = await DotnetMSBuild("Build", "/p:GenerateRazorHostingAssemblyInfo=false");
Assert.BuildPassed(result);
Assert.FileExists(result, assemblyInfo);
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorLanguageVersionAttribute(\"2.1\")");
Assert.FileDoesNotContain(result, assemblyInfo, "Microsoft.AspNetCore.Razor.Hosting.RazorConfigurationNameAttribute(\"MVC-2-1\")");
}
private static DependencyContext ReadDependencyContext(string depsFilePath)
{
var reader = new DependencyContextJsonReader();