Exclude RazorComponent items from RazorGenerate item group

Interim solution to allow components to share the .cshtml extension. When declared in
the project file, the SDK will prevent RazorComponent items from being included
in the RazorGenerate itemgroup.
\n\nCommit migrated from 92e2c70b69
This commit is contained in:
Pranav K 2019-01-07 15:09:56 -08:00
parent e6e8fba39c
commit 500bdc1fd4
6 changed files with 34 additions and 19 deletions

View File

@ -367,7 +367,10 @@ Copyright (c) .NET Foundation. All rights reserved.
</ItemGroup>
<ItemGroup Condition="'$(EnableDefaultRazorGenerateItems)'=='true'">
<RazorGenerate Include="@(Content)" Condition="'%(Content.Extension)'=='.cshtml'" />
<RazorGenerate
Include="@(Content)"
Exclude="@(RazorComponent)"
Condition="'%(Content.Extension)'=='.cshtml'" />
</ItemGroup>
<!--

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
@ -384,18 +385,37 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
}
assemblyPath = Path.Combine(result.Project.DirectoryPath, Path.Combine(assemblyPath));
var typeNames = GetDeclaredTypeNames(assemblyPath);
Assert.Contains(fullTypeName, typeNames);
}
public static void AssemblyDoesNotContainType(MSBuildResult result, string assemblyPath, string fullTypeName)
{
if (result == null)
{
throw new ArgumentNullException(nameof(result));
}
var typeNames = GetDeclaredTypeNames(assemblyPath);
Assert.DoesNotContain(fullTypeName, typeNames);
}
private static IEnumerable<string> GetDeclaredTypeNames(string assemblyPath)
{
IEnumerable<string> typeNames;
using (var file = File.OpenRead(assemblyPath))
{
var peReader = new PEReader(file);
var metadataReader = peReader.GetMetadataReader();
var typeNames = metadataReader.TypeDefinitions.Where(t => !t.IsNil).Select(t =>
typeNames = metadataReader.TypeDefinitions.Where(t => !t.IsNil).Select(t =>
{
var type = metadataReader.GetTypeDefinition(t);
return metadataReader.GetString(type.Namespace) + "." + metadataReader.GetString(type.Name);
});
Assert.Contains(fullTypeName, typeNames);
}
return typeNames;
}
private abstract class MSBuildXunitException : Xunit.Sdk.XunitException

View File

@ -36,6 +36,8 @@ namespace Microsoft.AspNetCore.Razor.Design.IntegrationTests
Assert.AssemblyContainsType(result, Path.Combine(OutputPath, "MvcWithComponents.dll"), "MvcWithComponents.TestComponent");
Assert.AssemblyContainsType(result, Path.Combine(OutputPath, "MvcWithComponents.dll"), "MvcWithComponents.Views.Shared.NavMenu");
Assert.AssemblyContainsType(result, Path.Combine(OutputPath, "MvcWithComponents.dll"), "MvcWithComponents.Components.Counter");
Assert.AssemblyDoesNotContainType(result, Path.Combine(OutputPath, "MvcWithComponents.dll"), "MvcWithComponents.Views.Home.Index");
}
}
}

View File

@ -27,28 +27,13 @@
<Reference Include="$(BinariesRoot)\Microsoft.AspNetCore.Razor.Test.ComponentShim.dll"/>
</ItemGroup>
<!--
BEGIN COMPONENT .cshtml WORKAROUND
This is the current workaround used by the components codebase to treat .cshtml files as components
so that we can have tooling support. Testing it here so it won't break.
-->
<ItemGroup>
<RazorComponent Include="@(Content->WithMetadataValue('Extension', '.cshtml'))" />
</ItemGroup>
<Target Name="_RemoveFilesFromRazorGenerate" AfterTargets="ResolveRazorGenerateInputs">
<ItemGroup>
<RazorGenerate Remove="@(RazorGenerate)" />
</ItemGroup>
</Target>
<ItemGroup>
<ProjectCapability Include="DotNetCoreRazorConfiguration" />
</ItemGroup>
<!--
END COMPONENT .cshtml WORKAROUND
-->
<!-- Test Placeholder -->

View File

@ -0,0 +1 @@
This file should produce a component

View File

@ -29,6 +29,10 @@
<Reference Include="$(BinariesRoot)\Microsoft.AspNetCore.Razor.Test.MvcShim.ClassLib.dll"/>
</ItemGroup>
<ItemGroup>
<RazorComponent Include="Components/**/*.cshtml" />
</ItemGroup>
<!-- Test Placeholder -->
</Project>