提交 6212f404 编写于 作者: H Heejae Chang 提交者: GitHub

Merge pull request #16655 from heejaechang/strongname

add support for StrongNameProvider for LUT.
......@@ -11,6 +11,7 @@
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServices.Remote;
using Roslyn.Test.Utilities;
using Roslyn.Utilities;
using Roslyn.VisualStudio.Next.UnitTests.Mocks;
using Xunit;
......@@ -35,6 +36,61 @@ public async Task TestCreation()
}
}
[Fact, Trait(Traits.Feature, Traits.Features.RemoteHost)]
public async Task TestStrongNameProvider()
{
var workspace = new AdhocWorkspace();
var filePath = typeof(SolutionServiceTests).Assembly.Location;
workspace.AddProject(
ProjectInfo.Create(
ProjectId.CreateNewId(), VersionStamp.Create(), "test", "test.dll", LanguageNames.CSharp,
filePath: filePath, outputFilePath: filePath));
var service = await GetSolutionServiceAsync(workspace.CurrentSolution);
var solutionChecksum = await workspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None);
var solution = await service.GetSolutionAsync(solutionChecksum, CancellationToken.None);
var compilationOptions = solution.Projects.First().CompilationOptions;
Assert.True(compilationOptions.StrongNameProvider is DesktopStrongNameProvider);
Assert.True(compilationOptions.XmlReferenceResolver is XmlFileResolver);
var dirName = PathUtilities.GetDirectoryName(filePath);
var array = new[] { dirName, dirName };
Assert.Equal(Hash.CombineValues(array, StringComparer.Ordinal), compilationOptions.StrongNameProvider.GetHashCode());
Assert.Equal(((XmlFileResolver)compilationOptions.XmlReferenceResolver).BaseDirectory, dirName);
}
[Fact, Trait(Traits.Feature, Traits.Features.RemoteHost)]
public async Task TestStrongNameProviderEmpty()
{
var workspace = new AdhocWorkspace();
var filePath = "testLocation";
workspace.AddProject(
ProjectInfo.Create(
ProjectId.CreateNewId(), VersionStamp.Create(), "test", "test.dll", LanguageNames.CSharp,
filePath: filePath, outputFilePath: filePath));
var service = await GetSolutionServiceAsync(workspace.CurrentSolution);
var solutionChecksum = await workspace.CurrentSolution.State.GetChecksumAsync(CancellationToken.None);
var solution = await service.GetSolutionAsync(solutionChecksum, CancellationToken.None);
var compilationOptions = solution.Projects.First().CompilationOptions;
Assert.True(compilationOptions.StrongNameProvider is DesktopStrongNameProvider);
Assert.True(compilationOptions.XmlReferenceResolver is XmlFileResolver);
var array = new string[] { };
Assert.Equal(Hash.CombineValues(array, StringComparer.Ordinal), compilationOptions.StrongNameProvider.GetHashCode());
Assert.Equal(((XmlFileResolver)compilationOptions.XmlReferenceResolver).BaseDirectory, null);
}
[Fact, Trait(Traits.Feature, Traits.Features.RemoteHost)]
public async Task TestCreationWithOption()
{
......
// Copyright (c) Microsoft. All Rights Reserved. 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.Collections.Immutable;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Diagnostics;
......@@ -160,7 +162,11 @@ private async Task<Solution> UpdateProjectAsync(Project project, ProjectStateChe
// changed compilation options
if (oldProjectChecksums.CompilationOptions != newProjectChecksums.CompilationOptions)
{
project = project.WithCompilationOptions(await _assetService.GetAssetAsync<CompilationOptions>(newProjectChecksums.CompilationOptions, _cancellationToken).ConfigureAwait(false));
project = project.WithCompilationOptions(
FixUpCompilationOptions(
project.State.ProjectInfo.Attributes,
await _assetService.GetAssetAsync<CompilationOptions>(
newProjectChecksums.CompilationOptions, _cancellationToken).ConfigureAwait(false)));
}
// changed parse options
......@@ -424,7 +430,11 @@ private async Task<ProjectInfo> CreateProjectInfoAsync(Checksum projectChecksum)
Contract.ThrowIfFalse(_baseSolution.Workspace.Services.IsSupported(projectInfo.Language));
var compilationOptions = await _assetService.GetAssetAsync<CompilationOptions>(projectSnapshot.CompilationOptions, _cancellationToken).ConfigureAwait(false);
var compilationOptions = FixUpCompilationOptions(
projectInfo,
await _assetService.GetAssetAsync<CompilationOptions>(
projectSnapshot.CompilationOptions, _cancellationToken).ConfigureAwait(false));
var parseOptions = await _assetService.GetAssetAsync<ParseOptions>(projectSnapshot.ParseOptions, _cancellationToken).ConfigureAwait(false);
var p2p = await CreateCollectionAsync<ProjectReference>(projectSnapshot.ProjectReferences).ConfigureAwait(false);
......@@ -497,5 +507,58 @@ private Project AddDocument(Project project, DocumentInfo documentInfo)
{
return project.Solution.AddDocument(documentInfo).GetProject(project.Id);
}
private CompilationOptions FixUpCompilationOptions(ProjectInfo.ProjectAttributes info, CompilationOptions compilationOptions)
{
return compilationOptions.WithXmlReferenceResolver(GetXmlResolver(info.FilePath))
.WithStrongNameProvider(new DesktopStrongNameProvider(GetStrongNameKeyPaths(info)));
}
private static XmlFileResolver GetXmlResolver(string filePath)
{
// Given filePath can be any arbitary string project is created with.
// for primary solution in host such as VSWorkspace, ETA or MSBuildWorkspace
// filePath will point to actual file on disk, but in memory solultion, or
// one from AdhocWorkspace and etc, FilePath can be a random string.
// Make sure we return only if given filePath is in right form.
if (!PathUtilities.IsAbsolute(filePath))
{
// xmlFileResolver can only deal with absolute path
// return Default
return XmlFileResolver.Default;
}
return new XmlFileResolver(PathUtilities.GetDirectoryName(filePath));
}
private ImmutableArray<string> GetStrongNameKeyPaths(ProjectInfo.ProjectAttributes info)
{
// Given FilePath/OutputFilePath can be any arbitary strings project is created with.
// for primary solution in host such as VSWorkspace, ETA or MSBuildWorkspace
// filePath will point to actual file on disk, but in memory solultion, or
// one from AdhocWorkspace and etc, FilePath/OutputFilePath can be a random string.
// Make sure we return only if given filePath is in right form.
if (info.FilePath == null && info.OutputFilePath == null)
{
// return empty since that is what IDE does for this case
// see AbstractProject.GetStrongNameKeyPaths
return ImmutableArray<string>.Empty;
}
var builder = ArrayBuilder<string>.GetInstance();
if (info.FilePath != null && PathUtilities.IsAbsolute(info.FilePath))
{
// desktop strong name provider only knows how to deal with absolute path
builder.Add(PathUtilities.GetDirectoryName(info.FilePath));
}
if (info.OutputFilePath != null && PathUtilities.IsAbsolute(info.OutputFilePath))
{
// desktop strong name provider only knows how to deal with absolute path
builder.Add(PathUtilities.GetDirectoryName(info.OutputFilePath));
}
return builder.ToImmutableAndFree();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册