Get the correct version of Mvc for multitargeting

This commit is contained in:
Ajay Bhargav Baaskaran 2017-08-04 15:56:59 -07:00
parent 5161ebaf8a
commit dbcac41be7
4 changed files with 51 additions and 1 deletions

View File

@ -2,7 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Host;
using Mvc1_X = Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X;
using MvcLatest = Microsoft.AspNetCore.Mvc.Razor.Extensions;
@ -11,6 +14,7 @@ namespace Microsoft.CodeAnalysis.Razor
{
internal class DefaultTemplateEngineFactoryService : RazorTemplateEngineFactoryService
{
private const string MvcAssemblyName = "Microsoft.AspNetCore.Mvc.Razor";
private static readonly Version LatestSupportedMvc = new Version(2, 1, 0);
private readonly HostLanguageServices _services;
@ -70,8 +74,36 @@ namespace Microsoft.CodeAnalysis.Razor
private Version GetMvcVersion(string projectPath)
{
// TODO: Need to figure out the actual referenced Mvc version in the project. https://github.com/aspnet/Razor/issues/1183
var workspace = _services.WorkspaceServices.Workspace;
var project = workspace.CurrentSolution.Projects.FirstOrDefault(p =>
{
var directory = Path.GetDirectoryName(p.FilePath);
return string.Equals(
NormalizeDirectoryPath(directory),
NormalizeDirectoryPath(projectPath),
StringComparison.OrdinalIgnoreCase);
});
if (project != null)
{
var compilation = CSharpCompilation.Create(project.AssemblyName).AddReferences(project.MetadataReferences);
foreach (var identity in compilation.ReferencedAssemblyNames)
{
if (identity.Name == MvcAssemblyName)
{
return identity.Version;
}
}
}
return null;
}
private string NormalizeDirectoryPath(string path)
{
return path.Replace('\\', '/').TrimEnd('/');
}
}
}

View File

@ -153,6 +153,14 @@
<IncludeOutputGroupsInVSIXLocalOnly>
</IncludeOutputGroupsInVSIXLocalOnly>
</ProjectReference>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X\Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.csproj">
<Name>Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X</Name>
<Private>False</Private>
<IncludeOutputGroupsInVSIX>
</IncludeOutputGroupsInVSIX>
<IncludeOutputGroupsInVSIXLocalOnly>
</IncludeOutputGroupsInVSIXLocalOnly>
</ProjectReference>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Razor.Language\Microsoft.AspNetCore.Razor.Language.csproj">
<Project>{932f3c9c-a6c0-40d3-ba50-9309886242fc}</Project>
<Name>Microsoft.AspNetCore.Razor.Language</Name>
@ -246,6 +254,7 @@
<ItemGroup>
<VSIXSourceItem Include="..\..\src\Microsoft.CodeAnalysis.Remote.Razor\bin\$(Configuration)\net46\Microsoft.CodeAnalysis.Remote.Razor.dll" />
<VSIXSourceItem Include="..\..\src\Microsoft.AspNetCore.Mvc.Razor.Extensions\bin\$(Configuration)\net46\Microsoft.AspNetCore.Mvc.Razor.Extensions.dll" />
<VSIXSourceItem Include="..\..\src\Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X\bin\$(Configuration)\net46\Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.dll" />
<VSIXSourceItem Include="..\..\src\Microsoft.VisualStudio.LanguageServices.Razor\bin\$(Configuration)\net46\Microsoft.AspNetCore.Razor.Language.dll" />
<VSIXSourceItem Include="..\..\src\Microsoft.VisualStudio.LanguageServices.Razor\bin\$(Configuration)\net46\Microsoft.CodeAnalysis.Razor.dll" />
<VSIXSourceItem Include="..\..\src\Microsoft.VisualStudio.LanguageServices.Razor\bin\$(Configuration)\net46\Microsoft.CodeAnalysis.Razor.Workspaces.dll" />

View File

@ -11,6 +11,14 @@ using Microsoft.VisualStudio.Shell;
OldVersionUpperBound = "2.1.0.0",
NewVersion = "2.1.0.0")]
[assembly: ProvideBindingRedirection(
AssemblyName = "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X",
GenerateCodeBase = true,
PublicKeyToken = "adb9793829ddae60",
OldVersionLowerBound = "0.0.0.0",
OldVersionUpperBound = "2.1.0.0",
NewVersion = "2.1.0.0")]
[assembly: ProvideBindingRedirection(
AssemblyName = "Microsoft.AspNetCore.Razor.Language",
GenerateCodeBase = true,

View File

@ -24,6 +24,7 @@
<Asset Type="Microsoft.VisualStudio.MefComponent" d:Source="Project" d:ProjectName="%CurrentProject%" Path="|%CurrentProject%|" />
<Asset Type="Microsoft.ServiceHub.Service" d:Source="File" Path="razorLanguageService.servicehub.service.json" />
<Asset Type="Microsoft.VisualStudio.Assembly" Path="Microsoft.AspNetCore.Mvc.Razor.Extensions.dll" />
<Asset Type="Microsoft.VisualStudio.Assembly" Path="Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X.dll" />
<Asset Type="Microsoft.VisualStudio.Assembly" Path="Microsoft.AspNetCore.Razor.Language.dll" />
<Asset Type="Microsoft.VisualStudio.MefComponent" Path="Microsoft.AspNetCore.Razor.Language.dll" />
<Asset Type="Microsoft.VisualStudio.Assembly" Path="Microsoft.CodeAnalysis.Razor.dll" />