From 14a815991919c21e657d47da1280b8c82095d7f8 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Thu, 27 Dec 2018 22:15:23 -0800 Subject: [PATCH] Enable component class libraries in SDK This change contains the enabling features to use Razor Components in a class library. Currently we require a few workarounds, see the test project file. This is good enough to get things unblocked. One part that was needed was to register the correct component features in the rzc. This is a good example, of why we don't like to add new features that get registered conditionally, it's error-prone :) The other part that was needed was to make some of the MVC-related features for assembly attributes conditional on the TFM. We need to be able to use the 3.0 (all inclusive) SDK, but without the MVC-related features. This isn't the right heuristic, but it gets us unblocked. \n\nCommit migrated from https://github.com/dotnet/aspnetcore-tooling/commit/8dbf02076b6105f1da7946072ba76abf7e3bca20 --- .../src/DiscoverCommand.cs | 5 ++ .../src/GenerateCommand.cs | 2 + ...rosoft.NET.Sdk.Razor.Configuration.targets | 16 +++--- .../BuildIncrementalismTest.cs | 2 +- .../IntegrationTests/BuildIntegrationTest.cs | 16 ++++++ .../ClassLibrary/ClassLibrary.csproj | 2 + .../testassets/ComponentLibrary/Class1.cs | 12 +++++ .../ComponentLibrary/ComponentLibrary.csproj | 51 +++++++++++++++++++ .../ComponentLibrary/GenericComponent.razor | 8 +++ .../ComponentLibrary/MyComponent.cshtml | 1 + 10 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/Razor/test/testassets/ComponentLibrary/Class1.cs create mode 100644 src/Razor/test/testassets/ComponentLibrary/ComponentLibrary.csproj create mode 100644 src/Razor/test/testassets/ComponentLibrary/GenericComponent.razor create mode 100644 src/Razor/test/testassets/ComponentLibrary/MyComponent.cshtml diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/DiscoverCommand.cs b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/DiscoverCommand.cs index 3ba5eb5ab5..65329ee46b 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/DiscoverCommand.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/DiscoverCommand.cs @@ -142,6 +142,11 @@ namespace Microsoft.AspNetCore.Razor.Tools b.Features.Add(new CompilationTagHelperFeature()); b.Features.Add(new DefaultTagHelperDescriptorProvider()); b.Features.Add(new ComponentTagHelperDescriptorProvider()); + b.Features.Add(new BindTagHelperDescriptorProvider()); + b.Features.Add(new EventHandlerTagHelperDescriptorProvider()); + b.Features.Add(new RefTagHelperDescriptorProvider()); + + b.Features.Add(new DefaultTypeNameFeature()); }); var feature = engine.Engine.Features.OfType().Single(); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs index f32075f5bb..e912b8ec26 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Tools/src/GenerateCommand.cs @@ -7,6 +7,7 @@ using System.Diagnostics; using System.IO; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; +using Microsoft.CodeAnalysis.Razor; using Microsoft.Extensions.CommandLineUtils; using Microsoft.VisualStudio.LanguageServices.Razor.Serialization; using Newtonsoft.Json; @@ -169,6 +170,7 @@ namespace Microsoft.AspNetCore.Razor.Tools var engine = RazorProjectEngine.Create(configuration, compositeFileSystem, b => { b.Features.Add(new StaticTagHelperFeature() { TagHelpers = tagHelpers, }); + b.Features.Add(new DefaultTypeNameFeature()); if (GenerateDeclaration.HasValue()) { diff --git a/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Configuration.targets b/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Configuration.targets index f4a4284ca4..7b27791129 100644 --- a/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Configuration.targets +++ b/src/Razor/Microsoft.NET.Sdk.Razor/src/build/netstandard2.0/Microsoft.NET.Sdk.Razor.Configuration.targets @@ -10,6 +10,11 @@ Copyright (c) .NET Foundation. All rights reserved. *********************************************************************************************** --> + + + <_TargetingNETCoreApp30OrLater Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' AND '$(_TargetFrameworkVersionWithoutV)' > '2.9'">true + + - true + $(_TargetingNETCoreApp30OrLater) - true + $(_TargetingNETCoreApp30OrLater) + false + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Razor/test/testassets/ComponentLibrary/GenericComponent.razor b/src/Razor/test/testassets/ComponentLibrary/GenericComponent.razor new file mode 100644 index 0000000000..5037fef5c0 --- /dev/null +++ b/src/Razor/test/testassets/ComponentLibrary/GenericComponent.razor @@ -0,0 +1,8 @@ +@typeparam TItem + +

@Title - @Item

+ +@functions { + [Parameter] TItem Item { get; set; } + [Parameter] string Title { get; set; } +} \ No newline at end of file diff --git a/src/Razor/test/testassets/ComponentLibrary/MyComponent.cshtml b/src/Razor/test/testassets/ComponentLibrary/MyComponent.cshtml new file mode 100644 index 0000000000..e1a56ee3ae --- /dev/null +++ b/src/Razor/test/testassets/ComponentLibrary/MyComponent.cshtml @@ -0,0 +1 @@ + \ No newline at end of file