Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit c39218f

Browse files
author
N. Taylor Mullen
committed
Consume first TFM for TagHelperDescriptor resolution.
- This makes RazorTooling consistent with the .cshtml editor. - Added `project.json` tests to validate new behavior of throwing an appropriate error/selecting the first TFM. #49
1 parent 2097280 commit c39218f

7 files changed

Lines changed: 84 additions & 20 deletions

File tree

src/dotnet-razor-tooling/Internal/ResolveProtocolCommand.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ internal static void Register(CommandLineApplication app)
3434
});
3535
}
3636

37-
/// <summary>
38-
/// Internal for testing.
39-
/// </summary>
4037
public static int ResolveProtocol(int clientProtocol, int pluginProtocol)
4138
{
4239
// Protocols start at 1 and increase.

src/dotnet-razor-tooling/Internal/ResolveTagHelpersCommand.cs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Globalization;
67
using System.Linq;
8+
using dotnet_razor_tooling;
79
using Microsoft.AspNetCore.Razor;
810
using Microsoft.AspNetCore.Razor.Compilation.TagHelpers;
911
using Microsoft.DotNet.Cli.Utils;
@@ -30,26 +32,16 @@ internal static void Register(CommandLineApplication app)
3032
"[project]",
3133
"Path to the project.json for the project resolving TagHelperDescriptors.",
3234
multipleValues: false);
33-
var framework = config.Argument(
34-
"[framework]",
35-
"The framework to resolve TagHelperDescriptors for.",
36-
multipleValues: false);
3735
var assemblyNames = config.Argument(
3836
"[assemblyName]",
3937
"Assembly name to resolve TagHelperDescriptors in.",
4038
multipleValues: true);
4139

4240
config.OnExecute(() =>
4341
{
44-
var projectValue = project.Value;
45-
var frameworkValue = framework.Value;
46-
var projectContexts = ProjectContext.CreateContextForEachFramework(projectValue);
47-
var startupProjectContext = projectContexts
48-
.First(frameworkContext => string.Equals(
49-
frameworkContext.TargetFramework.Framework,
50-
frameworkValue,
51-
StringComparison.OrdinalIgnoreCase));
52-
var assemblyLoadContext = startupProjectContext.CreateLoadContext();
42+
var projectFilePath = project.Value;
43+
var projectContext = ResolveProjectContext(projectFilePath);
44+
var assemblyLoadContext = projectContext.CreateLoadContext();
5345
var protocol = protocolOption.HasValue() ?
5446
int.Parse(protocolOption.Value()) :
5547
AssemblyTagHelperDescriptorResolver.DefaultProtocolVersion;
@@ -82,6 +74,20 @@ internal static void Register(CommandLineApplication app)
8274
});
8375
}
8476

77+
public static ProjectContext ResolveProjectContext(string projectFilePath)
78+
{
79+
var projectContexts = ProjectContext.CreateContextForEachFramework(projectFilePath);
80+
var projectContext = projectContexts.FirstOrDefault();
81+
82+
if (projectContext == null)
83+
{
84+
throw new InvalidOperationException(
85+
string.Format(CultureInfo.CurrentCulture, Resources.InvalidProjectFile, projectFilePath));
86+
}
87+
88+
return projectContext;
89+
}
90+
8591
private class ResolvedTagHelperDescriptorsResult
8692
{
8793
public IEnumerable<TagHelperDescriptor> Descriptors { get; set; }

src/dotnet-razor-tooling/Resources.Designer.cs

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dotnet-razor-tooling/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="InvalidProjectFile" xml:space="preserve">
121+
<value>Could not resolve any target frameworks for file '{0}'.</value>
122+
</data>
120123
<data name="InvalidProtocolValue" xml:space="preserve">
121124
<value>'{0}'s cannot be resolved with protocol '{1}'. Protocol not supported.</value>
122125
</data>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Globalization;
6+
using dotnet_razor_tooling;
7+
using Microsoft.AspNetCore.Tooling.Razor.Internal;
8+
using Xunit;
9+
10+
namespace Microsoft.AspNetCore.Tooling.Razor
11+
{
12+
public class ResolveTagHelpersCommandTest
13+
{
14+
[Fact]
15+
public void ResolveProjectContext_ThrowsWhenNoTargetFrameworks()
16+
{
17+
// Arrange
18+
var projectFilePath = "TestFiles/notfmproject.json";
19+
var expectedErrorMessage = string.Format(CultureInfo.CurrentCulture, Resources.InvalidProjectFile, projectFilePath);
20+
21+
// Act & Assert
22+
var ex = Assert.Throws<InvalidOperationException>(() => ResolveTagHelpersCommand.ResolveProjectContext(projectFilePath));
23+
Assert.Equal(expectedErrorMessage, ex.Message);
24+
}
25+
26+
[Fact]
27+
public void ResolveProjectContext_ResolvesProjectContextsCorrectly()
28+
{
29+
// Arrange
30+
var projectFilePath = "TestFiles/dnxcoreproject.json";
31+
32+
// Act
33+
var projectContext = ResolveTagHelpersCommand.ResolveProjectContext(projectFilePath);
34+
35+
// Assert
36+
Assert.Equal("DNXCore", projectContext.TargetFramework.Framework);
37+
}
38+
}
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"frameworks": {
3+
"dnxcore50": { },
4+
"net451": { }
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"frameworks": {
3+
}
4+
}

0 commit comments

Comments
 (0)